{"name":"outvariant","version":"1.0.0","description":"Yet another TypeScript implementation of invariant.","main":"invariant.js","typings":"invariant.d.ts","author":{"name":"Artem Zakharchenko"},"license":"MIT","scripts":{"test":"jest","clean":"rimraf invariant.js invariant.d.ts","build":"yarn clean && tsc","prepublishOnly":"yarn test && yarn build"},"devDependencies":{"@types/jest":"^26.0.23","jest":"^27.0.6","rimraf":"^3.0.2","ts-jest":"^27.0.3","ts-node":"^10.0.0","typescript":"^4.3.4"},"_id":"outvariant@1.0.0","dist":{"shasum":"d6765b498e79084e7d8fc144f2f4bcfc00b8a9dd","integrity":"sha512-L+SUumIfPD5op8rGZDtp/3PDoWhLEMd0M8WcUKVJPQkg8vl9WibBLDu94IKcI+Qfh8tIlyhDJ6tscSf1gclFlw==","tarball":"http://registry-npm.ywwl.com/outvariant/download/outvariant-1.0.0.tgz","fileCount":7,"unpackedSize":4893,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg3QK9CRA9TVsSAnZWagAAsF4QAIjNRy04xD2lE5Y0ZICj\nqis/By67uFFLK1eIvzbj5FpqpBTDGRxFuFJk7LgWn7qgehUL+SG/3ipRBvSL\n82PdZG3U82EfRqaeXg+yVhEQkfo7S48XNtW79muUiLI0O1hYj040uRLeWlp8\nVCYIYzHy4MvaxSHNUYad+Q7RE8Z+mZEL+5oTIAF2MS9QJxGdqDhXkK9yOvI1\n5p2sYYWg1qdNbYCddePzTf47qbxbq5Lss7py9W5K7B9Yc4A6t1GYft/zXKFr\nmOmhXiK4EccCewYrwskvVBMtPunpsLKIRdF7nIIuvyZFUJJ/TK+Nzg1GfMUL\niBk7oy+1qGPZ799dlahBOWMt3fFD6534GllcTXYwtFXFBsvJaFmVmnu7ntxh\nnyKevT4ewHrI31rJUCDt6462C8gIm5xRfKos0NtGySzpU0Lb8S+EXo7oTwKj\nfAKfiWSqoYUczsaDIIvqP2RfF76FVvAkYsSBh9tuxb3k9vD7izUQ2kAQsDGq\ntCW8BUtd4Og9RnifgbQm6xglNSHnAGFH/Cuf1jT/9SHwb2PyDRl+G7cPthrv\ntvUUgzZunXlOpJiIw6E4HnQSv7Jqq7L8nPrTFf2UjB20dOvHjJf+Qi0wySl8\nYjSOOEL7B3mwaC4DzornBzugGrPWgzopAiANEKf5awWIZXc9NiZeZVTUKN2K\nkT1e\r\n=Tfvd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIB9ws1tZPjxpNmDJc71Q2pfkF+VndBiM8bxwqOx0Hi8KAiEAy/X8efrHRL/1BM7L3itMHCdywId3c1VSjkXyRP8Znno="}],"size":1909,"noattachment":false,"key":"/outvariant/-/outvariant-1.0.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":"kettanaito@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/outvariant_1.0.0_1625096892956_0.8156408507642783"},"_hasShrinkwrap":false,"readme":"# `outvariant`\n\nType-safe implementation of invariant with positionals.\n\n## Motivation\n\n### Type-safely\n\nThis implementation asserts the given predicate expression so it's treated as non-nullable after the `invariant` call:\n\n```ts\n// Regular invariant:\ninvariant(user, 'Failed to fetch')\nuser?.firstName // \"user\" is possibly undefined\n\n// Outvariant:\ninvariant(user, 'Failed to fetch')\nuser.firstName // OK, \"invariant\" ensured the \"user\" exists\n```\n\n### Positionals support\n\nThis implementation uses [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) to support dynamic number of positionals:\n\n```js\ninvariant(predicate, 'Expected %s but got %s', 'one', false)\n```\n\n## What is this for?\n\nInvariant is a shorthand function that asserts a given predicate and throws an error if that predicate is false.\n\nCompare these two pieces of code identical in behavior:\n\n```js\nif (!token) {\n  throw new Error(`Expected a token to be set but got ${typeof token}`)\n}\n```\n\n```js\nimport { invariant } from 'outvariant'\n\ninvariant(token, 'Expected a token to be set but got %s', typeof token)\n```\n\nUsing `invariant` reduces the visual nesting of the code and leads to cleaner error messages thanks to formatted positionals (i.e. the `%s` (string) positional above).\n\n## Usage\n\n### Install\n\n```sh\nnpm install outvariant\n# or\nyarn add outvariant\n```\n\n> You may want to install this library as a dev dependency (`-D`) based on your usage.\n\n### Write an assertion\n\n```js\nimport { invariant } from 'outvariant'\n\ninvariant(user, 'Failed to load: expected user, but got %o', user)\n```\n\n## Positionals\n\nThe following positional tokens are supported:\n\n| Token     | Expected value type                                     |\n| --------- | ------------------------------------------------------- |\n| `%s`      | String                                                  |\n| `%d`/`%i` | Number                                                  |\n| `%j`      | JSON (non-stringified)                                  |\n| `%o`      | Arbitrary object or object-like (i.e. a class instance) |\n\nWhenever present in the error message, a positional token will look up the value to insert in its place from the arguments given to `invariant`.\n\n```js\ninvariant(\n  false,\n  'Expected the \"%s\" property but got %j',\n  // Note that positionals are sensitive to order:\n  // - \"firstName\" replaces \"%s\" because it's first.\n  // - {\"id\":1} replaces \"%j\" because it's second.\n  'firstName',\n  {\n    id: 1,\n  }\n)\n```\n\n## Polymorphic errors\n\nIt is possible to throw a custom `Error` instance using `invariant.as`:\n\n```js\nimport { invariant } from 'outvariant'\n\nclass NetworkError extends Error {\n  constructor(message) {\n    super(message)\n  }\n}\n\ninvariant.as(NetworkError, res.fulfilled, 'Failed to handle response')\n```\n\nNote that providing a custom error constructor as the argument to `invariant.as` requires the custom constructor's signature to be compatible with the `Error` class constructor.\n\nIf your error constructor has a different signature, you can pass a function as the first argument to `invariant.as` that creates a new custom error instance.\n\n```js\nimport { invariant } from 'outvariant'\n\nclass NetworkError extends Error {\n  constructor(statusCode, message) {\n    super(message)\n    this.statusCode = statusCode\n  }\n}\n\ninvariant.as(\n  (message) => new NetworkError(500, message),\n  res.fulfilled,\n  'Failed to handle response'\n)\n```\n\nAbstract the error into helper functions for flexibility:\n\n```js\nfunction toNetworkError(statusCode) {\n  return (message) => new NetworkError(statusCode, message)\n}\n\ninvariant.as(toNetworkError(404), res?.user?.id, 'User Not Found')\ninvariant.as(toNetworkError(500), res.fulfilled, 'Internal Server Error')\n```\n\n## Contributing\n\nPlease [open an issue](https://github.com/open-draft/outvariant/issues) or [submit a pull request](https://github.com/open-draft/outvariant/pulls) if you wish to contribute. Thank you.\n","publish_time":1625096893097,"_cnpm_publish_time":1625096893097,"dist-tags":{"latest":"1.4.3"}}