forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.js
More file actions
74 lines (61 loc) · 2.31 KB
/
chain.js
File metadata and controls
74 lines (61 loc) · 2.31 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
import assert from 'assert';
import lodashStable from 'lodash';
import { square } from './utils.js';
import chain from '../chain.js';
describe('chain', function() {
it('should return a wrapped value', function() {
var actual = chain({ 'a': 0 });
assert.ok(actual instanceof _);
});
it('should return existing wrapped values', function() {
var wrapped = _({ 'a': 0 });
assert.strictEqual(chain(wrapped), wrapped);
assert.strictEqual(wrapped.chain(), wrapped);
});
it('should enable chaining for methods that return unwrapped values', function() {
var array = ['c', 'b', 'a'];
assert.ok(chain(array).head() instanceof _);
assert.ok(_(array).chain().head() instanceof _);
assert.ok(chain(array).isArray() instanceof _);
assert.ok(_(array).chain().isArray() instanceof _);
assert.ok(chain(array).sortBy().head() instanceof _);
assert.ok(_(array).chain().sortBy().head() instanceof _);
});
it('should chain multiple methods', function() {
lodashStable.times(2, function(index) {
var array = ['one two three four', 'five six seven eight', 'nine ten eleven twelve'],
expected = { ' ': 9, 'e': 14, 'f': 2, 'g': 1, 'h': 2, 'i': 4, 'l': 2, 'n': 6, 'o': 3, 'r': 2, 's': 2, 't': 5, 'u': 1, 'v': 4, 'w': 2, 'x': 1 },
wrapped = index ? _(array).chain() : chain(array);
var actual = wrapped
.chain()
.map(function(value) { return value.split(''); })
.flatten()
.reduce(function(object, chr) {
object[chr] || (object[chr] = 0);
object[chr]++;
return object;
}, {})
.value();
assert.deepStrictEqual(actual, expected);
array = [1, 2, 3, 4, 5, 6];
wrapped = index ? _(array).chain() : chain(array);
actual = wrapped
.chain()
.filter(function(n) { return n % 2 != 0; })
.reject(function(n) { return n % 3 == 0; })
.sortBy(function(n) { return -n; })
.value();
assert.deepStrictEqual(actual, [5, 1]);
array = [3, 4];
wrapped = index ? _(array).chain() : chain(array);
actual = wrapped
.reverse()
.concat([2, 1])
.unshift(5)
.tap(function(value) { value.pop(); })
.map(square)
.value();
assert.deepStrictEqual(actual, [25, 16, 9, 4]);
});
});
});