Skip to content

Commit

Permalink
New function: deepSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
sadasant committed Feb 25, 2017
1 parent 013c219 commit 5600386
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ export const quote = _.partial(wrap, ['"', '"'])
export const parens = _.partial(wrap, ['(', ')'])

// Collection
// --------
// ----------
export const flowMap = (...fns) => _.map(_.flow(...fns))

// Trees
// -----
// deepSearch recursively finds all the values of the matching keys in an object
// It's useful to apply transformations to all the found nodes
const dSearch = (key, obj) => _.reduce((a, e) => a.concat(_.get(key, e), dSearch(key, e)), [], _.values(obj))
export const deepSearch = _.flow(dSearch, _.compact)

// Misc
// ----
export const testRegex = regex => regex.test.bind(regex)
52 changes: 52 additions & 0 deletions test/trees.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import chai from 'chai'
import * as f from '../src/'
import _ from 'lodash/fp'
chai.expect()
const expect = chai.expect

describe('Tree Functions', () => {
it('deepSearch', () => {
const obj = {
a: {
match: {
id: 1
},
b: {
match: {
id: 2
},
c: {
match: {
id: 3
}
}
}
}
}

_.map(e => { // eslint-disable-line lodash-fp/no-unused-result
e.matched = true
}, f.deepSearch('match', obj))

expect(obj).to.eql({
a: {
match: {
id: 1,
matched: true
},
b: {
match: {
id: 2,
matched: true
},
c: {
match: {
id: 3,
matched: true
}
}
}
}
})
})
})

0 comments on commit 5600386

Please sign in to comment.