forked from angus-c/just
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (122 loc) · 2.74 KB
/
Copy pathindex.js
File metadata and controls
143 lines (122 loc) · 2.74 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var test = require('../util/test')(__filename);
var reduce = require('../../packages/object-reduce');
var compare = require('../../packages/collection-compare');
var noop = function() {};
noop.toString = function() {
return 'noop function';
};
var obj = {a: 3, b: 5, c: 9, d: null, e: noop};
test('initialValue', function(t) {
t.plan(2);
var expectedKeys = Object.keys(obj);
function getArgsInitialIndex(idx) {
return function() {
if (arguments[3] == idx) {
return [].slice.call(arguments);
}
return arguments[0];
};
}
// Without initialValue
var result1 = reduce(obj, getArgsInitialIndex(1));
t.ok(compare(result1, [3, 'b', 5, 1, expectedKeys]));
// With initialValue
var result2 = reduce(obj, getArgsInitialIndex(0), []);
t.ok(compare(result2, [[], 'a', 3, 0, expectedKeys]));
t.end();
});
test('use value', function(t) {
t.plan(3);
var result1 = reduce(
obj,
function(target, key, value) {
target.push(value);
return target;
},
[]
);
t.ok(compare(result1, [3, 5, 9, null, noop]));
var result2 = reduce(
obj,
function(target, key, value) {
target += Number(value) || 0;
return target;
},
0
);
t.ok(compare(result2, 17));
var result3 = reduce(obj, function(target, key, value) {
target += Number(value) || 0;
return target;
});
t.ok(compare(result3, 17));
t.end();
});
test('use key', function(t) {
t.plan(3);
var result1 = reduce(
obj,
function(target, key, value) {
target.push(key);
return target;
},
[]
);
t.ok(compare(result1, ['a', 'b', 'c', 'd', 'e']));
var result2 = reduce(
obj,
function(target, key, value, index) {
target[index] = key;
return target;
},
{}
);
t.ok(compare(result2, {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}));
var result3 = reduce(obj, function(target, key, value) {
target += key;
return target;
});
t.ok(compare(result3, '3bcde'));
t.end();
});
test('use key and value', function(t) {
t.plan(2);
var expectedResult1 = ['a is 3', 'b is 5', 'c is 9', 'd is null', 'e is noop function'];
var result1 = reduce(
obj,
function(target, key, value) {
target.push(key + ' is ' + value);
return target;
},
[]
);
t.ok(compare(result1, expectedResult1));
var result2 = reduce(
obj,
function(target, key, value) {
target[value] = key;
return target;
},
{}
);
t.ok(
compare(result2, {
3: 'a',
5: 'b',
9: 'c',
null: 'd',
'noop function': 'e',
})
);
t.end();
});
test('invalid usage', function(t) {
t.plan(2);
t.throws(function() {
reduce(obj);
});
t.throws(function() {
reduce({}, noop);
});
t.end();
});