invariant.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. */
  9. 'use strict';
  10. var validateFormat = process.env.NODE_ENV !== "production" ? function (format) {} : function (format) {
  11. if (format === undefined) {
  12. throw new Error('invariant(...): Second argument must be a string.');
  13. }
  14. };
  15. /**
  16. * Use invariant() to assert state which your program assumes to be true.
  17. *
  18. * Provide sprintf-style format (only %s is supported) and arguments to provide
  19. * information about what broke and what you were expecting.
  20. *
  21. * The invariant message will be stripped in production, but the invariant will
  22. * remain to ensure logic does not differ in production.
  23. */
  24. function invariant(condition, format) {
  25. for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  26. args[_key - 2] = arguments[_key];
  27. }
  28. validateFormat(format);
  29. if (!condition) {
  30. var error;
  31. if (format === undefined) {
  32. error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.');
  33. } else {
  34. var argIndex = 0;
  35. error = new Error(format.replace(/%s/g, function () {
  36. return String(args[argIndex++]);
  37. }));
  38. error.name = 'Invariant Violation';
  39. }
  40. error.framesToPop = 1; // Skip invariant's own stack frame.
  41. throw error;
  42. }
  43. }
  44. module.exports = invariant;