-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
buffer-deno.ts
53 lines (40 loc) · 1.05 KB
/
buffer-deno.ts
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
/*
⛔⛔⛔ WARN ⛔⛔⛔
This file is built for Deno.
Dont be surprised if it yields errors in Node, it's not meant to be used there.
(transpilation replaces buffer-node.ts by buffer-node.ts when building deno version)
*/
export type TBuffer = Uint8Array;
export function bufToString(buf: TBuffer): string {
// @ts-ignore
const decoder = new TextDecoder()
return decoder.decode(buf);
}
export function bufCompare(a: TBuffer, b: TBuffer) {
if (a === b) {
return 0;
}
if (a.length > b.length) {
return 1;
}
for (let i = 0; i < a.length; i++) {
const d = a[i] - b[i];
if (d === 0) {
continue;
}
return d < 0 ? -1 : 1;
}
return 0;
}
export function bufFromString(str: string) {
// @ts-ignore
const encoder = new TextEncoder()
const buffer = encoder.encode(str);
return buffer;
}
export function isBuf(v: any): v is TBuffer {
return v instanceof Uint8Array;
}
export function bufClone(buf: TBuffer): TBuffer {
return new Uint8Array(buf);
}