-
Notifications
You must be signed in to change notification settings - Fork 6
/
filter.js
39 lines (37 loc) · 1.37 KB
/
filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @module object-loops/filter
*/
var forEach = require('./for-each')
/**
* Creates a new object with all entries that pass the test implemented by the provided function.
* @function module:object-loops/filter
* @param {object} [obj] - object to filter values, not accepted if being used directly on Object.prototype
* @param {filterCallback} callback - function to test each value in the object. return true to keep that entry, false otherwise.
* @param {*} [thisArg] - optional. context to bind to callback
* @returns {object} newly created object with filtered values
*/
module.exports = filter
function filter (obj, callback, thisArg) {
if (Array.isArray(obj)) {
return obj.filter(callback, thisArg)
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' must be a function')
}
var filteredObj = {}
forEach(obj, function (val, key, obj) {
var include = callback.call(thisArg, val, key, obj)
if (include) {
filteredObj[key] = val
}
})
return filteredObj
}
/**
* This callback type is called `filterCallback` and is displayed as a global symbol.
* @callback filterCallback
* @param {*} val - value for key
* @param {string} key - object key (used in current iteration)
* @param {object} obj - object which values are being iterated
* @returns {boolean} include - return true to keep that entry, false otherwise
*/