{"name":"outvariant","version":"1.0.3","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.3","dist":{"shasum":"7341c07b257b689db8bad46df9df897321ff219f","integrity":"sha512-mCrkiOMvzFuHQUgSR0truUK/8eeVzK4EXbnOMHZCpR++pWcNeHLqDsu9ZtpyG4YI88CdNzJUFYoA0aKpDU2uIQ==","tarball":"http://registry-npm.ywwl.com/outvariant/download/outvariant-1.0.3.tgz","fileCount":8,"unpackedSize":4196,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg3QfpCRA9TVsSAnZWagAAjzMP/0hyW/1Pw3MgKcJrnCmj\nlVXT7JGKQXMl5qzNV287/y+x0VgypG5RRmW7w5kOKRFHjJ5ElLEmbB9aWmE3\nVIQW2EcpxEwvEGqFp8Iyz4JGwLtEzMKIX1QqBMfVj1XfzKJkfOsSIV6g/DIn\n1kcXuk0tlyyEdQsAp6AQ9L6hm/a46Z08bF6JI3w1kUo9vFzewWCbAjL5n5c7\nMvINoDqVAVT9y0QQOJsYiuc8EBkanId2rDaqpgHVyjtwu2cwPiseVw/vexUG\nz73eDW2w6rjUJgOxqIDPhczvX6jHdL6bWxMZonKiVKNEVDn6sXS4XF9f4h/F\nD/ev4mIjyK4KKry1gvLsBurwWnmVZPFyyXRqsQJw7Az+pE8MYhgrxS1uUDR3\nsXgucyQ/E6LGB9R/hRe8bC0LUBqnU6UUYFdYZ1ygFllCVoic9Tjbjr1OfDww\nB0fIwzUgT+YeURcxMamGJNQ1W7iB3xsQTMJ25afFBObXaJHjXZhxn51hU2Mq\nSKmQKpjI3zGx1pHujAJA2V6VIjk/8h/3hSb7lArG1bDhqRbA7VwwI9QlPMBb\nmd8hB0PXNt/L60MCjgdyifAl0wpL9+f42DVxSIeedk99AFuV4J3FlnwDJfnA\nyzhWyAdhSmPkp0GYyWFO6421fufSAO62Y74n6e5MjK9x3232H6vzXIGplMFS\nOsUU\r\n=iqtN\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHE6L7CHQ/YmjRNxXJRmRjWtzweYz9s9ASSegifpG/QJAiEAhV7ewes46w1dLgSvG91g+xQLcRdW3oTZMUaim/Uc4qU="}],"size":1967,"noattachment":false,"key":"/outvariant/-/outvariant-1.0.3.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.3_1625098217122_0.5849783681471479"},"_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":1625098217311,"_cnpm_publish_time":1625098217311,"dist-tags":{"latest":"1.4.3"}}