-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.js
55 lines (47 loc) · 1.39 KB
/
binary.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
// binary.js
// Martin Pravda
/*jslint browser, node */
const reader_map = {
"float32": DataView.prototype.getFloat32,
"float64": DataView.prototype.getFloat64,
"int16": DataView.prototype.getInt16,
"int32": DataView.prototype.getInt32,
"int8": DataView.prototype.getInt8,
"uint16": DataView.prototype.getUint16,
"uint32": DataView.prototype.getUint32,
"uint8": DataView.prototype.getUint8
};
function reader(view) {
let cursor = 0;
let is_little_endian = false;
function read(type, increment) {
return function (offset = 0) {
const view_reader = reader_map[type];
const data = view_reader.apply(view, [
offset + cursor,
is_little_endian
]);
cursor += increment;
return data;
};
}
function set_little_endian(value = false) {
is_little_endian = value;
}
function reset_cursor() {
cursor = 0;
}
return Object.freeze({
read_float32: read("float32", 4),
read_float64: read("float64", 8),
read_int16: read("int16", 2),
read_int32: read("int32", 4),
read_int8: read("int8", 1),
read_uint16: read("uint16", 2),
read_uint32: read("uint32", 4),
read_uint8: read("uint8", 1),
reset_cursor,
set_little_endian
});
}
export default Object.freeze({reader});