Skip to content

Commit

Permalink
♻️ Toleration falsey args in flow fix #202
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Jan 5, 2018
1 parent 3382109 commit c5cc845
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/immutadot/src/flow/flow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { flatten } from 'util/array'
import { isNil } from 'util/lang'

/**
* A function successively applying a list of functions.
Expand All @@ -11,14 +12,15 @@ import { flatten } from 'util/array'

/**
* Successively calls <code>fns</code>.<br/>
* Each function is called with the result of the previous one.
* Each function is called with the result of the previous one.<br/>
* Falsey functions (<code>null</code>, <code>undefined</code> and <code>false</code>) are tolerated and will be skipped.
* @memberof flow
* @param {...(function|Array<function>)} args The functions to apply
* @returns {flow.flowFunction} A function successively calling <code>fns</code>
* @since 1.0.0
*/
function flow(...args) {
const fns = flatten(args)
const fns = flatten(args).filter(fn => !isNil(fn) && fn !== false)
return pObj => {
const [result] = fns.reduce(
([obj, appliedPaths], fn) => [
Expand Down
33 changes: 33 additions & 0 deletions packages/immutadot/src/flow/flow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,37 @@ describe('flow.flow', () => {
return output
}, object, 'nested1.prop2', 'nested2.prop3', 'nested2.prop4')
})

it('should skip falsey functions', () => {
immutaTest((input, path) => {
const output = flow(
null,
set(path, 'bar'),
undefined,
false,
[
null,
undefined,
false,
],
)(input)
expect(output).toEqual({
nested: { prop: 'bar' },
other: {},
})
return output
}, {
nested: { prop: 'foo' },
other: {},
}, 'nested.prop')
})

it('should do nothing if empty flow', () => {
const input = {
nested: { prop: 'foo' },
other: {},
}
const output = flow()(input)
expect(output).toBe(input)
})
})

0 comments on commit c5cc845

Please sign in to comment.