-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.js
More file actions
41 lines (41 loc) · 1.14 KB
/
Copy pathtypes.js
File metadata and controls
41 lines (41 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
'use strict';
function type(size, read, write) {
return { size, read, write };
}
const numtype = (name, size) =>
type(
size,
(buf, offset) => buf[`read${name}`](offset, size),
(buf, offset, value) => buf[`write${name}`](value, offset, size)
);
module.exports = {
int8: numtype('Int8', 1),
int16: numtype('IntBE', 2),
int24: numtype('IntBE', 3),
int32: numtype('IntBE', 4),
int40: numtype('IntBE', 5),
int48: numtype('IntBE', 6),
int64: numtype('BigInt64BE', 8),
uint8: numtype('UInt8', 1),
uint16: numtype('UIntBE', 2),
uint24: numtype('UIntBE', 3),
uint32: numtype('UIntBE', 4),
uint40: numtype('UIntBE', 5),
uint48: numtype('UIntBE', 6),
uint64: numtype('BigUInt64BE', 8),
float: numtype('FloatBE', 4),
double: numtype('DoubleBE', 8),
char: (size, encoding = 'utf8') =>
type(
size,
(buf, offset) => buf.toString(encoding, offset, size + offset),
(buf, offset, value) => {
const string = Buffer.from(value, encoding);
return string.length <= size ?
string.copy(buf, offset) :
console.log(new Error('String is too big'));
}
),
type,
numtype
};