-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (47 loc) · 1.03 KB
/
index.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
/**
* get type string
* @param val
* @returns {string}
*/
function getType(val) {
// `[object Array]`.slice(8, -1) ==> Array
return Object.prototype.toString.call(val).slice(8, -1);
}
/**
* check whether arr is an Array of TypedArray
* @param arr
* @return bool
*/
function isArray(arr) {
var type = getType(arr);
var arrTypes = [
'Array',
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
];
return arrTypes.indexOf(type) !== -1;
}
module.exports = function chunks(arr, size) {
if (!isArray(arr)) {
throw new TypeError('Input should be Array or TypedArray');
}
if (typeof size !== 'number') {
throw new TypeError('Size should be a Number');
}
var result = [];
for (var i = 0; i < arr.length; i += size) {
if (typeof arr.slice === 'function') {
result.push(arr.slice(i, size + i));
} else {
result.push(arr.subarray(i, size + i));
}
}
return result;
};