Skip to content

Commit

Permalink
feat(object): function to shallow exclude object keys
Browse files Browse the repository at this point in the history
Function similar to array.filter that retains the input key if the test
function passes
esatterwhite committed May 3, 2021

Verified

This commit was signed with the committer’s verified signature.
987Nabil Nabil Abdel-Hafeez
1 parent 9d46a3d commit 1c4e1d3
Showing 4 changed files with 94 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -170,6 +170,34 @@ const value = object.set(obj, 'four.five', 6)
// {one: { two: three: 3 }, four: {five: 6}}
```

#### `filter`(obj: [Object][], test: [Function][]): [Object][]

Similar to array.filter, removes keys from an input object that do not pass the
`test` function

`NOTE`: This function returns a `null` object - `Object.create(null)` which does not
inherit from Object.prototype

**Arguments**

* `obj` ([Object][]) - The object to introspect
* `test` ([Function][]) - The function to be used to reject keys from the input object.
If this function returns a `truthy` value, the key will be included in the final output. If `falsey`
it will be excluded

##### Example

```javascript
const {object} = require('@answerbook/stdlib')
const obj = {one: { two: three: 3 } }

object.filter({two: 2, three: 3}, (key) => {
return key.match(/ee$/)
}) // {three: 3}
```

**returns** [Object][] An object containing only the keys which passed the test function.
The return object will have a `null` prototype.

### string

@@ -305,5 +333,6 @@ typeOf(new Set()) // set
[Array]: https://mdn.io/array
[String]: https://mdn.io/string
[Object]: https://mdn.io/object
[Function]: https://mdn.io/function
[Generator]: https://mdn.io/generator
[itertools]: https://docs.python.org/3.7/library/itertools.html
30 changes: 30 additions & 0 deletions lib/object/filter.js
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}
**/

4 changes: 3 additions & 1 deletion lib/object/index.js
Original file line number Diff line number Diff line change
@@ -6,10 +6,12 @@
* @borrows module:lib/object/get-property as get
* @borrows module:lib/object/set-property as set
* @borrows module:lib/object/has-property as has
* @borrows module:lib/object/filter as filter
**/

module.exports = {
get: require('./get-property.js')
filter: require('./filter.js')
, get: require('./get-property.js')
, has: require('./has-property.js')
, set: require('./set-property.js')
}
34 changes: 32 additions & 2 deletions test/unit/object.js
Original file line number Diff line number Diff line change
@@ -6,9 +6,10 @@ const object = require('../../lib/object/index.js')
test('object', async (t) => {
t.test('Exports as expected', async (t) => {
const entries = Object.entries(object)
t.equal(entries.length, 3, 'function count')
t.equal(entries.length, 4, 'function count')
t.match(object, {
get: Function
filter: Function
, get: Function
, set: Function
, has: Function
}, 'function names')
@@ -102,4 +103,33 @@ test('object', async (t) => {
}, 'subsequent calls override previous values')
}
})

t.test('object.filter', async (t) => {
{
const input = 100
const output = object.filter(input, (key) => {
return !key.match(/o/ig)
})

t.deepEqual(output, {}, 'number returns empty object')
}

{
const input = null
const output = object.filter(input, (key) => {
return !key.match(/o/ig)
})

t.deepEqual(output, {}, 'null returns empty object')
}

{
const input = {one: 1, two: 2, three: 3, four: 4}
const output = object.filter(input, (key) => {
return !key.match(/o/ig)
})

t.deepEqual(output, {three: 3}, 'filters out non matching keys')
}
})
}).catch(threw)

0 comments on commit 1c4e1d3

Please sign in to comment.