From b9022f721c18ace05fa6dd976b215d08265301c4 Mon Sep 17 00:00:00 2001 From: Stephen Hyde Date: Wed, 6 Mar 2024 17:47:27 -0500 Subject: [PATCH] clarify message functionality in readme --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 761d141..90db978 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ invariant(falsyValue, 'This will throw!'); // Error('Invariant violation: This will throw!'); ``` +## 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: + +```js +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 ```js @@ -29,13 +37,7 @@ import invariant from 'tiny-invariant'; invariant(value, () => getExpensiveMessage()); ``` -## Why `tiny-invariant`? - -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: - -```js -invariant(condition, `Hello, ${name} - how are you today?`); -``` +When `process.env.NODE_ENV` is set to `production`, the message will be replaced with the generic message `Invariant failed`. ## Type narrowing