-
Notifications
You must be signed in to change notification settings - Fork 1
/
inferArrayType.js
40 lines (36 loc) · 1.04 KB
/
inferArrayType.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
const StringArray = require('./stringArray');
const TYPED_ARRAY = Int32Array.__proto__;
module.exports = function inferArrayType(arr) {
if (arr instanceof TYPED_ARRAY) {
return Object.getPrototypeOf(arr).constructor;
}
let hasNegative = false,
hasDecimal = false,
hasString = false,
largest = 0,
smallest = 0;
for (var i = 0; i < arr.length; i++) {
let v = arr[i];
if (!hasNegative && v < 0) hasNegative = true;
if (!hasDecimal && v && !Number.isInteger(v)) hasDecimal = true;
if (!hasString && typeof v === 'string') hasString = true;
if (v > largest) largest = v;
if (v < smallest) smallest = v;
}
if (hasString) {
return StringArray;
}
if (hasDecimal) {
return Float64Array;
}
else if (hasNegative) {
if (smallest < -32768 || largest >= 32768) return Int32Array;
if (smallest < -128 || largest >= 128) return Int16Array;
return Int8Array;
}
else {
if (largest >= 65536) return Uint32Array;
if (largest >= 256) return Uint16Array;
return Uint8Array;
}
};