Skip to content

Commit a0583f7

Browse files
authored
Shim refactor (#550)
* Refactor to organize shims by Object * [AUTOMATED]: Prettier Code Styling * [AUTOMATED]: License Headers
1 parent 0fa319e commit a0583f7

File tree

4 files changed

+76
-66
lines changed

4 files changed

+76
-66
lines changed

packages/polyfill/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
*/
1616

1717
import './src/polyfills/promise';
18-
import './src/shims/find';
19-
import './src/shims/findIndex';
18+
import './src/shims/Array';
19+
import './src/shims/String';

packages/polyfill/src/shims/findIndex.ts renamed to packages/polyfill/src/shims/Array.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,55 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* This is the Array.prototype.find polyfill from MDN
19+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
20+
* https://tc39.github.io/ecma262/#sec-array.prototype.find
21+
*/
22+
if (!Array.prototype.find) {
23+
Object.defineProperty(Array.prototype, 'find', {
24+
value: function(predicate) {
25+
// 1. Let O be ? ToObject(this value).
26+
if (this == null) {
27+
throw new TypeError('"this" is null or not defined');
28+
}
29+
30+
var o = Object(this);
31+
32+
// 2. Let len be ? ToLength(? Get(O, "length")).
33+
var len = o.length >>> 0;
34+
35+
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
36+
if (typeof predicate !== 'function') {
37+
throw new TypeError('predicate must be a function');
38+
}
39+
40+
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
41+
var thisArg = arguments[1];
42+
43+
// 5. Let k be 0.
44+
var k = 0;
45+
46+
// 6. Repeat, while k < len
47+
while (k < len) {
48+
// a. Let Pk be ! ToString(k).
49+
// b. Let kValue be ? Get(O, Pk).
50+
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
51+
// d. If testResult is true, return kValue.
52+
var kValue = o[k];
53+
if (predicate.call(thisArg, kValue, k, o)) {
54+
return kValue;
55+
}
56+
// e. Increase k by 1.
57+
k++;
58+
}
59+
60+
// 7. Return undefined.
61+
return undefined;
62+
}
63+
});
64+
}
65+
1766
/**
1867
* This is the Array.prototype.findIndex polyfill from MDN
1968
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

packages/polyfill/src/shims/String.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This is the String.prototype.startsWith polyfill from MDN
19+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
20+
*/
21+
if (!String.prototype.startsWith) {
22+
String.prototype.startsWith = function(search, pos) {
23+
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
24+
};
25+
}

packages/polyfill/src/shims/find.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)