-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
array.js
229 lines (193 loc) · 7.47 KB
/
array.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*!
* template-helpers <https://github.com/jonschlinkert/template-helpers>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
var assert = require('assert');
var helpers = require('..')('array');
var template = require('lodash.template');
var context = {arr: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']};
var imports = {imports: helpers};
describe('isArray', function() {
it('should return true if the value is an array.', function() {
assert.equal(template('<%= isArray("foo") %>', imports)(), 'false');
assert.equal(template('<%= isArray(["foo"]) %>', imports)(), 'true');
});
});
describe('arrayify', function() {
it('should coerce a value to an array.', function() {
assert.equal(template('<%= isArray(arrayify("foo")) %>', imports)(), 'true');
assert.equal(template('<%= isArray(arrayify(["foo"])) %>', imports)(), 'true');
});
});
describe('first', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= first() %>', imports)(), '');
});
it('should return the first item in an array.', function() {
var fn = template('<%= first(foo) %>', imports);
assert.equal(fn({foo: ['a', 'b', 'c']}), 'a');
});
it('should return an array with the first two items in an array.', function() {
var fn = template('<%= first(foo, 2) %>', imports);
assert.deepEqual(fn({foo: ['a', 'b', 'c']}), ['a', 'b'].toString());
});
});
describe('last', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= last() %>', imports)(), '');
});
it('should return the last item in an array.', function() {
assert.equal(template('<%= last(arr) %>', imports)(context), 'h');
});
it('should return an array with the last two items in an array.', function() {
assert.deepEqual(template('<%= last(arr, 2) %>', imports)(context), ['g', 'h'].toString());
});
});
describe('before', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= before() %>', imports)(), '');
});
it('should return all of the items in an array before the given index.', function() {
var fn = template('<%= before(arr, 5) %>', imports);
assert.deepEqual(fn(context), ['a', 'b', 'c'].toString());
});
});
describe('after', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= after() %>', imports)(), '');
});
it('should return all of the items in an array after the given index.', function() {
var fn = template('<%= after(arr, 5) %>', imports);
assert.deepEqual(fn(context), ['f', 'g', 'h'].toString());
});
});
describe('join', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= join() %>', imports)(), '');
});
it('should return all items in an array joined by the default separator.', function() {
var fn = template('<%= join(arr) %>', imports);
assert.equal(fn(context), 'a, b, c, d, e, f, g, h');
});
it('should return all items in an array joined by the given separator.', function() {
var fn = template('<%= join(arr, " | ") %>', imports);
assert.equal(fn(context), 'a | b | c | d | e | f | g | h');
});
});
describe('each', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= each() %>', imports)(), '');
});
it('should iterate over items in the array and return new values.', function() {
var o = {};
o.double = function(str) {
return str + str;
};
var fn = template('<%= each(["a","b","c"], double) %>', imports);
assert.equal(fn(o), 'aabbcc');
});
it('should expose the given context to each item', function() {
var o = {ctx: {sep: ' '}};
o.double = function(str) {
return str + this.sep + str;
};
var fn = template('<%= each(["a","b","c"], double, ctx) %>', imports);
assert.equal(fn(o), 'a ab bc c');
});
});
describe('map', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= map() %>', imports)(), '');
});
it('should map the items in the array and return new values.', function() {
var o = {};
o.double = function(str) {
return str + str;
};
var fn = template('<%= map(["a","b","c"], double) %>', imports);
assert.equal(fn(o), 'aa,bb,cc');
});
});
describe('sort', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= sort() %>', imports)(), '');
});
it('should sort the items in an array.', function() {
var fn = template('<%= sort(["b", "c", "a"]) %>', imports);
assert.equal(fn(context), 'a,b,c');
});
it('should take a compare function.', function() {
var o = {};
o.compare = function(a, b) {
return b.localeCompare(a);
};
var fn = template('<%= sort(["b", "c", "a"], compare) %>', imports);
assert.equal(fn(o), 'c,b,a');
});
it('should sort based on object key:', function() {
var fn = template('<%= JSON.stringify(sort([{a: "zzz"}, {a: "aaa"}], "a")) %>', imports);
assert.equal(fn(), '[{"a":"aaa"},{"a":"zzz"}]');
});
});
describe('length', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= length() %>', imports)(), '');
});
it('should return zero when the value is not an array.', function() {
var fn = template('<%= length("foo") %>', imports);
assert.equal(fn(context), '0');
});
it('should return the length of an array.', function() {
var fn = template('<%= length(["b", "c", "a"]) %>', imports);
assert.equal(fn(context), '3');
});
});
describe('compact', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= compact() %>', imports)(), '');
});
it('should remove falsey values from an array.', function() {
var fn = template('<%= compact([null, "a", undefined, 0, false, "b", "c", ""]) %>', imports);
assert.equal(fn(context), 'a,b,c');
});
});
describe('difference', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= difference() %>', imports)(), '');
});
it('should return the difference from multiple arrays', function() {
var o = {};
o.a = ['a', 'b', 'c', 'd'];
o.b = ['b', 'c'];
o.c = ['x', 'y'];
assert.equal(template('<%= difference(a, b, c) %>', imports)(o), 'a,d');
assert.equal(template('<%= difference(a, b) %>', imports)(o), 'a,d');
assert.equal(template('<%= difference(a) %>', imports)(o), 'a,b,c,d');
});
});
describe('unique', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= unique() %>', imports)(), '');
});
it('should unique items from multiple arrays:', function() {
var fn = template('<%= unique(["a", "b", "c", "c"]) %>', imports);
assert.equal(fn(context), 'a,b,c');
});
});
describe('union', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= union() %>', imports)(), '');
});
it('should union items from multiple arrays:', function() {
var fn = template('<%= union(["a", "c"], ["b", "b"]) %>', imports);
assert.equal(fn(context), 'a,c,b');
});
it('should union items from multiple arrays:', function() {
var fn = template('<%= union(["a"], ["b"]) %>', imports);
assert.equal(fn(context), 'a,b');
});
});