{"name":"outvariant","version":"1.0.4","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"},"_id":"outvariant@1.0.4","dist":{"shasum":"d33d7fa70922e9488dfffd27d1451a8124c2bfec","integrity":"sha512-2+ax7EEnAxi+g+HN4VrFE5qfLnCb7OQJiJ2LS4K+PQ6lTeHbLpF/AvVTilt7FnTCQuXksQI7lPV1C91oOKeKXg==","tarball":"http://registry-npm.ywwl.com/outvariant/download/outvariant-1.0.4.tgz","fileCount":8,"unpackedSize":5089,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg3u/6CRA9TVsSAnZWagAAA5AP/3Jro7l3zNa6M2BQuuNG\nqsgrb0IjwY3kPS7P4GDaNowXg+zRtuaOD15YBG6MzJYQ/zFtosw8Iui9UhWZ\nZqFgzzxaXdYaXR3bEsZHL8OQCdVeo5uGv1NNOsHAUngvGsNaKyWPgR+9VZda\nRV/4UQxFOOP/yYbUS0dXV/l0cTNjSzKK+OSvRlH2jT4WBlHr3QbAG6wH7MMK\np2kg2zWUA2yhFaJOU20e48V91M7sDBwAA/AlaX6DtVmaF1oDs7m4PPW66hAa\nDPE302+DvfVUJJ/S1S3pRC6KYkSg2l5r8u4vIO5VGCr1IALaKRHvsoRNEH2k\n7kLcXru0MCspCwgGzV3TgTQgkHQ6t+pMxvwfjOkvp8+JEY5tR1roRKhDrww9\nO+6YcFWUuKRZVqWTT6UJ6vbFDnsBOJK4iWecXxfXQp4JOr+wIgLaqIIEBSKJ\nW2JTSeDdnKpg4zRCZA1zoLd/A3udnTv2+LQw96X/xBsni9rIvnXeiiwNktaJ\nbOIW1lzEOSpkiKNaxKDtvoXYR/qNqoRsZYiuDKO++O3RamRYcjnd/UX0Mr2f\nWMUV5FRahtW56p9nn+b0tsShs61aeu4RAOxWFfLqoHSYGWJfGxbe6H3xQUbA\nZxdYp1Ny272I131sCFV5uI22Us6ad+1pB9Fwoj3D/0nBstnJUE4c2oV/aE5l\nXxAc\r\n=+uUt\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDZ6oLwD8yIAgz5Ba1uHMmXwKX9SMJpMCSCwqROAC9NeQIhAINby+Snzq/30OSFn+7jH6i+f+Gwfj2TH49Zsp5e7Qpm"}],"size":2190,"noattachment":false,"key":"/outvariant/-/outvariant-1.0.4.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.4_1625223162064_0.816552274224202"},"_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":1625223162386,"_cnpm_publish_time":1625223162386,"dist-tags":{"latest":"1.4.3"}}