extendscript-es5-shim
Version:
a collection of useful es5-shims for Extendscript
39 lines (31 loc) • 1.09 kB
JavaScript
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
*/
if (!Array.prototype.filter) {
Array.prototype.filter = function(callback, thisArg) {
if (this === void 0 || this === null) {
throw new TypeError('Array.prototype.filter called on null or undefined');
}
var t = Object(this);
var len = t.length >>> 0;
if (callback.__class__ !== 'Function') {
throw new TypeError(callback + ' is not a function');
}
var res = [];
var T = (arguments.length > 1) ? thisArg : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];
// NOTE: Technically this should Object.defineProperty at
// the next index, as push can be affected by
// properties on Object.prototype and Array.prototype.
// But that method's new, and collisions should be
// rare, so use the more-compatible alternative.
if (callback.call(T, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}