UNPKG

1.42 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10/**
11 * Use invariant() to assert state which your program assumes to be true.
12 *
13 * Provide sprintf-style format (only %s is supported) and arguments
14 * to provide information about what broke and what you were
15 * expecting.
16 *
17 * The invariant message will be stripped in production, but the invariant
18 * will remain to ensure logic does not differ in production.
19 */
20
21var NODE_ENV = process.env.NODE_ENV;
22
23var invariant = function(condition, format, a, b, c, d, e, f) {
24 if (NODE_ENV !== 'production') {
25 if (format === undefined) {
26 throw new Error('invariant requires an error message argument');
27 }
28 }
29
30 if (!condition) {
31 var error;
32 if (format === undefined) {
33 error = new Error(
34 'Minified exception occurred; use the non-minified dev environment ' +
35 'for the full error message and additional helpful warnings.'
36 );
37 } else {
38 var args = [a, b, c, d, e, f];
39 var argIndex = 0;
40 error = new Error(
41 format.replace(/%s/g, function() { return args[argIndex++]; })
42 );
43 error.name = 'Invariant Violation';
44 }
45
46 error.framesToPop = 1; // we don't care about invariant's own frame
47 throw error;
48 }
49};
50
51module.exports = invariant;