-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.js
75 lines (59 loc) · 1.61 KB
/
chunk.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
/**
@name chunk
@param array {Array}
@param size {Number} size=1
Creates an array of elements split into groups the length of size.
If array can't split evenly, the final chunk will be in the remaining elements
O(n) complexity
Test
var testArray = ['a', 'b', 'c', 'd'];
chunk(testArray, 2) // => [['a', 'b'], ['c', 'd']]
chunk(testArray, 2) // => [['a', 'b', 'c'], ['d']]
assert(JSON.stringify(chunk(testArray, 2)), JSON.stringify([['a', 'b'], ['c', 'd']]));
assert(JSON.stringify(chunk(testArray, 3)), JSON.stringify([['a', 'b', 'c'], ['d']]));
function assert(a, b) {
if (a === b) {
console.log('ASSERTION PASSED');
} else {
console.log('ASSERTION FAILED');
}
}
*/
// version 1
function chunk(array, size) {
// default to 1
if ( typeof size !== 'number' ) {
size = 1;
}
var chunks = [];
var chunkIndex = 0;
for (var i = 0, length = array.length; i < length; i++ ) {
// chunk initialization
if (chunks[chunkIndex] === undefined) {
chunks[chunkIndex] = [];
}
// results.length < size
if ( chunks[chunkIndex].length < size ) {
chunks[chunkIndex].push(array[i]);
}
// if is chunks length is equal to the size
if ( chunks[chunkIndex].length === size ) {
chunkIndex++;
}
}
return chunks;
}
// version 0
// slower because slice must doing another operations
function chunk (array, size) {
var length = array.length;
var i = 0;
var j = size;
var results = [];
while(i < length) {
results.push(array.slice(i, j));
i = i + size;
j = i + size;
}
return results;
}