forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvokeMap.js
105 lines (78 loc) · 3.14 KB
/
invokeMap.js
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
import assert from 'assert';
import lodashStable from 'lodash';
import { slice, stubOne } from './utils.js';
import invokeMap from '../invokeMap.js';
describe('invokeMap', function() {
it('should invoke a methods on each element of `collection`', function() {
var array = ['a', 'b', 'c'],
actual = invokeMap(array, 'toUpperCase');
assert.deepStrictEqual(actual, ['A', 'B', 'C']);
});
it('should support invoking with arguments', function() {
var array = [function() { return slice.call(arguments); }],
actual = invokeMap(array, 'call', null, 'a', 'b', 'c');
assert.deepStrictEqual(actual, [['a', 'b', 'c']]);
});
it('should work with a function for `methodName`', function() {
var array = ['a', 'b', 'c'];
var actual = invokeMap(array, function(left, right) {
return left + this.toUpperCase() + right;
}, '(', ')');
assert.deepStrictEqual(actual, ['(A)', '(B)', '(C)']);
});
it('should work with an object for `collection`', function() {
var object = { 'a': 1, 'b': 2, 'c': 3 },
actual = invokeMap(object, 'toFixed', 1);
assert.deepStrictEqual(actual, ['1.0', '2.0', '3.0']);
});
it('should treat number values for `collection` as empty', function() {
assert.deepStrictEqual(invokeMap(1), []);
});
it('should not error on nullish elements', function() {
var array = ['a', null, undefined, 'd'];
try {
var actual = invokeMap(array, 'toUpperCase');
} catch (e) {}
assert.deepStrictEqual(actual, ['A', undefined, undefined, 'D']);
});
it('should not error on elements with missing properties', function() {
var objects = lodashStable.map([null, undefined, stubOne], function(value) {
return { 'a': value };
});
var expected = lodashStable.map(objects, function(object) {
return object.a ? object.a() : undefined;
});
try {
var actual = invokeMap(objects, 'a');
} catch (e) {}
assert.deepStrictEqual(actual, expected);
});
it('should invoke deep property methods with the correct `this` binding', function() {
var object = { 'a': { 'b': function() { return this.c; }, 'c': 1 } };
lodashStable.each(['a.b', ['a', 'b']], function(path) {
assert.deepStrictEqual(invokeMap([object], path), [1]);
});
});
it('should return a wrapped value when chaining', function() {
var array = ['a', 'b', 'c'],
wrapped = _(array),
actual = wrapped.invokeMap('toUpperCase');
assert.ok(actual instanceof _);
assert.deepEqual(actual.valueOf(), ['A', 'B', 'C']);
actual = wrapped.invokeMap(function(left, right) {
return left + this.toUpperCase() + right;
}, '(', ')');
assert.ok(actual instanceof _);
assert.deepEqual(actual.valueOf(), ['(A)', '(B)', '(C)']);
});
it('should support shortcut fusion', function() {
var count = 0,
method = function() { count++; return this.index; };
var array = lodashStable.times(LARGE_ARRAY_SIZE, function(index) {
return { 'index': index, 'method': method };
});
var actual = _(array).invokeMap('method').take(1).value();
assert.strictEqual(count, 1);
assert.deepEqual(actual, [0]);
});
});