| [a762898] | 1 | # tiny-invariant 🔬💥
|
|---|
| 2 |
|
|---|
| 3 | [](https://travis-ci.org/alexreardon/tiny-invariant)
|
|---|
| 4 | [](https://www.npmjs.com/package/tiny-invariant) [](https://david-dm.org/alexreardon/tiny-invariant)
|
|---|
| 5 | 
|
|---|
| 6 | [](https://www.npmjs.com/package/tiny-invariant)
|
|---|
| 7 | [](https://www.npmjs.com/package/tiny-invariant)
|
|---|
| 8 |
|
|---|
| 9 | A tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative.
|
|---|
| 10 |
|
|---|
| 11 | ## What is `invariant`?
|
|---|
| 12 |
|
|---|
| 13 | An `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.
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | import invariant from 'tiny-invariant';
|
|---|
| 17 |
|
|---|
| 18 | invariant(truthyValue, 'This should not throw!');
|
|---|
| 19 |
|
|---|
| 20 | invariant(falsyValue, 'This will throw!');
|
|---|
| 21 | // Error('Invariant violation: This will throw!');
|
|---|
| 22 | ```
|
|---|
| 23 |
|
|---|
| 24 | You can also provide a function to generate your message, for when your message is expensive to create
|
|---|
| 25 |
|
|---|
| 26 | ```js
|
|---|
| 27 | import invariant from 'tiny-invariant';
|
|---|
| 28 |
|
|---|
| 29 | invariant(value, () => getExpensiveMessage());
|
|---|
| 30 | ```
|
|---|
| 31 |
|
|---|
| 32 | ## Why `tiny-invariant`?
|
|---|
| 33 |
|
|---|
| 34 | The [`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:
|
|---|
| 35 |
|
|---|
| 36 | ```js
|
|---|
| 37 | invariant(condition, `Hello, ${name} - how are you today?`);
|
|---|
| 38 | ```
|
|---|
| 39 |
|
|---|
| 40 | ## Type narrowing
|
|---|
| 41 |
|
|---|
| 42 | `tiny-invariant` is useful for correctly narrowing types for `flow` and `typescript`
|
|---|
| 43 |
|
|---|
| 44 | ```ts
|
|---|
| 45 | const value: Person | null = { name: 'Alex' }; // type of value == 'Person | null'
|
|---|
| 46 | invariant(value, 'Expected value to be a person');
|
|---|
| 47 | // type of value has been narrowed to 'Person'
|
|---|
| 48 | ```
|
|---|
| 49 |
|
|---|
| 50 | ## API: `(condition: any, message?: string | (() => string)) => void`
|
|---|
| 51 |
|
|---|
| 52 | - `condition` is required and can be anything
|
|---|
| 53 | - `message` optional `string` or a function that returns a `string` (`() => string`)
|
|---|
| 54 |
|
|---|
| 55 | ## Installation
|
|---|
| 56 |
|
|---|
| 57 | ```bash
|
|---|
| 58 | # yarn
|
|---|
| 59 | yarn add tiny-invariant
|
|---|
| 60 |
|
|---|
| 61 | # npm
|
|---|
| 62 | npm install tiny-invariant --save
|
|---|
| 63 | ```
|
|---|
| 64 |
|
|---|
| 65 | ## Dropping your `message` for kb savings!
|
|---|
| 66 |
|
|---|
| 67 | Big idea: you will want your compiler to convert this code:
|
|---|
| 68 |
|
|---|
| 69 | ```js
|
|---|
| 70 | invariant(condition, 'My cool message that takes up a lot of kbs');
|
|---|
| 71 | ```
|
|---|
| 72 |
|
|---|
| 73 | Into this:
|
|---|
| 74 |
|
|---|
| 75 | ```js
|
|---|
| 76 | if (!condition) {
|
|---|
| 77 | if ('production' !== process.env.NODE_ENV) {
|
|---|
| 78 | invariant(false, 'My cool message that takes up a lot of kbs');
|
|---|
| 79 | } else {
|
|---|
| 80 | invariant(false);
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | ```
|
|---|
| 84 |
|
|---|
| 85 | - **Babel**: recommend [`babel-plugin-dev-expression`](https://www.npmjs.com/package/babel-plugin-dev-expression)
|
|---|
| 86 | - **TypeScript**: recommend [`tsdx`](https://github.com/jaredpalmer/tsdx#invariant) (or you can run `babel-plugin-dev-expression` after TypeScript compiling)
|
|---|
| 87 |
|
|---|
| 88 | Your bundler can then drop the code in the `"production" !== process.env.NODE_ENV` block for your production builds to end up with this:
|
|---|
| 89 |
|
|---|
| 90 | ```js
|
|---|
| 91 | if (!condition) {
|
|---|
| 92 | invariant(false);
|
|---|
| 93 | }
|
|---|
| 94 | ```
|
|---|
| 95 |
|
|---|
| 96 | - 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
|
|---|
| 97 | - Webpack: [instructions](https://webpack.js.org/guides/production/#specify-the-mode)
|
|---|
| 98 |
|
|---|
| 99 | ## Builds
|
|---|
| 100 |
|
|---|
| 101 | - We have a `es` (EcmaScript module) build
|
|---|
| 102 | - We have a `cjs` (CommonJS) build
|
|---|
| 103 | - We have a `umd` (Universal module definition) build in case you needed it
|
|---|
| 104 |
|
|---|
| 105 | We expect `process.env.NODE_ENV` to be available at module compilation. We cache this value
|
|---|
| 106 |
|
|---|
| 107 | ## That's it!
|
|---|
| 108 |
|
|---|
| 109 | 🤘
|
|---|