/
invariant.js
59 lines (51 loc) · 1.46 KB
/
invariant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Facebook's invariant.
*
* [Related docs](https://github.com/zertosh/invariant)
*/
/* example
* invariant(true, 'This will not throw');
* // No errors
* invariant(false, 'This will throw an error with this message');
* // Error: Invariant Violation: This will throw an error with this message
*/
/* module
* env: all
* since: 1.17.0
*/
/* typescript
* export declare function invariant(
* condition: boolean,
* format?: string,
* a?: string,
* b?: string,
* c?: string,
* d?: string,
* e?: string,
* f?: string
* ): void;
*/
_('root');
exports = function(condition, format, a, b, c, d, e, f) {
const process = root.process || { env: { NODE_ENV: 'development' } };
if (process.env.NODE_ENV !== 'production') {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}
if (!condition) {
let error;
if (format === undefined) {
error = new Error(
'Minified exception occurred; use the non-minified dev environment ' +
'for the full error message and additional helpful warnings.'
);
} else {
const args = [a, b, c, d, e, f];
let argIndex = 0;
error = new Error(format.replace(/%s/g, () => args[argIndex++]));
error.name = 'Invariant Violation';
}
error.framesToPop = 1;
throw error;
}
};