|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -/** |
7 | | - * Represents a UUID as defined by rfc4122. |
8 | | - */ |
9 | | -export interface UUID { |
10 | | - |
11 | | - /** |
12 | | - * @returns the canonical representation in sets of hexadecimal numbers separated by dashes. |
13 | | - */ |
14 | | - asHex(): string; |
15 | | -} |
16 | | - |
17 | | -class ValueUUID implements UUID { |
18 | | - |
19 | | - constructor(public _value: string) { |
20 | | - // empty |
21 | | - } |
22 | | - |
23 | | - public asHex(): string { |
24 | | - return this._value; |
25 | | - } |
26 | | -} |
27 | | - |
28 | | -class V4UUID extends ValueUUID { |
29 | | - |
30 | | - private static readonly _chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; |
31 | | - |
32 | | - private static readonly _timeHighBits = ['8', '9', 'a', 'b']; |
33 | | - |
34 | | - private static _oneOf(array: string[]): string { |
35 | | - return array[Math.floor(array.length * Math.random())]; |
36 | | - } |
37 | | - |
38 | | - private static _randomHex(): string { |
39 | | - return V4UUID._oneOf(V4UUID._chars); |
40 | | - } |
41 | | - |
42 | | - constructor() { |
43 | | - super([ |
44 | | - V4UUID._randomHex(), |
45 | | - V4UUID._randomHex(), |
46 | | - V4UUID._randomHex(), |
47 | | - V4UUID._randomHex(), |
48 | | - V4UUID._randomHex(), |
49 | | - V4UUID._randomHex(), |
50 | | - V4UUID._randomHex(), |
51 | | - V4UUID._randomHex(), |
52 | | - '-', |
53 | | - V4UUID._randomHex(), |
54 | | - V4UUID._randomHex(), |
55 | | - V4UUID._randomHex(), |
56 | | - V4UUID._randomHex(), |
57 | | - '-', |
58 | | - '4', |
59 | | - V4UUID._randomHex(), |
60 | | - V4UUID._randomHex(), |
61 | | - V4UUID._randomHex(), |
62 | | - '-', |
63 | | - V4UUID._oneOf(V4UUID._timeHighBits), |
64 | | - V4UUID._randomHex(), |
65 | | - V4UUID._randomHex(), |
66 | | - V4UUID._randomHex(), |
67 | | - '-', |
68 | | - V4UUID._randomHex(), |
69 | | - V4UUID._randomHex(), |
70 | | - V4UUID._randomHex(), |
71 | | - V4UUID._randomHex(), |
72 | | - V4UUID._randomHex(), |
73 | | - V4UUID._randomHex(), |
74 | | - V4UUID._randomHex(), |
75 | | - V4UUID._randomHex(), |
76 | | - V4UUID._randomHex(), |
77 | | - V4UUID._randomHex(), |
78 | | - V4UUID._randomHex(), |
79 | | - V4UUID._randomHex(), |
80 | | - ].join('')); |
81 | | - } |
82 | | -} |
83 | | - |
84 | | -export function v4(): UUID { |
85 | | - return new V4UUID(); |
86 | | -} |
87 | 6 |
|
88 | 7 | const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; |
89 | 8 |
|
90 | 9 | export function isUUID(value: string): boolean { |
91 | 10 | return _UUIDPattern.test(value); |
92 | 11 | } |
93 | 12 |
|
94 | | -/** |
95 | | - * Parses a UUID that is of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. |
96 | | - * @param value A uuid string. |
97 | | - */ |
98 | | -export function parse(value: string): UUID { |
99 | | - if (!isUUID(value)) { |
100 | | - throw new Error('invalid uuid'); |
101 | | - } |
102 | | - |
103 | | - return new ValueUUID(value); |
| 13 | +// prep-work |
| 14 | +const _data = new Uint8Array(16); |
| 15 | +const _hex: string[] = []; |
| 16 | +for (let i = 0; i < 256; i++) { |
| 17 | + _hex.push(i.toString(16).padStart(2, '0')); |
104 | 18 | } |
105 | 19 |
|
| 20 | +const _fillRandomValues = typeof crypto === 'object' |
| 21 | + ? crypto.getRandomValues.bind(crypto) |
| 22 | + : function (bucket: Uint8Array): Uint8Array { // todo@jrieken node nodejs use `crypto#randomBytes`, see: https://nodejs.org/docs/latest/api/crypto.html#crypto_crypto_randombytes_size_callback |
| 23 | + for (let i = 0; i < bucket.length; i++) { |
| 24 | + bucket[i] = Math.floor(Math.random() * 256); |
| 25 | + } |
| 26 | + return bucket; |
| 27 | + }; |
| 28 | + |
106 | 29 | export function generateUuid(): string { |
107 | | - return v4().asHex(); |
| 30 | + // get data |
| 31 | + _fillRandomValues(_data); |
| 32 | + |
| 33 | + // set version bits |
| 34 | + _data[6] = (_data[6] & 0x0f) | 0x40; |
| 35 | + _data[8] = (_data[8] & 0x3f) | 0x80; |
| 36 | + |
| 37 | + // print as string |
| 38 | + let i = 0; |
| 39 | + let result = ''; |
| 40 | + result += _hex[_data[i++]]; |
| 41 | + result += _hex[_data[i++]]; |
| 42 | + result += _hex[_data[i++]]; |
| 43 | + result += _hex[_data[i++]]; |
| 44 | + result += '-'; |
| 45 | + result += _hex[_data[i++]]; |
| 46 | + result += _hex[_data[i++]]; |
| 47 | + result += '-'; |
| 48 | + result += _hex[_data[i++]]; |
| 49 | + result += _hex[_data[i++]]; |
| 50 | + result += '-'; |
| 51 | + result += _hex[_data[i++]]; |
| 52 | + result += _hex[_data[i++]]; |
| 53 | + result += '-'; |
| 54 | + result += _hex[_data[i++]]; |
| 55 | + result += _hex[_data[i++]]; |
| 56 | + result += _hex[_data[i++]]; |
| 57 | + result += _hex[_data[i++]]; |
| 58 | + result += _hex[_data[i++]]; |
| 59 | + result += _hex[_data[i++]]; |
| 60 | + return result; |
108 | 61 | } |
0 commit comments