invariant-slim
v1.0.5
Published
A simpler version of Facebook's invariant implementation from React
Downloads
6
Maintainers
Readme
invariant-slim
A simpler version of Facebook's invariant implementation from React.
Installation
invariant-slim
is available as an npm package. Simply run npm install invariant-slim
command.
To use it in your project:
import invariant from 'invariant-slim'; // ES6 syntax
var invariant = require('invariant-slim'); // ES5 syntax
Usage
Use invariant()
to assert state which your program assumes to be true.
Pass in a condition to be evaluated and a format to be displayed as an error message.
The format respects the sprintf
-style syntax ('%s' will be replaced by whatever arguments are passed in).
Examples:
The following code will not throw:
invariant(
true,
'Demo message'
);
The following code will throw with a static message and will output the following error in the console - 'Invariant Violation: A static error message':
invariant(
false,
'A static error message'
);
The following code will throw with a dynamic formatted error message and will output the following error in the console - 'Invariant Violation: A dynamic error message arg1,arg2':
invariant(
false,
'A dynamic error message %s',
['arg1', 'arg2']
);
License
This software is released under the terms of WTFPL v2.0 license.