Skip to content

Commit 97c26eb

Browse files
author
Eric Wendelin
committed
Add Function.bind, Array.map, and Array.filter polyfills for legacy browsers.
1 parent 814a723 commit 97c26eb

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

error-stack-parser.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,91 @@
1111
}
1212
}(this, function () {
1313
'use strict';
14+
15+
// ES5 Polyfills
16+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
17+
if (!Function.prototype.bind) {
18+
Function.prototype.bind = function (oThis) {
19+
if (typeof this !== 'function') {
20+
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
21+
}
22+
23+
var aArgs = Array.prototype.slice.call(arguments, 1);
24+
var fToBind = this;
25+
var noOp = function () {
26+
};
27+
var fBound = function () {
28+
return fToBind.apply(this instanceof noOp && oThis ? this : oThis,
29+
aArgs.concat(Array.prototype.slice.call(arguments)));
30+
};
31+
32+
noOp.prototype = this.prototype;
33+
fBound.prototype = new noOp();
34+
35+
return fBound;
36+
};
37+
}
38+
39+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
40+
if (!Array.prototype.map) {
41+
Array.prototype.map = function(callback, thisArg) {
42+
if (this === void 0 || this === null) {
43+
throw new TypeError("this is null or not defined");
44+
}
45+
var O = Object(this);
46+
var len = O.length >>> 0;
47+
if (typeof callback !== "function") {
48+
throw new TypeError(callback + " is not a function");
49+
}
50+
if (arguments.length > 1) {
51+
var T = thisArg;
52+
}
53+
54+
var A = new Array(len);
55+
var k = 0;
56+
57+
while (k < len) {
58+
var kValue, mappedValue;
59+
if (k in O) {
60+
kValue = O[k];
61+
mappedValue = callback.call(T, kValue, k, O);
62+
A[k] = mappedValue;
63+
}
64+
k++;
65+
}
66+
67+
return A;
68+
};
69+
}
70+
71+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
72+
if (!Array.prototype.filter) {
73+
Array.prototype.filter = function(callback/*, thisArg*/) {
74+
if (this === void 0 || this === null) {
75+
throw new TypeError("this is null or not defined");
76+
}
77+
78+
var t = Object(this);
79+
var len = t.length >>> 0;
80+
if (typeof callback !== "function") {
81+
throw new TypeError(callback + " is not a function");
82+
}
83+
84+
var res = [];
85+
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
86+
for (var i = 0; i < len; i++) {
87+
if (i in t) {
88+
var val = t[i];
89+
if (callback.call(thisArg, val, i, t)) {
90+
res.push(val);
91+
}
92+
}
93+
}
94+
95+
return res;
96+
};
97+
}
98+
1499
return function ErrorStackParser() {
15100
this.firefoxSafariStackEntryRegExp = /\S+\:\d+/;
16101
this.chromeIEStackEntryRegExp = /\s+at /;
@@ -71,7 +156,7 @@
71156

72157
this.parseOpera = function parseOpera(e) {
73158
if (!e.stacktrace || (e.message.indexOf('\n') > -1 &&
74-
e.message.split('\n').length > e.stacktrace.split('\n').length)) {
159+
e.message.split('\n').length > e.stacktrace.split('\n').length)) {
75160
return this.parseOpera9(e);
76161
} else if (!e.stack) {
77162
return this.parseOpera10a(e);

0 commit comments

Comments
 (0)