invariant.js 1.5 KB

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