From c5cc845c9b11bd6f3361f8fe4c7bb6c7addfdb1c Mon Sep 17 00:00:00 2001
From: nlepage <19571875+nlepage@users.noreply.github.com>
Date: Fri, 5 Jan 2018 14:08:30 +0100
Subject: [PATCH] :recycle: Toleration falsey args in flow fix #202
---
packages/immutadot/src/flow/flow.js | 6 +++--
packages/immutadot/src/flow/flow.spec.js | 33 ++++++++++++++++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/packages/immutadot/src/flow/flow.js b/packages/immutadot/src/flow/flow.js
index b8651254..f5f68161 100644
--- a/packages/immutadot/src/flow/flow.js
+++ b/packages/immutadot/src/flow/flow.js
@@ -1,4 +1,5 @@
import { flatten } from 'util/array'
+import { isNil } from 'util/lang'
/**
* A function successively applying a list of functions.
@@ -11,14 +12,15 @@ import { flatten } from 'util/array'
/**
* Successively calls fns
.
- * Each function is called with the result of the previous one.
+ * Each function is called with the result of the previous one.
+ * Falsey functions (null
, undefined
and false
) are tolerated and will be skipped.
* @memberof flow
* @param {...(function|Array)} args The functions to apply
* @returns {flow.flowFunction} A function successively calling fns
* @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) => [
diff --git a/packages/immutadot/src/flow/flow.spec.js b/packages/immutadot/src/flow/flow.spec.js
index 6cfc76bf..a05e943b 100644
--- a/packages/immutadot/src/flow/flow.spec.js
+++ b/packages/immutadot/src/flow/flow.spec.js
@@ -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)
+ })
})