Created
November 14, 2016 07:20
-
-
Save zheeeng/b675822497abfc20f8d00c6710b2fb5d to your computer and use it in GitHub Desktop.
filter function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isArrayLike = function (arg) { | |
return arg && typeof arg.length === 'number' && arg.length > -1 && arg.length % 1 === 0 && typeof arg !== 'function' | |
} | |
var filter = (function () { | |
if (Array.prototype.filter) { | |
return function (arrayLikeList, filter, thisArg) { | |
return Array.prototype.filter.call(arrayLikeList, filter, thisArg) | |
} | |
} else { // Array.prototype.filter fallback | |
return function (arrayLikeList, filter, thisArg) { | |
if (!isArrayLike(arrayLikeList)) throw new TypeError('Parameter arrayLikeList isn\'t array like') | |
if (typeof filter !== 'function') throw new TypeError('Parameter filter is not a function') | |
if (thisArg === void 0) thisArg = this | |
var o = Object(arrayLikeList) | |
var len = o.length >>> 0 | |
var res = [] | |
var k = 0 | |
while (k < len) { | |
if (k in o && filter.call(thisArg, o[k], k, o)) res.push(o[k]) | |
k++ | |
} | |
return res | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment