Skip to content

Commit

Permalink
✨ Add object.pickBy (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin authored and nlepage committed Jun 20, 2017
1 parent 8c0781a commit 049c528
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/object/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assign from './assign'
import mapValues from './mapValues'
import pickBy from './pickBy'

/**
* Array functions.
Expand All @@ -9,4 +10,5 @@ import mapValues from './mapValues'
export {
assign,
mapValues,
pickBy,
}
22 changes: 22 additions & 0 deletions src/object/pickBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { lodashFpConvertOptions } from '../core/consts'
import pickBy from 'lodash/fp/pickBy'
import update from '../core/update'

const rawPickBy = pickBy.convert(lodashFpConvertOptions)

/**
* Creates an object composed of the object properties predicate returns truthy
* for. The predicate is invoked with two arguments: (value, key).
* @function pickBy
* @memberof object
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
* @return {Object} Returns the updated object.
* @example pickBy({ nested: { a: 1, b: 2, c: 3, d: 4 } }, 'nested', v => v < 3) // => { nested: { a: 1, b: 2 } }
* @see {@link core.update|update} for more information.
* @see {@link https://lodash.com/docs#pickBy|lodash.pickBy} for more information.
* @see {@link https://lodash.com/docs#identity|lodash.identity} for more information.
* @since 0.1.12
*/
export default update(rawPickBy)
29 changes: 29 additions & 0 deletions src/object/pickBy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-env node, mocha */
import { expect } from 'chai'
import pickBy from './pickBy'

describe('PickBy', () => {

const objectOne = {
nested: {
a: 1,
b: 2,
c: 3,
d: 4,
},
}
const output = {
nested: {
a: 1,
b: 2,
},
}

it('should pick all matching props by value', () => {
expect(pickBy(objectOne, 'nested', v => v < 3)).to.be.deep.equal(output)
})

it('should pick all matching props by key', () => {
expect(pickBy(objectOne, 'nested', (v, k) => k < 'c')).to.be.deep.equal(output)
})
})

0 comments on commit 049c528

Please sign in to comment.