Skip to content

Commit 0e9d44e

Browse files
blikblumjdalton
authored andcommitted
Fix flow and flowRight parameter handling (lodash#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
1 parent e51a424 commit 0e9d44e

File tree

4 files changed

+62
-113
lines changed

4 files changed

+62
-113
lines changed

flow.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
* @see flowRight
1111
* @example
1212
*
13+
* import add from 'lodash/add'
14+
*
1315
* function square(n) {
1416
* return n * n
1517
* }
1618
*
17-
* const addSquare = flow([add, square])
19+
* const addSquare = flow(add, square)
1820
* addSquare(1, 2)
1921
* // => 9
2022
*/
21-
function flow(funcs) {
23+
function flow(...funcs) {
2224
const length = funcs ? funcs.length : 0
2325
let index = length
2426
while (index--) {

flowRight.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import flow from './flow.js'
1111
* @see flow
1212
* @example
1313
*
14+
* import add from 'lodash/add'
15+
*
1416
* function square(n) {
1517
* return n * n
1618
* }
1719
*
18-
* const addSquare = flowRight([square, add])
20+
* const addSquare = flowRight(square, add)
1921
* addSquare(1, 2)
2022
* // => 9
2123
*/
22-
function flowRight(funcs) {
23-
return flow(funcs.reverse())
24+
function flowRight(...funcs) {
25+
return flow(...funcs.reverse())
2426
}
2527

2628
export default flowRight

test/flow-methods.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

test/flow-methods.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import assert from 'assert';
2+
import lodashStable from 'lodash';
3+
import { add, square, noop, identity } from './utils.js';
4+
import head from '../head.js';
5+
import map from '../map.js';
6+
import uniq from '../uniq.js';
7+
import flow from '../flow.js';
8+
import flowRight from '../flowRight.js';
9+
10+
const methods = {
11+
flow,
12+
flowRight
13+
}
14+
15+
describe('flow methods', function() {
16+
lodashStable.each(['flow', 'flowRight'], function(methodName) {
17+
var func = methods[methodName],
18+
isFlow = methodName == 'flow';
19+
20+
it('`_.' + methodName + '` should supply each function with the return value of the previous', function() {
21+
var fixed = function(n) { return n.toFixed(1); },
22+
combined = isFlow ? func(add, square, fixed) : func(fixed, square, add);
23+
24+
assert.strictEqual(combined(1, 2), '9.0');
25+
});
26+
27+
it('`_.' + methodName + '` should return a new function', function() {
28+
assert.notStrictEqual(func(noop), noop);
29+
});
30+
31+
it('`_.' + methodName + '` should work with a curried function and `_.head`', function() {
32+
var curried = lodashStable.curry(identity);
33+
34+
var combined = isFlow
35+
? func(head, curried)
36+
: func(curried, head);
37+
38+
assert.strictEqual(combined([1]), 1);
39+
});
40+
41+
it('`_.' + methodName + '` should work with curried functions with placeholders', function() {
42+
var curried = lodashStable.curry(lodashStable.ary(map, 2), 2),
43+
getProp = curried(curried.placeholder, (value) => value.a),
44+
objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 1 }];
45+
46+
var combined = isFlow
47+
? func(getProp, uniq)
48+
: func(uniq, getProp);
49+
50+
assert.deepStrictEqual(combined(objects), [1, 2]);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)