{"name":"outvariant","version":"1.2.0","description":"Type-safe implementation of invariant with positionals.","main":"lib/index.js","typings":"lib/index.d.ts","author":{"name":"Artem Zakharchenko"},"license":"MIT","scripts":{"test":"jest","clean":"rimraf ./lib","build":"yarn clean && tsc","prepublishOnly":"yarn test && yarn build"},"keywords":["invariant","outvariant","exception","positional"],"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"},"repository":{"type":"git","url":"https://github.com/open-draft/outvariant"},"_id":"outvariant@1.2.0","dist":{"shasum":"c904d30dc36d70d93902b13641aa1fda69ae8c0c","integrity":"sha512-OKEOPXtvIeSohFl9avZwm1BL/awT49PV/HQHyc0RHcdICkvj5XP+j+IHhsSoV7nT5gDtMJTlWXqVpA/F8yoi2A==","tarball":"http://registry-npm.ywwl.com/outvariant/download/outvariant-1.2.0.tgz","fileCount":10,"unpackedSize":6663,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhCq1hCRA9TVsSAnZWagAA4dQQAIECnWYBEjA+AIsAkdHO\neJydv3J87J7zf6zZkxBnaEUeCu1BuM3qZ00VsaMyGK0w/jHHAkHaW8QaAwan\nH9W7iQP29z4TI/yzr1jrRcZoGCVrDykLE5g6uHcT6uCWS5+9MK2gFfHtsia/\nLY1ZB3larchKw05Sa3Z3mnMGvmC9dn7UEc/4ULHsj3BOWesghGXdUmbRXvnF\n5V50rE/NuWHLeNSmDPblPicHuiP1FRjd14fmzbXpHntOS8EIoQDbu8oXJYO7\ni10amQTT+iuat/1mxST/SMvHAtdA2sI30p7eyy4OIbZzEisKTIB57cukpZ0a\neczzMNbnQO2b3XOdCwAWzC/kMd5UNsGG6v8raRYGGZOCuwdbYcfYKqMIn7uG\nTlAKo+3rot9FcwRXto0+uOHYfw707OdRbHAt/bOdc945/qhXJrvizsTm/BLU\n46vMA9dQoaAUovwJXs7wM/eQn7sFdp2R65qn39YDS6ScN8fJaBA10wZXtTec\nIeIXu7XSD1fWqVMHtrkigI73AU9SBsZFU60o4AGAgkLkhH+Et+Z+4kZhgOQV\nsgu1AlUEhs9Y0JqvHUk4biosZFCAV2mFtI4qQTPBDrC4OsMChsFHprv4BJrk\nm8PmtZfnSHXBT13ee3ALrmLRVLoLdQ9I1S576eshXqSq9oVN9DlAOM6ZaDHg\nxETo\r\n=zlE4\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCID3ZA0y2+or3uDtn5/8g4QzZ+9ZDIih609/leIDP10UiAiAouzkhEb/tD/w7idUa7vXJq2UIS00DrJMfxKAUaoQhTA=="}],"size":2721,"noattachment":false,"key":"/outvariant/-/outvariant-1.2.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.2.0_1628089697115_0.03295952541227565"},"_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":1628089697271,"_cnpm_publish_time":1628089697271,"dist-tags":{"latest":"1.4.3"}}