array.builders.js
8.9 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
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
$(document).ready(function() {
module("underscore.array.builders");
test("cat", function() {
// no args
deepEqual(_.cat(), [], 'should return an empty array when given no args');
// one arg
deepEqual(_.cat([]), [], 'should concatenate one empty array');
deepEqual(_.cat([1,2,3]), [1,2,3], 'should concatenate one homogenious array');
var result = _.cat([1, "2", [3], {n: 4}]);
deepEqual(result, [1, "2", [3], {n: 4}], 'should concatenate one heterogenious array');
result = (function(){ return _.cat(arguments); })(1, 2, 3);
deepEqual(result, [1, 2, 3], 'should concatenate the arguments object');
// two args
deepEqual(_.cat([1,2,3],[4,5,6]), [1,2,3,4,5,6], 'should concatenate two arrays');
result = (function(){ return _.cat(arguments, [4,5,6]); })(1,2,3);
deepEqual(result, [1,2,3,4,5,6], 'should concatenate an array and an arguments object');
// > 2 args
var a = [1,2,3];
var b = [4,5,6];
var c = [7,8,9];
var d = [0,0,0];
deepEqual(_.cat(a,b,c), [1,2,3,4,5,6,7,8,9], 'should concatenate three arrays');
deepEqual(_.cat(a,b,c,d), [1,2,3,4,5,6,7,8,9,0,0,0], 'should concatenate four arrays');
result = (function(){ return _.cat(arguments,b,c,d); }).apply(null, a);
deepEqual(result, [1,2,3,4,5,6,7,8,9,0,0,0], 'should concatenate four arrays, including an arguments object');
// heterogenious types
deepEqual(_.cat([1],2), [1,2], 'should concatenate mixed types');
deepEqual(_.cat([1],2,3), [1,2,3], 'should concatenate mixed types');
deepEqual(_.cat(1,2,3), [1,2,3], 'should concatenate mixed types');
result = (function(){ return _.cat(arguments, 4,5,6); })(1,2,3);
deepEqual(result, [1,2,3,4,5,6], 'should concatenate mixed types, including an arguments object');
});
test("cons", function() {
deepEqual(_.cons(0, []), [0], 'should insert the first arg into the array given as the second arg');
deepEqual(_.cons(1, [2]), [1,2], 'should insert the first arg into the array given as the second arg');
deepEqual(_.cons([0], [1,2,3]), [[0],1,2,3], 'should insert the first arg into the array given as the second arg');
deepEqual(_.cons(1, 2), [1,2], 'should create a pair if the second is not an array');
deepEqual(_.cons([1], 2), [[1],2], 'should create a pair if the second is not an array');
result = (function(){ return _.cons(0, arguments); })(1,2,3);
deepEqual(result, [0,1,2,3], 'should construct an array given an arguments object as the tail');
var a = [1,2,3];
var result = _.cons(0,a);
deepEqual(a, [1,2,3], 'should not modify the original tail');
});
test("chunk", function() {
var a = _.range(4);
var b = _.range(5);
var c = _.range(7);
deepEqual(_.chunk(a, 2), [[0,1],[2,3]], 'should chunk into the size given');
deepEqual(_.chunk(b, 2), [[0,1],[2,3]], 'should chunk into the size given. Extras are dropped');
var result = _.chunk(a, 2);
deepEqual(a, _.range(4), 'should not modify the original array');
deepEqual(_.chunk(c, 3, [7,8]), [[0,1,2],[3,4,5],[6,7,8]], 'should allow one to specify a padding array');
deepEqual(_.chunk(b, 3, 9), [[0,1,2],[3,4,9]], 'should allow one to specify a padding value');
});
test("chunkAll", function() {
var a = _.range(4);
var b = _.range(10);
deepEqual(_.chunkAll(a, 2), [[0,1],[2,3]], 'should chunk into the size given');
deepEqual(_.chunkAll(b, 4), [[0,1,2,3],[4,5,6,7],[8,9]], 'should chunk into the size given, with a small end');
var result = _.chunkAll(a, 2);
deepEqual(a, _.range(4), 'should not modify the original array');
deepEqual(_.chunkAll(b, 2, 4), [[0,1],[4,5],[8,9]], 'should chunk into the size given, with skips');
deepEqual(_.chunkAll(b, 3, 4), [[0,1,2],[4,5,6],[8,9]], 'should chunk into the size given, with skips and a small end');
});
test("mapcat", function() {
var a = [1,2,3];
var commaize = function(e) { return _.cons(e, [","]); };
deepEqual(_.mapcat(a, commaize), [1, ",", 2, ",", 3, ","], 'should return an array with all intermediate mapped arrays concatenated');
});
test("interpose", function() {
var a = [1,2,3];
var b = [1,2];
var c = [1];
deepEqual(_.interpose(a, 0), [1,0,2,0,3], 'should put the 2nd arg between the elements of the array given');
deepEqual(_.interpose(b, 0), [1,0,2], 'should put the 2nd arg between the elements of the array given');
deepEqual(_.interpose(c, 0), [1], 'should return the array given if nothing to interpose');
deepEqual(_.interpose([], 0), [], 'should return an empty array given an empty array');
var result = _.interpose(b,0);
deepEqual(b, [1,2], 'should not modify the original array');
});
test("weave", function() {
var a = [1,2,3];
var b = [1,2];
var c = ['a', 'b', 'c'];
var d = [1, [2]];
// zero
deepEqual(_.weave(), [], 'should weave zero arrays');
// one
deepEqual(_.weave([]), [], 'should weave one array');
deepEqual(_.weave([1,[2]]), [1,[2]], 'should weave one array');
// two
deepEqual(_.weave(a,b), [1,1,2,2,3], 'should weave two arrays');
deepEqual(_.weave(a,a), [1,1,2,2,3,3], 'should weave two arrays');
deepEqual(_.weave(c,a), ['a',1,'b',2,'c',3], 'should weave two arrays');
deepEqual(_.weave(a,d), [1,1,2,[2],3], 'should weave two arrays');
// > 2
deepEqual(_.weave(a,b,c), [1,1,'a',2,2,'b',3,'c'], 'should weave more than two arrays');
deepEqual(_.weave(a,b,c,d), [1,1,'a',1,2,2,'b',[2],3,'c'], 'should weave more than two arrays');
});
test("repeat", function() {
deepEqual(_.repeat(3,1), [1,1,1], 'should build an array of size n with the specified element in each slot');
deepEqual(_.repeat(0), [], 'should return an empty array if given zero and no repeat arg');
deepEqual(_.repeat(0,9999), [], 'should return an empty array if given zero and some repeat arg');
});
test("cycle", function() {
var a = [1,2,3];
deepEqual(_.cycle(3, a), [1,2,3,1,2,3,1,2,3], 'should build an array with the specified array contents repeated n times');
deepEqual(_.cycle(0, a), [], 'should return an empty array if told to repeat zero times');
deepEqual(_.cycle(-1000, a), [], 'should return an empty array if told to repeat negative times');
});
test("splitAt", function() {
var a = [1,2,3,4,5];
deepEqual(_.splitAt(a, 2), [[1,2],[3,4,5]], 'should bifurcate an array at a given index');
deepEqual(_.splitAt(a, 0), [[], [1,2,3,4,5]], 'should bifurcate an array at a given index');
deepEqual(_.splitAt(a, 5), [[1,2,3,4,5],[]], 'should bifurcate an array at a given index');
deepEqual(_.splitAt([], 5), [[],[]], 'should bifurcate an array at a given index');
});
test("iterateUntil", function() {
var dec = function(n) { return n - 1; };
var isPos = function(n) { return n > 0; };
deepEqual(_.iterateUntil(dec, isPos, 6), [5,4,3,2,1], 'should build an array, decrementing a number while positive');
});
test("takeSkipping", function() {
deepEqual(_.takeSkipping(_.range(5), 0), [], 'should take nothing if told to skip by zero');
deepEqual(_.takeSkipping(_.range(5), -1), [], 'should take nothing if told to skip by negative');
deepEqual(_.takeSkipping(_.range(5), 100), [0], 'should take first element if told to skip by big number');
deepEqual(_.takeSkipping(_.range(5), 1), [0,1,2,3,4], 'should take every element in an array');
deepEqual(_.takeSkipping(_.range(10), 2), [0,2,4,6,8], 'should take every 2nd element in an array');
});
test("reductions", function() {
var result = _.reductions([1,2,3,4,5], function(agg, n) {
return agg + n;
}, 0);
deepEqual(result, [1,3,6,10,15], 'should retain each intermediate step in a reduce');
});
test("keepIndexed", function() {
var a = ['a', 'b', 'c', 'd', 'e'];
var b = [-9, 0, 29, -7, 45, 3, -8];
var oddy = function(k, v) { return _.isOdd(k) ? v : undefined; };
var posy = function(k, v) { return _.isPositive(v) ? k : undefined; };
deepEqual(_.keepIndexed(a, _.isOdd), [false,true,false,true,false], 'runs the predciate on the index, and not the element');
deepEqual(_.keepIndexed(a, oddy), ['b', 'd'], 'keeps elements whose index passes a truthy test');
deepEqual(_.keepIndexed(b, posy), [2,4,5], 'keeps elements whose index passes a truthy test');
deepEqual(_.keepIndexed(_.range(10), oddy), [1,3,5,7,9], 'keeps elements whose index passes a truthy test');
});
test('reverseOrder', function() {
var arr = [1, 2, 3];
deepEqual(_.reverseOrder(arr), [3, 2, 1], 'returns an array whose elements are in the opposite order of the argument');
deepEqual(arr, [1, 2, 3], 'should not mutate the argument');
var throwingFn = function() { _.reverseOrder('string'); };
throws(throwingFn, TypeError, 'throws a TypeError when given a string');
var argObj = (function() { return arguments; })(1, 2, 3);
deepEqual(_.reverseOrder(argObj), [3, 2, 1], 'works with other array-like objects');
});
});