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