1
+ if ( ! String . prototype . endsWith ) {
2
+ String . prototype . endsWith = function ( search , this_len ) {
3
+ if ( this_len === undefined || this_len > this . length ) {
4
+ this_len = this . length ;
5
+ }
6
+ return this . substring ( this_len - search . length , this_len ) === search ;
7
+ } ;
8
+ }
9
+ if ( ! String . prototype . startsWith ) {
10
+ Object . defineProperty ( String . prototype , 'startsWith' , {
11
+ value : function ( search , pos ) {
12
+ pos = ! pos || pos < 0 ? 0 : + pos ;
13
+ return this . substring ( pos , pos + search . length ) === search ;
14
+ }
15
+ } ) ;
16
+ }
17
+ if ( ! String . prototype . includes ) {
18
+ String . prototype . includes = function ( search , start ) {
19
+ 'use strict' ;
20
+ if ( typeof start !== 'number' ) {
21
+ start = 0 ;
22
+ }
23
+
24
+ if ( start + search . length > this . length ) {
25
+ return false ;
26
+ } else {
27
+ return this . indexOf ( search , start ) !== - 1 ;
28
+ }
29
+ } ;
30
+ }
31
+ // https://tc39.github.io/ecma262/#sec-array.prototype.find
32
+ if ( ! Array . prototype . find ) {
33
+ Object . defineProperty ( Array . prototype , 'find' , {
34
+ value : function ( predicate ) {
35
+ // 1. Let O be ? ToObject(this value).
36
+ if ( this == null ) {
37
+ throw new TypeError ( '"this" is null or not defined' ) ;
38
+ }
39
+
40
+ var o = Object ( this ) ;
41
+
42
+ // 2. Let len be ? ToLength(? Get(O, "length")).
43
+ var len = o . length >>> 0 ;
44
+
45
+ // 3. If IsCallable(predicate) is false, throw a TypeError exception.
46
+ if ( typeof predicate !== 'function' ) {
47
+ throw new TypeError ( 'predicate must be a function' ) ;
48
+ }
49
+
50
+ // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
51
+ var thisArg = arguments [ 1 ] ;
52
+
53
+ // 5. Let k be 0.
54
+ var k = 0 ;
55
+
56
+ // 6. Repeat, while k < len
57
+ while ( k < len ) {
58
+ // a. Let Pk be ! ToString(k).
59
+ // b. Let kValue be ? Get(O, Pk).
60
+ // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
61
+ // d. If testResult is true, return kValue.
62
+ var kValue = o [ k ] ;
63
+ if ( predicate . call ( thisArg , kValue , k , o ) ) {
64
+ return kValue ;
65
+ }
66
+ // e. Increase k by 1.
67
+ k ++ ;
68
+ }
69
+
70
+ // 7. Return undefined.
71
+ return undefined ;
72
+ } ,
73
+ configurable : true ,
74
+ writable : true
75
+ } ) ;
76
+ }
77
+
78
+ // From https://github.com/kevlatus/polyfill-array-includes/blob/master/array-includes.js
79
+ if ( ! Array . prototype . includes ) {
80
+ Object . defineProperty ( Array . prototype , 'includes' , {
81
+ value : function ( searchElement , fromIndex ) {
82
+
83
+ // 1. Let O be ? ToObject(this value).
84
+ if ( this == null ) {
85
+ throw new TypeError ( '"this" is null or not defined' ) ;
86
+ }
87
+
88
+ var o = Object ( this ) ;
89
+
90
+ // 2. Let len be ? ToLength(? Get(O, "length")).
91
+ var len = o . length >>> 0 ;
92
+
93
+ // 3. If len is 0, return false.
94
+ if ( len === 0 ) {
95
+ return false ;
96
+ }
97
+
98
+ // 4. Let n be ? ToInteger(fromIndex).
99
+ // (If fromIndex is undefined, this step produces the value 0.)
100
+ var n = fromIndex | 0 ;
101
+
102
+ // 5. If n ≥ 0, then
103
+ // a. Let k be n.
104
+ // 6. Else n < 0,
105
+ // a. Let k be len + n.
106
+ // b. If k < 0, let k be 0.
107
+ var k = Math . max ( n >= 0 ? n : len - Math . abs ( n ) , 0 ) ;
108
+
109
+ function sameValueZero ( x , y ) {
110
+ return x === y || ( typeof x === 'number' && typeof y === 'number' && isNaN ( x ) && isNaN ( y ) ) ;
111
+ }
112
+
113
+ // 7. Repeat, while k < len
114
+ while ( k < len ) {
115
+ // a. Let elementK be the result of ? Get(O, ! ToString(k)).
116
+ // b. If SameValueZero(searchElement, elementK) is true, return true.
117
+ // c. Increase k by 1.
118
+ if ( sameValueZero ( o [ k ] , searchElement ) ) {
119
+ return true ;
120
+ }
121
+ k ++ ;
122
+ }
123
+
124
+ // 8. Return false
125
+ return false ;
126
+ }
127
+ } ) ;
128
+ }
0 commit comments