{"bugs":{"url":"https://github.com/alexreardon/tiny-invariant/issues"},"dist":{"shasum":"bab79f8c36614e2af7e8843b6aa142b93e0e6091","tarball":"https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.2.0-beta.1.tgz","fileCount":10,"integrity":"sha512-2tn9fAGY1kIj6ZIfHSzaU0tE8qDZ99SBBjdjPQig+U0mWNz5rCS0Z5r7htWeMuoasBTYymbPRUHdUTntZzAQhw==","signatures":[{"sig":"MEUCIQDo3AdAlwpthGMhO+hAf85o/z27Pu/3/F8/gSmsKnBJlgIgNvhUFoCv/ya6wRCQxAu2UOKDeuZwK6+OXWVidydrXMI=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":11327},"main":"dist/tiny-invariant.cjs.js","name":"tiny-invariant","types":"dist/tiny-invariant.d.ts","author":{"name":"Alex Reardon","email":"alexreardon@gmail.com"},"module":"dist/tiny-invariant.esm.js","readme":"# tiny-invariant 🔬💥\n\n[![Build Status](https://travis-ci.org/alexreardon/tiny-invariant.svg?branch=master)](https://travis-ci.org/alexreardon/tiny-invariant)\n[![npm](https://img.shields.io/npm/v/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) [![dependencies](https://david-dm.org/alexreardon/tiny-invariant.svg)](https://david-dm.org/alexreardon/tiny-invariant)\n![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet)\n[![minzip](https://img.shields.io/bundlephobia/minzip/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant)\n[![Downloads per month](https://img.shields.io/npm/dm/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant)\n\nA tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative.\n\n## What is `invariant`?\n\nAn `invariant` function takes a value, and if the value is [falsy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) then the `invariant` function will throw. If the value is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy), then the function will not throw.\n\n```js\nimport invariant from 'tiny-invariant';\n\ninvariant(truthyValue, 'This should not throw!');\n\ninvariant(falsyValue, 'This will throw!');\n// Error('Invariant violation: This will throw!');\n```\n\n## Why `tiny-invariant`?\n\nThe [`library: invariant`](https://www.npmjs.com/package/invariant) supports passing in arguments to the `invariant` function in a sprintf style `(condition, format, a, b, c, d, e, f)`. It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. `tiny-invariant` has dropped all of the sprintf logic. `tiny-invariant` allows you to pass a single string message. With [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) there is really no need for a custom message formatter to be built into the library. If you need a multi part message you can just do this: `invariant(condition, 'Hello, ${name} - how are you today?')`\n\n## Type narrowing\n\n`tiny-invariant` is useful for correctly narrowing types for `flow` and `typescript`\n\n```ts\nconst value: Person | null = { name: 'Alex' }; // type of value == 'Person | null'\ninvariant(value, 'Expected value to be a person');\n// type of value has been narrowed to 'Person'\n```\n\n## API: `(condition: any, message?: string | (() => string)) => void`\n\n- `condition` is required and can be anything\n- `message` optional `string` or a function that returns a `string` (`() => string`)\n\nYour `message` can be a function that returns a `string` (`() => string`) for the cases where you want to lazily create your error message, such as when they are expensive to make.\n\n```ts\ninvariant(value, () => getExpensiveMessage());\n```\n\n## Installation\n\n```bash\n# yarn\nyarn add tiny-invariant\n\n# npm\nnpm add tiny-invariant --save\n```\n\n## Dropping your `message` for kb savings!\n\nBig idea: you will want your compiler to convert this code:\n\n```js\ninvariant(condition, 'My cool message that takes up a lot of kbs');\n```\n\nInto this:\n\n```js\nif (!condition) {\n  if ('production' !== process.env.NODE_ENV) {\n    invariant(false, 'My cool message that takes up a lot of kbs');\n  } else {\n    invariant(false);\n  }\n}\n```\n\n- **Babel**: recommend [`babel-plugin-dev-expression`](https://www.npmjs.com/package/babel-plugin-dev-expression)\n- **TypeScript**: recommend [`tsdx`](https://github.com/jaredpalmer/tsdx#invariant) (or you can run `babel-plugin-dev-expression` after TypeScript compiling)\n\nYour bundler can then drop the code in the `\"production\" !== process.env.NODE_ENV` block for your production builds to end up with this:\n\n```js\nif (!condition) {\n  invariant(false);\n}\n```\n\n- rollup: use [rollup-plugin-replace](https://github.com/rollup/rollup-plugin-replace) and set `NODE_ENV` to `production` and then `rollup` will treeshake out the unused code\n- Webpack: [instructions](https://webpack.js.org/guides/production/#specify-the-mode)\n\n## Builds\n\n- We have a `es` (EcmaScript module) build (because you _know_ you want to deduplicate this super heavy library)\n- We have a `cjs` (CommonJS) build\n- We have a `umd` (Universal module definition) build in case you needed it\n\nWe expect `process.env.NODE_ENV` to be available at module compilation. We cache this value\n\n## That's it!\n\n🤘\n","gitHead":"f2f2ca9ae8b74cb642960dfa444ddf96f59ae087","license":"MIT","scripts":{"test":"yarn jest","build":"yarn build:clean && yarn build:dist && yarn build:typescript","validate":"yarn prettier:check && yarn typescript:check","test:size":"yarn build && yarn size-limit","build:dist":"yarn rollup --config rollup.config.js","build:flow":"cp src/tiny-invariant.js.flow dist/tiny-invariant.cjs.js.flow","build:clean":"rimraf dist","prepublishOnly":"yarn build","prettier:check":"yarn prettier --write src/** test/**","prettier:write":"yarn prettier --debug-check src/** test/**","build:typescript":"tsc ./src/tiny-invariant.ts --emitDeclarationOnly --declaration --outDir ./dist","typescript:check":"yarn tsc --noEmit src/*.ts test/*.ts"},"_npmUser":{"name":"alexreardon","email":"alexreardon@gmail.com"},"homepage":"https://github.com/alexreardon/tiny-invariant#readme","keywords":["invariant","error","assert","asserts"],"repository":{"url":"git+https://github.com/alexreardon/tiny-invariant.git","type":"git"},"size-limit":[{"path":"dist/tiny-invariant.min.js","limit":"175B"},{"path":"dist/tiny-invariant.js","limit":"176B"},{"path":"dist/tiny-invariant.cjs.js","limit":"164B"},{"path":"dist/tiny-invariant.esm.js","limit":"49B","import":"foo"}],"_npmVersion":"7.11.1","description":"A tiny invariant function","directories":{},"maintainers":[{"name":"alexreardon","email":"alexreardon@gmail.com"}],"sideEffects":false,"_nodeVersion":"14.15.0","_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"jest":"^27.3.1","tslib":"^2.3.1","rimraf":"^3.0.2","rollup":"^2.58.3","ts-jest":"^27.0.7","prettier":"^2.4.1","size-limit":"^6.0.3","typescript":"^4.4.4","@types/jest":"^27.0.2","expect-type":"^0.13.0","@types/rollup":"^0.54.0","rollup-plugin-terser":"^7.0.2","@rollup/plugin-replace":"^3.0.0","@rollup/plugin-typescript":"^8.3.0","@size-limit/preset-small-lib":"^6.0.3"},"_npmOperationalInternal":{"tmp":"tmp/tiny-invariant_1.2.0-beta.1_1635327882904_0.9965063638425529","host":"s3://npm-registry-packages"},"_id":"tiny-invariant@1.2.0-beta.1","version":"1.2.0-beta.1"}