diff --git a/README.md b/README.md index 976553e..59a5081 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,19 @@ # tiny-invariant 🔬💥 -[![Build Status](https://travis-ci.org/alexreardon/tiny-invariant.svg?branch=master)](https://travis-ci.org/alexreardon/tiny-invariant) -[![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) +![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/alexreardon/tiny-invariant/test.yml) ![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet) -[![minzip](https://img.shields.io/bundlephobia/minzip/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) -[![Downloads per month](https://img.shields.io/npm/dm/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) +![npm bundle size](https://img.shields.io/bundlephobia/minzip/tiny-invariant) +![NPM Downloads](https://img.shields.io/npm/dm/tiny-invariant) -A tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative. +`tiny-invariant` is a _tiny_, widely-supported, zero-dependency alternative to [`invariant`](https://www.npmjs.com/package/invariant). + +`tiny-invariant` - when every byte counts! ## What is `invariant`? 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. -```js +```ts import invariant from 'tiny-invariant'; invariant(truthyValue, 'This should not throw!'); @@ -22,23 +23,23 @@ invariant(falsyValue, 'This will throw!'); ``` ## Why `tiny-invariant`? -`tiny-invariant` is a *tiny*, widely-supported, zero-dependency alternative to [`invariant`](https://www.npmjs.com/package/invariant). See below for details on just how compact it can get. - -`tiny-invariant` - when every byte counts! - -## Error Messages -`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: +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 code for `sprintf` logic and instead encourages consumers to leverage [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) for message formatting. -```js +```ts invariant(condition, `Hello, ${name} - how are you today?`); ``` -You can also provide a function to generate your message, for when your message is expensive to create +## Error Messages + +`tiny-invariant` allows you to pass a `string` message, or a function that returns a `string` message. Using a function that returns a message is helpful when your message is expensive to create. -```js +```ts import invariant from 'tiny-invariant'; +invariant(condition, `Hello, ${name} - how are you today?`); + +// Using a function is helpful when your message is expensive invariant(value, () => getExpensiveMessage()); ``` @@ -73,13 +74,13 @@ npm install tiny-invariant --save Big idea: you will want your compiler to convert this code: -```js +```ts invariant(condition, 'My cool message that takes up a lot of kbs'); ``` Into this: -```js +```ts if (!condition) { if ('production' !== process.env.NODE_ENV) { invariant(false, 'My cool message that takes up a lot of kbs'); @@ -94,7 +95,7 @@ if (!condition) { Your bundler can then drop the code in the `"production" !== process.env.NODE_ENV` block for your production builds to end up with this: -```js +```ts if (!condition) { invariant(false); }