forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter-methods.js
More file actions
100 lines (75 loc) · 3.38 KB
/
filter-methods.js
File metadata and controls
100 lines (75 loc) · 3.38 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
import assert from 'assert';
import lodashStable from 'lodash';
import { _, LARGE_ARRAY_SIZE, isEven, square } from './utils.js';
describe('filter methods', function() {
lodashStable.each(['filter', 'reject'], function(methodName) {
var array = [1, 2, 3, 4],
func = _[methodName],
isFilter = methodName == 'filter',
objects = [{ 'a': 0 }, { 'a': 1 }];
it('`_.' + methodName + '` should not modify the resulting value from within `predicate`', function() {
var actual = func([0], function(value, index, array) {
array[index] = 1;
return isFilter;
});
assert.deepStrictEqual(actual, [0]);
});
it('`_.' + methodName + '` should work with `_.property` shorthands', function() {
assert.deepStrictEqual(func(objects, 'a'), [objects[isFilter ? 1 : 0]]);
});
it('`_.' + methodName + '` should work with `_.matches` shorthands', function() {
assert.deepStrictEqual(func(objects, objects[1]), [objects[isFilter ? 1 : 0]]);
});
it('`_.' + methodName + '` should not modify wrapped values', function() {
var wrapped = _(array);
var actual = wrapped[methodName](function(n) {
return n < 3;
});
assert.deepEqual(actual.value(), isFilter ? [1, 2] : [3, 4]);
actual = wrapped[methodName](function(n) {
return n > 2;
});
assert.deepEqual(actual.value(), isFilter ? [3, 4] : [1, 2]);
});
it('`_.' + methodName + '` should work in a lazy sequence', function() {
var array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
predicate = function(value) { return isFilter ? isEven(value) : !isEven(value); };
var object = lodashStable.zipObject(lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
return ['key' + index, index];
}));
var actual = _(array).slice(1).map(square)[methodName](predicate).value();
assert.deepEqual(actual, _[methodName](lodashStable.map(array.slice(1), square), predicate));
actual = _(object).mapValues(square)[methodName](predicate).value();
assert.deepEqual(actual, _[methodName](lodashStable.mapValues(object, square), predicate));
});
it('`_.' + methodName + '` should provide correct `predicate` arguments in a lazy sequence', function() {
var args,
array = lodashStable.range(LARGE_ARRAY_SIZE + 1),
expected = [1, 0, lodashStable.map(array.slice(1), square)];
_(array).slice(1)[methodName](function(value, index, array) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, [1, 0, array.slice(1)]);
args = undefined;
_(array).slice(1).map(square)[methodName](function(value, index, array) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, expected);
args = undefined;
_(array).slice(1).map(square)[methodName](function(value, index) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, expected);
args = undefined;
_(array).slice(1).map(square)[methodName](function(value) {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, [1]);
args = undefined;
_(array).slice(1).map(square)[methodName](function() {
args || (args = slice.call(arguments));
}).value();
assert.deepEqual(args, expected);
});
});
});