IndexError.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.IndexError = IndexError;
  6. /**
  7. * Create a range error with the message:
  8. * 'Index out of range (index < min)'
  9. * 'Index out of range (index < max)'
  10. *
  11. * @param {number} index The actual index
  12. * @param {number} [min=0] Minimum index (included)
  13. * @param {number} [max] Maximum index (excluded)
  14. * @extends RangeError
  15. */
  16. function IndexError(index, min, max) {
  17. if (!(this instanceof IndexError)) {
  18. throw new SyntaxError('Constructor must be called with the new operator');
  19. }
  20. this.index = index;
  21. if (arguments.length < 3) {
  22. this.min = 0;
  23. this.max = min;
  24. } else {
  25. this.min = min;
  26. this.max = max;
  27. }
  28. if (this.min !== undefined && this.index < this.min) {
  29. this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')';
  30. } else if (this.max !== undefined && this.index >= this.max) {
  31. this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')';
  32. } else {
  33. this.message = 'Index out of range (' + this.index + ')';
  34. }
  35. this.stack = new Error().stack;
  36. }
  37. IndexError.prototype = new RangeError();
  38. IndexError.prototype.constructor = RangeError;
  39. IndexError.prototype.name = 'IndexError';
  40. IndexError.prototype.isIndexError = true;