1
+ export module Polyfills {
2
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find?v=control
3
+ export function find ( ) {
4
+ // https://tc39.github.io/ecma262/#sec-array.prototype.find
5
+ if ( ! Array . prototype . find ) {
6
+ Object . defineProperty ( Array . prototype , 'find' , {
7
+ value : function ( predicate ) {
8
+ // 1. const O be ? ToObject(this value).
9
+ if ( this == null ) {
10
+ throw new TypeError ( '"this" is null or not defined' ) ;
11
+ }
12
+
13
+ var o = Object ( this ) ;
14
+
15
+ // 2. const len be ? ToLength(? Get(O, "length")).
16
+ // tslint:disable-next-line:no-bitwise
17
+ var len = o . length >>> 0 ;
18
+
19
+ // 3. If IsCallable(predicate) is false, throw a TypeError exception.
20
+ if ( typeof predicate !== 'function' ) {
21
+ throw new TypeError ( 'predicate must be a function' ) ;
22
+ }
23
+
24
+ // 4. If thisArg was supplied, const T be thisArg; else const T be undefined.
25
+ var thisArg = arguments [ 1 ] ;
26
+
27
+ // 5. const k be 0.
28
+ var k = 0 ;
29
+
30
+ // 6. Repeat, while k < len
31
+ while ( k < len ) {
32
+ // a. const Pk be ! ToString(k).
33
+ // b. const kValue be ? Get(O, Pk).
34
+ // c. const testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
35
+ // d. If testResult is true, return kValue.
36
+ var kValue = o [ k ] ;
37
+ if ( predicate . call ( thisArg , kValue , k , o ) ) {
38
+ return kValue ;
39
+ }
40
+ // e. Increase k by 1.
41
+ k ++ ;
42
+ }
43
+
44
+ // 7. Return undefined.
45
+ return undefined ;
46
+ }
47
+ } ) ;
48
+ }
49
+ }
50
+ }
0 commit comments