UNPKG

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