UNPKG

1.53 kBJavaScriptView Raw
1/* tslint:disable */
2/* eslint-disable no-extend-native */
3// https://tc39.github.io/ecma262/#sec-array.prototype.includes
4if (!Array.prototype.includes) {
5 Object.defineProperty(Array.prototype, 'includes', {
6 configurable: true,
7 value: function (searchElement, fromIndex) {
8
9 if (this == null) {
10 throw new TypeError('"this" is null or not defined');
11 }
12
13 // 1. Let O be ? ToObject(this value).
14 var o = Object(this);
15
16 // 2. Let len be ? ToLength(? Get(O, "length")).
17 var len = o.length >>> 0;
18
19 // 3. If len is 0, return false.
20 if (len === 0) {
21 return false;
22 }
23
24 // 4. Let n be ? ToInteger(fromIndex).
25 // (If fromIndex is undefined, this step produces the value 0.)
26 var n = fromIndex | 0;
27
28 // 5. If n ≥ 0, then
29 // a. Let k be n.
30 // 6. Else n < 0,
31 // a. Let k be len + n.
32 // b. If k < 0, let k be 0.
33 var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
34
35 function sameValueZero(x, y) {
36 return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
37 }
38
39 // 7. Repeat, while k < len
40 while (k < len) {
41 // a. Let elementK be the result of ? Get(O, ! ToString(k)).
42 // b. If SameValueZero(searchElement, elementK) is true, return true.
43 if (sameValueZero(o[k], searchElement)) {
44 return true;
45 }
46 // c. Increase k by 1.
47 k++;
48 }
49
50 // 8. Return false
51 return false;
52 }
53 });
54}
\No newline at end of file