invariant.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright 2013-2015, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. 'use strict';
  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. var NODE_ENV = process.env.NODE_ENV;
  21. var invariant = function(condition, format, a, b, c, d, e, f) {
  22. if (NODE_ENV !== 'production') {
  23. if (format === undefined) {
  24. throw new Error('invariant requires an error message argument');
  25. }
  26. }
  27. if (!condition) {
  28. var error;
  29. if (format === undefined) {
  30. error = new Error(
  31. 'Minified exception occurred; use the non-minified dev environment ' +
  32. 'for the full error message and additional helpful warnings.'
  33. );
  34. } else {
  35. var args = [a, b, c, d, e, f];
  36. var argIndex = 0;
  37. error = new Error(
  38. format.replace(/%s/g, function() { return args[argIndex++]; })
  39. );
  40. error.name = 'Invariant Violation';
  41. }
  42. error.framesToPop = 1; // we don't care about invariant's own frame
  43. throw error;
  44. }
  45. };
  46. module.exports = invariant;