Skip to content

Commit 4b6936a

Browse files
authored
fix(ie11): add Array.prototype.find/findIndex polyfills (#115)
These two functions are part of ES2015 and are not present in IE11. In the migration we removed closure compiler which was injecting these by default
1 parent c58719c commit 4b6936a

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/app.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
// Import the needed shims
17+
import './utils/shims';
18+
1619
// Import the createFirebaseNamespace function
1720
import { createFirebaseNamespace, FirebaseNamespace } from './app/firebase_app';
1821

src/utils/shims.ts

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* This is the Array.prototype.findIndex polyfill from MDN
3+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
4+
* https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
5+
*/
6+
if (!Array.prototype.findIndex) {
7+
Object.defineProperty(Array.prototype, 'findIndex', {
8+
value: function(predicate) {
9+
// 1. Let O be ? ToObject(this value).
10+
if (this == null) {
11+
throw new TypeError('"this" is null or not defined');
12+
}
13+
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 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, let T be thisArg; else let T be undefined.
25+
var thisArg = arguments[1];
26+
27+
// 5. Let k be 0.
28+
var k = 0;
29+
30+
// 6. Repeat, while k < len
31+
while (k < len) {
32+
// a. Let Pk be ! ToString(k).
33+
// b. Let kValue be ? Get(O, Pk).
34+
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
35+
// d. If testResult is true, return k.
36+
var kValue = o[k];
37+
if (predicate.call(thisArg, kValue, k, o)) {
38+
return k;
39+
}
40+
// e. Increase k by 1.
41+
k++;
42+
}
43+
44+
// 7. Return -1.
45+
return -1;
46+
}
47+
});
48+
}
49+
50+
/**
51+
* This is the Array.prototype.find polyfill from MDN
52+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
53+
* https://tc39.github.io/ecma262/#sec-array.prototype.find
54+
*/
55+
if (!Array.prototype.find) {
56+
Object.defineProperty(Array.prototype, 'find', {
57+
value: function(predicate) {
58+
// 1. Let O be ? ToObject(this value).
59+
if (this == null) {
60+
throw new TypeError('"this" is null or not defined');
61+
}
62+
63+
var o = Object(this);
64+
65+
// 2. Let len be ? ToLength(? Get(O, "length")).
66+
var len = o.length >>> 0;
67+
68+
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
69+
if (typeof predicate !== 'function') {
70+
throw new TypeError('predicate must be a function');
71+
}
72+
73+
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
74+
var thisArg = arguments[1];
75+
76+
// 5. Let k be 0.
77+
var k = 0;
78+
79+
// 6. Repeat, while k < len
80+
while (k < len) {
81+
// a. Let Pk be ! ToString(k).
82+
// b. Let kValue be ? Get(O, Pk).
83+
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
84+
// d. If testResult is true, return kValue.
85+
var kValue = o[k];
86+
if (predicate.call(thisArg, kValue, k, o)) {
87+
return kValue;
88+
}
89+
// e. Increase k by 1.
90+
k++;
91+
}
92+
93+
// 7. Return undefined.
94+
return undefined;
95+
}
96+
});
97+
}

0 commit comments

Comments
 (0)