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
48 lines (40 loc) · 1.14 KB
/
Copy pathindex.js
File metadata and controls
48 lines (40 loc) · 1.14 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
module.exports = zip;
/*
zip([1, 2, 3]); // [[1], [2], [3]]
zip([1, 2, 3], ['a', 'b', 'c']); // [[1, 'a'], [2, 'b'], [3, 'c']]
zip([1, 2], ['a', 'b'], [true, false]); //[[1, 'a', true], [2, 'b', false]]
zip([1, 2, 3], ['a', 'b'], [true]);
// [[1, 'a', true], [2, 'b', undefined], [3, undefined, undefined]]
zip(undefined, {}, false, 1, 'foo'); // throws
zip([1, 2], ['a', 'b'], undefined, {}, false, 1, 'foo'); // throws
*/
function zip() {
var result = [];
var args = Array.prototype.slice.call(arguments);
var argsLen = args.length;
var maxLen = 0;
var i, j;
if (!argsLen) {
throw new Error('zip requires at least one argument');
}
for (i = 0; i < argsLen; i++) {
if (!Array.isArray(args[i])) {
throw new Error('all arguments must be arrays');
}
var arrLen = args[i].length;
if (arrLen > maxLen) {
maxLen = arrLen;
}
}
for (i = 0; i < maxLen; i++) {
var group = [];
for (j = 0; j < argsLen; j++) {
if (!Array.isArray(args[j])) {
throw new Error('all arguments must be arrays');
}
group[j] = args[j][i];
}
result[i] = group;
}
return result;
}