-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(object): function to shallow exclude object keys
Function similar to array.filter that retains the input key if the test function passes
1 parent
9d46a3d
commit 1c4e1d3
Showing
4 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module lib/object/filter | ||
* @author Eric Satterwhite | ||
**/ | ||
|
||
const typeOf = require('../type-of') | ||
module.exports = filter | ||
|
||
function filter(obj, fn) { | ||
const out = Object.create(null) | ||
if (typeOf(obj) !== 'object') return out | ||
for (const key of Object.keys(obj)) { | ||
if (fn(key)) out[key] = obj[key] | ||
} | ||
return out | ||
} | ||
|
||
/** | ||
* similar to array.filter, removes keys from an input object | ||
* @function module:lib/object/filter | ||
* @param {Object} obj The object to filter | ||
* @param {Function} fn A test function. return a truthy value to keep the key | ||
* @example | ||
* object.filter({two: 2, three: 3}, (key) =>{ | ||
* return key.match(/ee$/) | ||
* }) {three: 3} | ||
**/ | ||
|
This file contains 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
This file contains 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