Comments (4)
👍
What does this project do? I see it referenced in a lot of high profile projects, but it's description just says "A mirror of Facebook's invariant (e.g. React, flux)."
Since the links to React and Flux's invariant are broken, I can't tell what's going on here without looking at the source. Even just a simple one paragraph description would be nice.
from invariant.
From the source code:
/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/
Basically,
invariant(somethingThatMustBeTrue, 'Something that I thought is true is not true.');
Will throw an Error
with a message "Something that I thought is true is not true." when somethingThatMustBeTrue
is not truth-y.
from invariant.
Knowing that I would still like the explanation for how is this better than a simple condition, e.g.
if (somethingThatMustBeTrue) {
throw new Error('Something that I thought is true is not true.');
}
from invariant.
@gajus, you can very well use if -> throw
, nothing wrong with that. I think it comes down to preference.
The way you'd write an invariant call is very similar to a test's assertion function:
// in a test:
assert.ok(someCondition, 'something bad happened!');
// in user code:
invariant(someCondition, 'something bad happened!');
// using an if:
if (!someCondition) {
throw new Error('something bad happened!');
}
Some people like that. It's also worth noting that if
and throw
are statements, while an invariant
call is an expression. So:
// this is invalid syntax
var mustBeTrue = true ? 'true string' : throw new Error('not true!');
// while this is valid syntax
var mustBeTrue = true ? 'true string' : invariant(false, 'not true!');
Some people find it convenient to throw errors from expressions. One last thing, invariant also provides printf-like string replacement:
// using an if:
if (!someCondition) {
throw new Error(
'[' + moduleName + '] got "' +
someCondition + '" but expected something truthy'
);
}
// using invariant:
invariant(
someCondition,
'[%s] got "%s" but expected something truthy',
moduleName, someCondition);
from invariant.
Related Issues (20)
- No LICENSE file HOT 2
- Make message optional HOT 1
- 2.2.2 is somewhat broken... HOT 1
- Typical use case isn't well documented HOT 4
- Zero, the number should not throw HOT 1
- No PATENTS file ! HOT 3
- flow type definition more permissive than the actual code HOT 2
- Support ES Modules
- Question: is `process.env` possibly undefined? HOT 1
- Update license to MIT HOT 2
- Invariant can't be imported in TypeScript under jest vs metro-bundler/packager (in react-native) HOT 6
- The invariant message will be stripped in production? HOT 1
- not ok TypeError: "file" argument must be a non-empty string
- es module version?
- The `framesToPop` is puzzling me HOT 2
- Is there a specific reason to not allow `invariant(cond, message, ...args)` ? HOT 1
- Invariant Violation: Module is not a registered callable module (calling onShouldStartLoadWithRequest)
- The original intention of developing this library HOT 2
- Why?
- Audit vulnerabilities detected in the invariant project on Tag: v2.2.4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from invariant.