Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lib_polyfill.js Array.prototype.filter #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update lib_polyfill.js
yysahler authored Dec 7, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit f7653c3179182793525e8abe9b1b4e419f0bdbca
49 changes: 10 additions & 39 deletions core/lib_polyfill.js
Original file line number Diff line number Diff line change
@@ -133,44 +133,15 @@
/************************* ARRAY *************************/


if (!Array.prototype.filter) {
Array.prototype.filter = function(func, thisArg) {
'use strict';
if (!((typeof func === 'Function' || typeof func === 'function') && this))
throw new TypeError();

var len = this.length >>> 0,
res = new Array(len), // preallocate array
t = this,
c = 0,
i = -1;

var kValue;
if (thisArg === undefined) {
while (++i !== len) {
// checks to see if the key was set
if (i in this) {
kValue = t[i]; // in case t is changed in callback
if (func(t[i], i, t)) {
res[c++] = kValue;
}
}
}
} else {
while (++i !== len) {
// checks to see if the key was set
if (i in this) {
kValue = t[i];
if (func.call(thisArg, t[i], i, t)) {
res[c++] = kValue;
}
}
}
}

res.length = c; // shrink down array to proper size
return res;
};
Array.prototype.filter = function(fn) {
var a = [];
for (var i = 0; i < this.length; i++) {
var n = fn(this[i]);
if (n) {
a.push(this[i]);
}
}
return a;
}


@@ -461,4 +432,4 @@
}());
}

</script>
</script>