-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
27 lines (22 loc) · 746 Bytes
/
util.js
File metadata and controls
27 lines (22 loc) · 746 Bytes
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
export function concatWithLength(...arrays) {
let totalLength = arrays.reduce((sum, arr) => sum + 2 + arr.length, 0);
let combined = new Uint8Array(totalLength);
let offset = 0;
for (const arr of arrays) {
combined[offset] = (arr.length >> 8) & 0xff;
combined[offset + 1] = arr.length & 0xff;
combined.set(arr, offset + 2);
offset += 2 + arr.length;
}
return combined;
}
export function splitWithLength(combined) {
const arrays = [];
let offset = 0;
while (offset < combined.length) {
const len = (combined[offset] << 8) | combined[offset + 1];
arrays.push(combined.slice(offset + 2, offset + 2 + len));
offset += 2 + len;
}
return arrays;
}