UNPKG

519 BJavaScriptView Raw
1var forEach = require('./each')
2
3module.exports = function filter (each, fn, _isArray) {
4 var isArray = typeof _isArray !== 'undefined' ? _isArray : Array.isArray(each)
5 var result
6
7 if (typeof each !== 'function') each = forEach.bind(this, each)
8
9 if (isArray) {
10 result = []
11 each(function (val, key) {
12 if (fn.apply(this, arguments)) result.push(val)
13 })
14 } else {
15 result = {}
16 each(function (val, key) {
17 if (fn.apply(this, arguments)) result[key] = val
18 })
19 }
20
21 return result
22}