Repository navigation
Expand file tree
/
Copy pathflow-methods.js
More file actions
108 lines (89 loc) · 3.66 KB
/
Copy pathflow-methods.js
File metadata and controls
108 lines (89 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 _);
});
});
});