-
Notifications
You must be signed in to change notification settings - Fork 1
/
Id.ts
99 lines (83 loc) · 2.52 KB
/
Id.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { MultibaseFormats } from './utils';
import * as utils from './utils';
/**
* IdInternal can be used as a string primitive
* This type hack (as a number) prevents TS from complaining
* See: https://github.com/microsoft/TypeScript/issues/4538
*/
type Id = IdInternal & number;
class IdInternal extends Uint8Array {
public static create<T extends Id = Id>(): T;
public static create<T extends Id = Id>(id: T): T;
public static create<T extends Id = Id>(length: number): T;
public static create<T extends Id = Id>(
array: ArrayLike<number> | ArrayBufferLike,
): T;
public static create<T extends Id = Id>(
buffer: ArrayBufferLike,
byteOffset?: number,
length?: number,
): T;
public static create<T extends Id = Id>(...args: Array<any>): T {
// @ts-ignore: spreading into Uint8Array constructor
return new IdInternal(...args) as T;
}
public static fromString<T extends Id = Id>(idString: string): T {
return utils.fromString(idString) as T;
}
public static fromJSON<T extends Id = Id>(json: any): T | undefined {
return utils.fromJSON(json) as T;
}
/**
* Decodes as Buffer zero-copy
*/
public static fromBuffer<T extends Id = Id>(idBuffer: Buffer): T {
return utils.fromBuffer(idBuffer) as T;
}
public static fromUUID<T extends Id = Id>(uuid: string): T | undefined {
return utils.fromUUID(uuid) as T;
}
public static fromMultibase<T extends Id = Id>(
idString: string,
): T | undefined {
return utils.fromMultibase(idString) as T;
}
public [Symbol.toPrimitive](_hint: 'string' | 'number' | 'default'): string {
return utils.toString(this as unknown as Id);
}
public toString(): string {
return utils.toString(this as unknown as Id);
}
public toJSON(): { type: string; data: Array<number> } {
return utils.toJSON(this);
}
/**
* Encodes as Buffer zero-copy
*/
public toBuffer(): Buffer {
return utils.toBuffer(this);
}
/**
* Encodes as a 16 byte UUID
* This only works when the Id is 16 bytes long
*/
public toUUID(): string {
return utils.toUUID(this);
}
/**
* Encodes an multibase ID string
*/
public toMultibase(format: MultibaseFormats): string {
return utils.toMultibase(this, format);
}
/**
* Efficiently compares for equality
* This is faster than comparing by binary string
* If you have an ArrayBuffer, wrap it in Uint8Array first
*/
public equals(id: Uint8Array): boolean {
return utils.equals(this, id);
}
}
export default IdInternal;
export type { Id };