From 0e9d44eedbfe40423da2bba8ad5eb4d7d86e3682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Wed, 28 Aug 2019 20:06:45 -0300 Subject: [PATCH] Fix flow and flowRight parameter handling (#4445) * Enable flow and flowRight tests * Remove flow and flowRight tests for default value and shortcut fusion * Use native rest parameters / spread operator in flow and flowRight * Fix syntax of flow and flowRight examples --- flow.js | 6 ++- flowRight.js | 8 +-- test/flow-methods.js | 108 -------------------------------------- test/flow-methods.test.js | 53 +++++++++++++++++++ 4 files changed, 62 insertions(+), 113 deletions(-) delete mode 100644 test/flow-methods.js create mode 100644 test/flow-methods.test.js diff --git a/flow.js b/flow.js index 0016789bd..a8ca694cd 100644 --- a/flow.js +++ b/flow.js @@ -10,15 +10,17 @@ * @see flowRight * @example * + * import add from 'lodash/add' + * * function square(n) { * return n * n * } * - * const addSquare = flow([add, square]) + * const addSquare = flow(add, square) * addSquare(1, 2) * // => 9 */ -function flow(funcs) { +function flow(...funcs) { const length = funcs ? funcs.length : 0 let index = length while (index--) { diff --git a/flowRight.js b/flowRight.js index 6375ba2ca..7e4e082a1 100644 --- a/flowRight.js +++ b/flowRight.js @@ -11,16 +11,18 @@ import flow from './flow.js' * @see flow * @example * + * import add from 'lodash/add' + * * function square(n) { * return n * n * } * - * const addSquare = flowRight([square, add]) + * const addSquare = flowRight(square, add) * addSquare(1, 2) * // => 9 */ -function flowRight(funcs) { - return flow(funcs.reverse()) +function flowRight(...funcs) { + return flow(...funcs.reverse()) } export default flowRight diff --git a/test/flow-methods.js b/test/flow-methods.js deleted file mode 100644 index 916a4e396..000000000 --- a/test/flow-methods.js +++ /dev/null @@ -1,108 +0,0 @@ -import assert from 'assert'; -import lodashStable from 'lodash'; -import { _, add, square, noop, identity, LARGE_ARRAY_SIZE, isEven, isNpm } from './utils.js'; -import times from '../times.js'; -import curry from '../curry.js'; -import head from '../head.js'; -import filter from '../filter.js'; -import rearg from '../rearg.js'; -import ary from '../ary.js'; -import map from '../map.js'; -import take from '../take.js'; -import compact from '../compact.js'; -import uniq from '../uniq.js'; - -describe('flow methods', function() { - lodashStable.each(['flow', 'flowRight'], function(methodName) { - var func = _[methodName], - isFlow = methodName == 'flow'; - - it('`_.' + methodName + '` should supply each function with the return value of the previous', function() { - var fixed = function(n) { return n.toFixed(1); }, - combined = isFlow ? func(add, square, fixed) : func(fixed, square, add); - - assert.strictEqual(combined(1, 2), '9.0'); - }); - - it('`_.' + methodName + '` should return a new function', function() { - assert.notStrictEqual(func(noop), noop); - }); - - it('`_.' + methodName + '` should return an identity function when no arguments are given', function() { - times(2, function(index) { - try { - var combined = index ? func([]) : func(); - assert.strictEqual(combined('a'), 'a'); - } catch (e) { - assert.ok(false, e.message); - } - assert.strictEqual(combined.length, 0); - assert.notStrictEqual(combined, identity); - }); - }); - - it('`_.' + methodName + '` should work with a curried function and `_.head`', function() { - var curried = curry(identity); - - var combined = isFlow - ? func(head, curried) - : func(curried, head); - - assert.strictEqual(combined([1]), 1); - }); - - it('`_.' + methodName + '` should support shortcut fusion', function() { - var filterCount, - mapCount, - array = lodashStable.range(LARGE_ARRAY_SIZE), - iteratee = function(value) { mapCount++; return square(value); }, - predicate = function(value) { filterCount++; return isEven(value); }; - - lodashStable.times(2, function(index) { - var filter1 = filter, - filter2 = curry(rearg(ary(filter, 2), 1, 0), 2), - filter3 = (filter = index ? filter2 : filter1, filter2(predicate)); - - var map1 = map, - map2 = curry(rearg(ary(map, 2), 1, 0), 2), - map3 = (map = index ? map2 : map1, map2(iteratee)); - - var take1 = take, - take2 = curry(rearg(ary(take, 2), 1, 0), 2), - take3 = (take = index ? take2 : take1, take2(2)); - - var combined = isFlow - ? func(map3, filter3, compact, take3) - : func(take3, compact, filter3, map3); - - filterCount = mapCount = 0; - assert.deepStrictEqual(combined(array), [4, 16]); - - if (!isNpm && WeakMap && WeakMap.name) { - assert.strictEqual(filterCount, 5, 'filterCount'); - assert.strictEqual(mapCount, 5, 'mapCount'); - } - filter = filter1; - map = map1; - take = take1; - }); - }); - - it('`_.' + methodName + '` should work with curried functions with placeholders', function() { - var curried = curry(ary(map, 2), 2), - getProp = curried(curried.placeholder, 'a'), - objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 1 }]; - - var combined = isFlow - ? func(getProp, uniq) - : func(uniq, getProp); - - assert.deepStrictEqual(combined(objects), [1, 2]); - }); - - it('`_.' + methodName + '` should return a wrapped value when chaining', function() { - var wrapped = _(noop)[methodName](); - assert.ok(wrapped instanceof _); - }); - }); -}); diff --git a/test/flow-methods.test.js b/test/flow-methods.test.js new file mode 100644 index 000000000..c1706ab1e --- /dev/null +++ b/test/flow-methods.test.js @@ -0,0 +1,53 @@ +import assert from 'assert'; +import lodashStable from 'lodash'; +import { add, square, noop, identity } from './utils.js'; +import head from '../head.js'; +import map from '../map.js'; +import uniq from '../uniq.js'; +import flow from '../flow.js'; +import flowRight from '../flowRight.js'; + +const methods = { + flow, + flowRight +} + +describe('flow methods', function() { + lodashStable.each(['flow', 'flowRight'], function(methodName) { + var func = methods[methodName], + isFlow = methodName == 'flow'; + + it('`_.' + methodName + '` should supply each function with the return value of the previous', function() { + var fixed = function(n) { return n.toFixed(1); }, + combined = isFlow ? func(add, square, fixed) : func(fixed, square, add); + + assert.strictEqual(combined(1, 2), '9.0'); + }); + + it('`_.' + methodName + '` should return a new function', function() { + assert.notStrictEqual(func(noop), noop); + }); + + it('`_.' + methodName + '` should work with a curried function and `_.head`', function() { + var curried = lodashStable.curry(identity); + + var combined = isFlow + ? func(head, curried) + : func(curried, head); + + assert.strictEqual(combined([1]), 1); + }); + + it('`_.' + methodName + '` should work with curried functions with placeholders', function() { + var curried = lodashStable.curry(lodashStable.ary(map, 2), 2), + getProp = curried(curried.placeholder, (value) => value.a), + objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 1 }]; + + var combined = isFlow + ? func(getProp, uniq) + : func(uniq, getProp); + + assert.deepStrictEqual(combined(objects), [1, 2]); + }); + }); +});