@powfix/core-js 0.19.2 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/cjs/browser/utils/UUID.js +2 -303
- package/dist/{node/cjs/browser → browser/cjs/shared}/utils/UUID.js +16 -10
- package/dist/browser/cjs/shared/utils/UUID.types.js +2 -0
- package/dist/browser/cjs/shared/utils/global/{fallbackIfEqual.js → fallbackIfMatch.js} +2 -2
- package/dist/browser/cjs/shared/utils/global/fallbackIfNull.js +2 -2
- package/dist/browser/cjs/shared/utils/global/fallbackIfNullish.js +9 -0
- package/dist/browser/cjs/shared/utils/global/fallbackIfUndefined.js +2 -2
- package/dist/browser/cjs/shared/utils/global/index.js +2 -1
- package/dist/browser/cjs/shared/utils/index.js +2 -0
- package/dist/browser/esm/browser/utils/UUID.mjs +2 -303
- package/dist/{node/esm/browser → browser/esm/shared}/utils/UUID.mjs +13 -7
- package/dist/browser/esm/shared/utils/UUID.types.mjs +1 -0
- package/dist/browser/esm/shared/utils/global/{fallbackIfEqual.mjs → fallbackIfMatch.mjs} +1 -1
- package/dist/browser/esm/shared/utils/global/fallbackIfNull.mjs +2 -2
- package/dist/browser/esm/shared/utils/global/fallbackIfNullish.mjs +6 -0
- package/dist/browser/esm/shared/utils/global/fallbackIfUndefined.mjs +2 -2
- package/dist/browser/esm/shared/utils/global/index.mjs +2 -1
- package/dist/browser/esm/shared/utils/index.mjs +2 -0
- package/dist/browser/types/browser/utils/UUID.d.ts +2 -143
- package/dist/{node/types/browser → browser/types/shared}/utils/UUID.d.ts +9 -8
- package/dist/browser/types/shared/utils/UUID.types.d.ts +2 -0
- package/dist/browser/types/shared/utils/global/fallbackIfMatch.d.ts +1 -0
- package/dist/browser/types/shared/utils/global/fallbackIfNullish.d.ts +1 -0
- package/dist/browser/types/shared/utils/global/index.d.ts +2 -1
- package/dist/browser/types/shared/utils/index.d.ts +1 -0
- package/dist/node/cjs/nodejs/utils/UUID.js +1 -1
- package/dist/node/cjs/nodejs/utils/sequelize-utils/SequelizeUtils.js +5 -6
- package/dist/node/cjs/shared/utils/UUID.js +314 -0
- package/dist/node/cjs/shared/utils/UUID.types.js +2 -0
- package/dist/node/cjs/shared/utils/global/{fallbackIfEqual.js → fallbackIfMatch.js} +2 -2
- package/dist/node/cjs/shared/utils/global/fallbackIfNull.js +2 -2
- package/dist/node/cjs/shared/utils/global/fallbackIfNullish.js +9 -0
- package/dist/node/cjs/shared/utils/global/fallbackIfUndefined.js +2 -2
- package/dist/node/cjs/shared/utils/global/index.js +2 -1
- package/dist/node/cjs/shared/utils/index.js +2 -0
- package/dist/node/esm/nodejs/utils/UUID.mjs +1 -1
- package/dist/node/esm/nodejs/utils/sequelize-utils/SequelizeUtils.mjs +5 -6
- package/dist/node/esm/shared/utils/UUID.mjs +310 -0
- package/dist/node/esm/shared/utils/UUID.types.mjs +1 -0
- package/dist/node/esm/shared/utils/global/{fallbackIfEqual.mjs → fallbackIfMatch.mjs} +1 -1
- package/dist/node/esm/shared/utils/global/fallbackIfNull.mjs +2 -2
- package/dist/node/esm/shared/utils/global/fallbackIfNullish.mjs +6 -0
- package/dist/node/esm/shared/utils/global/fallbackIfUndefined.mjs +2 -2
- package/dist/node/esm/shared/utils/global/index.mjs +2 -1
- package/dist/node/esm/shared/utils/index.mjs +2 -0
- package/dist/node/types/nodejs/utils/UUID.d.ts +1 -1
- package/dist/node/types/shared/utils/UUID.d.ts +145 -0
- package/dist/node/types/shared/utils/UUID.types.d.ts +2 -0
- package/dist/node/types/shared/utils/global/fallbackIfMatch.d.ts +1 -0
- package/dist/node/types/shared/utils/global/fallbackIfNullish.d.ts +1 -0
- package/dist/node/types/shared/utils/global/index.d.ts +2 -1
- package/dist/node/types/shared/utils/index.d.ts +1 -0
- package/package.json +1 -1
- package/dist/browser/types/shared/utils/global/fallbackIfEqual.d.ts +0 -1
- package/dist/node/types/shared/utils/global/fallbackIfEqual.d.ts +0 -1
|
@@ -1,304 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
* Represents a UUID (Universally Unique Identifier) and provides various utility
|
|
4
|
-
* methods for validation, conversion, comparison and creation.
|
|
5
|
-
*/
|
|
6
|
-
export class UUID {
|
|
7
|
-
/* --------------------------------------------------------------------
|
|
8
|
-
* Validation helpers
|
|
9
|
-
* -------------------------------------------------------------------- */
|
|
10
|
-
/**
|
|
11
|
-
* Checks if the supplied value is a valid hex representation of a UUID.
|
|
12
|
-
* @param hex - The string to validate.
|
|
13
|
-
* @returns true if the string matches {@link REGEX_HEX}.
|
|
14
|
-
*/
|
|
15
|
-
static isValidHex(hex) {
|
|
16
|
-
if (typeof hex !== 'string') {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
return UUID.REGEX_HEX.test(hex);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Checks if the supplied value is a valid RFC 4122 UUID string.
|
|
23
|
-
* @param str - The string to validate.
|
|
24
|
-
* @returns true if the string matches {@link REGEX_RFC4122}.
|
|
25
|
-
*/
|
|
26
|
-
static isValidString(str) {
|
|
27
|
-
if (typeof str !== 'string') {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
return UUID.REGEX_RFC4122.test(str);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Checks whether an ArrayBufferView contains exactly {@link BYTE_LENGTH} bytes.
|
|
34
|
-
* @param bytes - The view to check.
|
|
35
|
-
* @returns true if the byte length is correct.
|
|
36
|
-
*/
|
|
37
|
-
static isValidBytes(bytes) {
|
|
38
|
-
return bytes.byteLength === UUID.BYTE_LENGTH;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Generic validation that accepts either a string (hex or RFC 4122 format)
|
|
42
|
-
* or an ArrayBufferView containing the raw bytes.
|
|
43
|
-
* @param input - The value to validate.
|
|
44
|
-
* @returns true if the input is a valid representation of a UUID.
|
|
45
|
-
*/
|
|
46
|
-
static isValid(input) {
|
|
47
|
-
if (typeof input === 'string') {
|
|
48
|
-
const length = input.length;
|
|
49
|
-
switch (length) {
|
|
50
|
-
case UUID.STR_LENGTH:
|
|
51
|
-
// RFC 4122 uuid(string)
|
|
52
|
-
// 9e472052-a654-4693-9a8b-3ce57ada3d6c
|
|
53
|
-
return UUID.isValidString(input);
|
|
54
|
-
case UUID.HEX_STR_LENGTH:
|
|
55
|
-
// RFC 4122 uuid(string) without hyphens
|
|
56
|
-
// 9e472052a65446939a8b3ce57ada3d6c
|
|
57
|
-
return UUID.isValidHex(input);
|
|
58
|
-
default:
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else if (ArrayBuffer.isView(input)) {
|
|
63
|
-
return UUID.isValidBytes(input);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
/* --------------------------------------------------------------------
|
|
70
|
-
* Parsing / formatting helpers
|
|
71
|
-
* -------------------------------------------------------------------- */
|
|
72
|
-
/**
|
|
73
|
-
* Inserts hyphens into a 32‑character hex string to produce an RFC 4122 string.
|
|
74
|
-
* @param hex - The plain hexadecimal UUID.
|
|
75
|
-
* @returns The formatted RFC 4122 string.
|
|
76
|
-
*/
|
|
77
|
-
static formatHex(hex) {
|
|
78
|
-
if (hex.length !== UUID.HEX_STR_LENGTH) {
|
|
79
|
-
throw new Error(`hex length should be ${UUID.HEX_STR_LENGTH}`);
|
|
80
|
-
}
|
|
81
|
-
return hex.slice(0, 8) + '-' +
|
|
82
|
-
hex.slice(8, 12) + '-' +
|
|
83
|
-
hex.slice(12, 16) + '-' +
|
|
84
|
-
hex.slice(16, 20) + '-' +
|
|
85
|
-
hex.slice(20);
|
|
86
|
-
}
|
|
87
|
-
/** Removes all hyphens from a UUID string. */
|
|
88
|
-
static stripHyphens(str) {
|
|
89
|
-
return str.replace(/-/g, "");
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Converts a plain hexadecimal UUID into a Uint8Array.
|
|
93
|
-
* @param hex - The hex string to parse.
|
|
94
|
-
* @returns A Uint8Array containing the raw bytes.
|
|
95
|
-
*/
|
|
96
|
-
static parseHex(hex) {
|
|
97
|
-
if (hex.length !== UUID.HEX_STR_LENGTH) {
|
|
98
|
-
throw new Error(`Invalid hex string, length should be ${UUID.HEX_STR_LENGTH}`);
|
|
99
|
-
}
|
|
100
|
-
return Uint8ArrayUtils.fromHex(hex);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Parses an RFC 4122 string into a Uint8Array.
|
|
104
|
-
* @param str - The formatted UUID string.
|
|
105
|
-
* @returns A Uint8Array containing the raw bytes.
|
|
106
|
-
*/
|
|
107
|
-
static parseString(str) {
|
|
108
|
-
if (str.length !== UUID.STR_LENGTH) {
|
|
109
|
-
throw new Error(`Invalid UUID string, invalid character length should be ${UUID.STR_LENGTH}`);
|
|
110
|
-
}
|
|
111
|
-
if (!UUID.isValid(str)) {
|
|
112
|
-
throw new Error('Invalid UUID string, should be RFC 4122 format');
|
|
113
|
-
}
|
|
114
|
-
const hex = UUID.stripHyphens(str);
|
|
115
|
-
if (hex.length !== UUID.HEX_STR_LENGTH) {
|
|
116
|
-
throw new Error('Invalid UUID string, invalid character length after strip hyphens');
|
|
117
|
-
}
|
|
118
|
-
return Uint8ArrayUtils.fromHex(hex);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Parses an ArrayBufferView into a Uint8Array ensuring the correct byte length.
|
|
122
|
-
* @param bytes - The view to parse.
|
|
123
|
-
* @returns A Uint8Array containing the raw bytes.
|
|
124
|
-
*/
|
|
125
|
-
static parseBytes(bytes) {
|
|
126
|
-
const view = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
127
|
-
if (view.length !== UUID.BYTE_LENGTH) {
|
|
128
|
-
throw new Error(`Expected ${UUID.BYTE_LENGTH} bytes`);
|
|
129
|
-
}
|
|
130
|
-
return new Uint8Array(view);
|
|
131
|
-
}
|
|
132
|
-
/* --------------------------------------------------------------------
|
|
133
|
-
* Factory methods
|
|
134
|
-
* -------------------------------------------------------------------- */
|
|
135
|
-
/**
|
|
136
|
-
* Creates a UUID instance from a plain hexadecimal string.
|
|
137
|
-
* @param hex - The hex representation of the UUID.
|
|
138
|
-
* @returns A new {@link UUID} object.
|
|
139
|
-
*/
|
|
140
|
-
static fromHex(hex) {
|
|
141
|
-
return new UUID(UUID.parseHex(hex));
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Creates a UUID instance from an RFC 4122 formatted string.
|
|
145
|
-
* @param str - The UUID string with hyphens.
|
|
146
|
-
* @returns A new {@link UUID} object.
|
|
147
|
-
*/
|
|
148
|
-
static fromString(str) {
|
|
149
|
-
return new UUID(UUID.parseString(str));
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Creates a UUID instance from raw bytes.
|
|
153
|
-
* @param bytes - An ArrayBufferView containing 16 bytes.
|
|
154
|
-
* @returns A new {@link UUID} object.
|
|
155
|
-
*/
|
|
156
|
-
static fromBytes(bytes) {
|
|
157
|
-
return new UUID(UUID.parseBytes(bytes));
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Parses any supported input (string or bytes) and returns the raw byte array.
|
|
161
|
-
* @param input - The value to parse.
|
|
162
|
-
* @returns A Uint8Array of length {@link BYTE_LENGTH}.
|
|
163
|
-
*/
|
|
164
|
-
static parse(input) {
|
|
165
|
-
if (typeof input === 'string') {
|
|
166
|
-
const length = input.length;
|
|
167
|
-
switch (length) {
|
|
168
|
-
case UUID.STR_LENGTH:
|
|
169
|
-
// RFC 4122 uuid(string)
|
|
170
|
-
return UUID.parseString(input);
|
|
171
|
-
case UUID.HEX_STR_LENGTH:
|
|
172
|
-
// RFC 4122 uuid(string) without hyphens
|
|
173
|
-
return UUID.parseHex(input);
|
|
174
|
-
default:
|
|
175
|
-
throw new Error(`Invalid input string, length should be ${UUID.STR_LENGTH} or ${UUID.HEX_STR_LENGTH}`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
else if (ArrayBuffer.isView(input)) {
|
|
179
|
-
return UUID.parseBytes(input);
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
throw new Error("Invalid input, Expected string or ArrayBufferView");
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Creates a UUID from any supported input type.
|
|
187
|
-
* @param input - The value to parse.
|
|
188
|
-
* @returns A new {@link UUID} object.
|
|
189
|
-
*/
|
|
190
|
-
static from(input) {
|
|
191
|
-
return UUID.fromBytes(UUID.parse(input));
|
|
192
|
-
}
|
|
193
|
-
/** Returns the nil (all zero) UUID. */
|
|
194
|
-
static nil() {
|
|
195
|
-
return UUID.fromBytes(new Uint8Array(UUID.BYTE_LENGTH));
|
|
196
|
-
}
|
|
197
|
-
/** Returns a UUID consisting of all 0xFF bytes. */
|
|
198
|
-
static max() {
|
|
199
|
-
return UUID.fromBytes((new Uint8Array(UUID.BYTE_LENGTH)).fill(0xFF));
|
|
200
|
-
}
|
|
201
|
-
/* --------------------------------------------------------------------
|
|
202
|
-
* Equality / comparison
|
|
203
|
-
* -------------------------------------------------------------------- */
|
|
204
|
-
/**
|
|
205
|
-
* Compares multiple UUIDs for strict equality.
|
|
206
|
-
* @param uuids - The UUIDs to compare (at least two required).
|
|
207
|
-
* @returns true if all provided UUIDs are identical.
|
|
208
|
-
*/
|
|
209
|
-
static equals(...uuids) {
|
|
210
|
-
const n = uuids.length;
|
|
211
|
-
if (n < 2) {
|
|
212
|
-
throw new Error('At least two UUIDs required for comparison');
|
|
213
|
-
}
|
|
214
|
-
const ref = uuids[0].bytes;
|
|
215
|
-
for (let i = 1; i < n; ++i) {
|
|
216
|
-
const b = uuids[i].bytes;
|
|
217
|
-
for (let j = 0; j < UUID.BYTE_LENGTH; ++j) {
|
|
218
|
-
if (ref[j] !== b[j])
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Lexicographically compares two UUIDs.
|
|
226
|
-
* @param uuid1 - The first UUID.
|
|
227
|
-
* @param uuid2 - The second UUID.
|
|
228
|
-
* @returns -1 if uuid1 < uuid2, 1 if uuid1 > uuid2, 0 otherwise.
|
|
229
|
-
*/
|
|
230
|
-
static compare(uuid1, uuid2) {
|
|
231
|
-
const a = uuid1.bytes;
|
|
232
|
-
const b = uuid2.bytes;
|
|
233
|
-
for (let i = 0; i < UUID.BYTE_LENGTH; i++) {
|
|
234
|
-
if (a[i] !== b[i])
|
|
235
|
-
return a[i] < b[i] ? -1 : 1;
|
|
236
|
-
}
|
|
237
|
-
return 0;
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Constructs a new {@link UUID} instance from any supported input type.
|
|
241
|
-
* @param input - The value to parse (string or ArrayBufferView).
|
|
242
|
-
*/
|
|
243
|
-
constructor(input) {
|
|
244
|
-
this.bytes = UUID.parse(input);
|
|
245
|
-
}
|
|
246
|
-
/** Instance wrapper for {@link equals}. */
|
|
247
|
-
equals(...uuids) {
|
|
248
|
-
return UUID.equals(this, ...uuids);
|
|
249
|
-
}
|
|
250
|
-
/** Instance wrapper for {@link compare}. */
|
|
251
|
-
compare(other) {
|
|
252
|
-
return UUID.compare(this, other);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Returns the RFC 4122 string representation of this UUID.
|
|
256
|
-
* @returns The formatted UUID string.
|
|
257
|
-
*/
|
|
258
|
-
toString() {
|
|
259
|
-
if (this._str != null) {
|
|
260
|
-
return this._str;
|
|
261
|
-
}
|
|
262
|
-
const hex = this.toHex();
|
|
263
|
-
this._str = UUID.formatHex(hex);
|
|
264
|
-
return this._str;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Returns the plain hexadecimal representation of this UUID.
|
|
268
|
-
* @returns A 32‑character hex string.
|
|
269
|
-
*/
|
|
270
|
-
toHex() {
|
|
271
|
-
if (this._hex != null) {
|
|
272
|
-
return this._hex;
|
|
273
|
-
}
|
|
274
|
-
this._hex = Uint8ArrayUtils.toHex(this.bytes);
|
|
275
|
-
return this._hex;
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Returns a copy of the raw byte array.
|
|
279
|
-
* @returns A new Uint8Array containing the UUID bytes.
|
|
280
|
-
*/
|
|
281
|
-
toBytes() {
|
|
282
|
-
return new Uint8Array(this.bytes);
|
|
283
|
-
}
|
|
284
|
-
/** JSON serialization helper – returns the RFC 4122 string. */
|
|
285
|
-
toJSON() {
|
|
286
|
-
return this.toString();
|
|
287
|
-
}
|
|
1
|
+
import { UUID as BaseUUID } from "../../shared/utils/UUID.mjs";
|
|
2
|
+
export class UUID extends BaseUUID {
|
|
288
3
|
}
|
|
289
|
-
/* --------------------------------------------------------------------
|
|
290
|
-
* Regular expressions used to validate UUIDs in different string forms
|
|
291
|
-
* -------------------------------------------------------------------- */
|
|
292
|
-
/** Matches a 32‑character hexadecimal representation of a UUID without hyphens. */
|
|
293
|
-
UUID.REGEX_HEX = /^[0-9a-fA-F]{8}[0-9a-fA-F]{4}[1-5][0-9a-fA-F]{3}[89abAB][0-9a-fA-F]{3}[0-9a-fA-F]{12}$/;
|
|
294
|
-
/** Matches the RFC 4122 canonical UUID format with hyphens. */
|
|
295
|
-
UUID.REGEX_RFC4122 = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
|
|
296
|
-
/* --------------------------------------------------------------------
|
|
297
|
-
* Constant lengths used throughout the class
|
|
298
|
-
* -------------------------------------------------------------------- */
|
|
299
|
-
/** Number of bytes that represent a UUID. */
|
|
300
|
-
UUID.BYTE_LENGTH = 16;
|
|
301
|
-
/** Total length of an RFC 4122 UUID string (including hyphens). */
|
|
302
|
-
UUID.STR_LENGTH = 36;
|
|
303
|
-
/** Length of a UUID represented as a plain hexadecimal string. */
|
|
304
|
-
UUID.HEX_STR_LENGTH = 32;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Uint8ArrayUtils } from "
|
|
1
|
+
import { Uint8ArrayUtils } from "../index.mjs";
|
|
2
2
|
/**
|
|
3
3
|
* Represents a UUID (Universally Unique Identifier) and provides various utility
|
|
4
4
|
* methods for validation, conversion, comparison and creation.
|
|
@@ -59,6 +59,9 @@ export class UUID {
|
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
+
else if (input instanceof UUID) {
|
|
63
|
+
return UUID.isValidBytes(input.bytes);
|
|
64
|
+
}
|
|
62
65
|
else if (ArrayBuffer.isView(input)) {
|
|
63
66
|
return UUID.isValidBytes(input);
|
|
64
67
|
}
|
|
@@ -138,7 +141,7 @@ export class UUID {
|
|
|
138
141
|
* @returns A new {@link UUID} object.
|
|
139
142
|
*/
|
|
140
143
|
static fromHex(hex) {
|
|
141
|
-
return new
|
|
144
|
+
return new this(UUID.parseHex(hex));
|
|
142
145
|
}
|
|
143
146
|
/**
|
|
144
147
|
* Creates a UUID instance from an RFC 4122 formatted string.
|
|
@@ -146,7 +149,7 @@ export class UUID {
|
|
|
146
149
|
* @returns A new {@link UUID} object.
|
|
147
150
|
*/
|
|
148
151
|
static fromString(str) {
|
|
149
|
-
return new
|
|
152
|
+
return new this(UUID.parseString(str));
|
|
150
153
|
}
|
|
151
154
|
/**
|
|
152
155
|
* Creates a UUID instance from raw bytes.
|
|
@@ -154,7 +157,7 @@ export class UUID {
|
|
|
154
157
|
* @returns A new {@link UUID} object.
|
|
155
158
|
*/
|
|
156
159
|
static fromBytes(bytes) {
|
|
157
|
-
return new
|
|
160
|
+
return new this(UUID.parseBytes(bytes));
|
|
158
161
|
}
|
|
159
162
|
/**
|
|
160
163
|
* Parses any supported input (string or bytes) and returns the raw byte array.
|
|
@@ -175,6 +178,9 @@ export class UUID {
|
|
|
175
178
|
throw new Error(`Invalid input string, length should be ${UUID.STR_LENGTH} or ${UUID.HEX_STR_LENGTH}`);
|
|
176
179
|
}
|
|
177
180
|
}
|
|
181
|
+
else if (input instanceof UUID) {
|
|
182
|
+
return input.toBytes();
|
|
183
|
+
}
|
|
178
184
|
else if (ArrayBuffer.isView(input)) {
|
|
179
185
|
return UUID.parseBytes(input);
|
|
180
186
|
}
|
|
@@ -188,15 +194,15 @@ export class UUID {
|
|
|
188
194
|
* @returns A new {@link UUID} object.
|
|
189
195
|
*/
|
|
190
196
|
static from(input) {
|
|
191
|
-
return
|
|
197
|
+
return this.fromBytes(UUID.parse(input));
|
|
192
198
|
}
|
|
193
199
|
/** Returns the nil (all zero) UUID. */
|
|
194
200
|
static nil() {
|
|
195
|
-
return
|
|
201
|
+
return this.fromBytes(new Uint8Array(UUID.BYTE_LENGTH));
|
|
196
202
|
}
|
|
197
203
|
/** Returns a UUID consisting of all 0xFF bytes. */
|
|
198
204
|
static max() {
|
|
199
|
-
return
|
|
205
|
+
return this.fromBytes((new Uint8Array(UUID.BYTE_LENGTH)).fill(0xFF));
|
|
200
206
|
}
|
|
201
207
|
/* --------------------------------------------------------------------
|
|
202
208
|
* Equality / comparison
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fallbackIfMatch } from "./fallbackIfMatch.mjs";
|
|
2
2
|
export function fallbackIfUndefined(value, fallback) {
|
|
3
|
-
return
|
|
3
|
+
return fallbackIfMatch(value, undefined, fallback);
|
|
4
4
|
}
|
|
@@ -5,8 +5,9 @@ export * from './btoa.mjs';
|
|
|
5
5
|
export * from './castArray.mjs';
|
|
6
6
|
export * from './circularDistance.mjs';
|
|
7
7
|
export * from './sleep.mjs';
|
|
8
|
-
export * from './
|
|
8
|
+
export * from './fallbackIfMatch.mjs';
|
|
9
9
|
export * from './fallbackIfNull.mjs';
|
|
10
|
+
export * from './fallbackIfNullish.mjs';
|
|
10
11
|
export * from './fallbackIfUndefined.mjs';
|
|
11
12
|
export * from './firstNonNullish.mjs';
|
|
12
13
|
export * from './flat.mjs';
|
|
@@ -3,6 +3,8 @@ export * from './ArrayUtils.mjs';
|
|
|
3
3
|
export * from './AxiosUtils.mjs';
|
|
4
4
|
export * from './StringUtils.mjs';
|
|
5
5
|
export * from './Uint8ArrayUtils.mjs';
|
|
6
|
+
// export * from './UUID';
|
|
7
|
+
export * from './UUID.types.mjs';
|
|
6
8
|
export * from './UuidUtils.mjs';
|
|
7
9
|
export * from './BooleanUtils.mjs';
|
|
8
10
|
export * from './Calc.mjs';
|
|
@@ -1,144 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* methods for validation, conversion, comparison and creation.
|
|
4
|
-
*/
|
|
5
|
-
export declare class UUID {
|
|
6
|
-
/** Matches a 32‑character hexadecimal representation of a UUID without hyphens. */
|
|
7
|
-
private static REGEX_HEX;
|
|
8
|
-
/** Matches the RFC 4122 canonical UUID format with hyphens. */
|
|
9
|
-
private static REGEX_RFC4122;
|
|
10
|
-
/** Number of bytes that represent a UUID. */
|
|
11
|
-
private static BYTE_LENGTH;
|
|
12
|
-
/** Total length of an RFC 4122 UUID string (including hyphens). */
|
|
13
|
-
private static STR_LENGTH;
|
|
14
|
-
/** Length of a UUID represented as a plain hexadecimal string. */
|
|
15
|
-
private static HEX_STR_LENGTH;
|
|
16
|
-
/**
|
|
17
|
-
* Checks if the supplied value is a valid hex representation of a UUID.
|
|
18
|
-
* @param hex - The string to validate.
|
|
19
|
-
* @returns true if the string matches {@link REGEX_HEX}.
|
|
20
|
-
*/
|
|
21
|
-
static isValidHex(hex: string): boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Checks if the supplied value is a valid RFC 4122 UUID string.
|
|
24
|
-
* @param str - The string to validate.
|
|
25
|
-
* @returns true if the string matches {@link REGEX_RFC4122}.
|
|
26
|
-
*/
|
|
27
|
-
static isValidString(str: string): boolean;
|
|
28
|
-
/**
|
|
29
|
-
* Checks whether an ArrayBufferView contains exactly {@link BYTE_LENGTH} bytes.
|
|
30
|
-
* @param bytes - The view to check.
|
|
31
|
-
* @returns true if the byte length is correct.
|
|
32
|
-
*/
|
|
33
|
-
static isValidBytes(bytes: ArrayBufferView): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Generic validation that accepts either a string (hex or RFC 4122 format)
|
|
36
|
-
* or an ArrayBufferView containing the raw bytes.
|
|
37
|
-
* @param input - The value to validate.
|
|
38
|
-
* @returns true if the input is a valid representation of a UUID.
|
|
39
|
-
*/
|
|
40
|
-
static isValid(input: string | ArrayBufferView): boolean;
|
|
41
|
-
/**
|
|
42
|
-
* Inserts hyphens into a 32‑character hex string to produce an RFC 4122 string.
|
|
43
|
-
* @param hex - The plain hexadecimal UUID.
|
|
44
|
-
* @returns The formatted RFC 4122 string.
|
|
45
|
-
*/
|
|
46
|
-
private static formatHex;
|
|
47
|
-
/** Removes all hyphens from a UUID string. */
|
|
48
|
-
private static stripHyphens;
|
|
49
|
-
/**
|
|
50
|
-
* Converts a plain hexadecimal UUID into a Uint8Array.
|
|
51
|
-
* @param hex - The hex string to parse.
|
|
52
|
-
* @returns A Uint8Array containing the raw bytes.
|
|
53
|
-
*/
|
|
54
|
-
private static parseHex;
|
|
55
|
-
/**
|
|
56
|
-
* Parses an RFC 4122 string into a Uint8Array.
|
|
57
|
-
* @param str - The formatted UUID string.
|
|
58
|
-
* @returns A Uint8Array containing the raw bytes.
|
|
59
|
-
*/
|
|
60
|
-
private static parseString;
|
|
61
|
-
/**
|
|
62
|
-
* Parses an ArrayBufferView into a Uint8Array ensuring the correct byte length.
|
|
63
|
-
* @param bytes - The view to parse.
|
|
64
|
-
* @returns A Uint8Array containing the raw bytes.
|
|
65
|
-
*/
|
|
66
|
-
private static parseBytes;
|
|
67
|
-
/**
|
|
68
|
-
* Creates a UUID instance from a plain hexadecimal string.
|
|
69
|
-
* @param hex - The hex representation of the UUID.
|
|
70
|
-
* @returns A new {@link UUID} object.
|
|
71
|
-
*/
|
|
72
|
-
static fromHex(hex: string): UUID;
|
|
73
|
-
/**
|
|
74
|
-
* Creates a UUID instance from an RFC 4122 formatted string.
|
|
75
|
-
* @param str - The UUID string with hyphens.
|
|
76
|
-
* @returns A new {@link UUID} object.
|
|
77
|
-
*/
|
|
78
|
-
static fromString(str: string): UUID;
|
|
79
|
-
/**
|
|
80
|
-
* Creates a UUID instance from raw bytes.
|
|
81
|
-
* @param bytes - An ArrayBufferView containing 16 bytes.
|
|
82
|
-
* @returns A new {@link UUID} object.
|
|
83
|
-
*/
|
|
84
|
-
static fromBytes(bytes: ArrayBufferView): UUID;
|
|
85
|
-
/**
|
|
86
|
-
* Parses any supported input (string or bytes) and returns the raw byte array.
|
|
87
|
-
* @param input - The value to parse.
|
|
88
|
-
* @returns A Uint8Array of length {@link BYTE_LENGTH}.
|
|
89
|
-
*/
|
|
90
|
-
private static parse;
|
|
91
|
-
/**
|
|
92
|
-
* Creates a UUID from any supported input type.
|
|
93
|
-
* @param input - The value to parse.
|
|
94
|
-
* @returns A new {@link UUID} object.
|
|
95
|
-
*/
|
|
96
|
-
static from(input: string | ArrayBufferView): UUID;
|
|
97
|
-
/** Returns the nil (all zero) UUID. */
|
|
98
|
-
static nil(): UUID;
|
|
99
|
-
/** Returns a UUID consisting of all 0xFF bytes. */
|
|
100
|
-
static max(): UUID;
|
|
101
|
-
/**
|
|
102
|
-
* Compares multiple UUIDs for strict equality.
|
|
103
|
-
* @param uuids - The UUIDs to compare (at least two required).
|
|
104
|
-
* @returns true if all provided UUIDs are identical.
|
|
105
|
-
*/
|
|
106
|
-
static equals(...uuids: UUID[]): boolean;
|
|
107
|
-
/**
|
|
108
|
-
* Lexicographically compares two UUIDs.
|
|
109
|
-
* @param uuid1 - The first UUID.
|
|
110
|
-
* @param uuid2 - The second UUID.
|
|
111
|
-
* @returns -1 if uuid1 < uuid2, 1 if uuid1 > uuid2, 0 otherwise.
|
|
112
|
-
*/
|
|
113
|
-
static compare(uuid1: UUID, uuid2: UUID): number;
|
|
114
|
-
/** Raw byte representation of the UUID. */
|
|
115
|
-
protected readonly bytes: Uint8Array;
|
|
116
|
-
private _str?;
|
|
117
|
-
private _hex?;
|
|
118
|
-
/**
|
|
119
|
-
* Constructs a new {@link UUID} instance from any supported input type.
|
|
120
|
-
* @param input - The value to parse (string or ArrayBufferView).
|
|
121
|
-
*/
|
|
122
|
-
constructor(input: string | ArrayBufferView);
|
|
123
|
-
/** Instance wrapper for {@link equals}. */
|
|
124
|
-
equals(...uuids: UUID[]): boolean;
|
|
125
|
-
/** Instance wrapper for {@link compare}. */
|
|
126
|
-
compare(other: UUID): number;
|
|
127
|
-
/**
|
|
128
|
-
* Returns the RFC 4122 string representation of this UUID.
|
|
129
|
-
* @returns The formatted UUID string.
|
|
130
|
-
*/
|
|
131
|
-
toString(): string;
|
|
132
|
-
/**
|
|
133
|
-
* Returns the plain hexadecimal representation of this UUID.
|
|
134
|
-
* @returns A 32‑character hex string.
|
|
135
|
-
*/
|
|
136
|
-
toHex(): string;
|
|
137
|
-
/**
|
|
138
|
-
* Returns a copy of the raw byte array.
|
|
139
|
-
* @returns A new Uint8Array containing the UUID bytes.
|
|
140
|
-
*/
|
|
141
|
-
toBytes(): Uint8Array;
|
|
142
|
-
/** JSON serialization helper – returns the RFC 4122 string. */
|
|
143
|
-
toJSON(): string;
|
|
1
|
+
import { UUID as BaseUUID } from "../../shared/utils/UUID";
|
|
2
|
+
export declare class UUID extends BaseUUID {
|
|
144
3
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UuidInput } from "./UUID.types";
|
|
1
2
|
/**
|
|
2
3
|
* Represents a UUID (Universally Unique Identifier) and provides various utility
|
|
3
4
|
* methods for validation, conversion, comparison and creation.
|
|
@@ -37,7 +38,7 @@ export declare class UUID {
|
|
|
37
38
|
* @param input - The value to validate.
|
|
38
39
|
* @returns true if the input is a valid representation of a UUID.
|
|
39
40
|
*/
|
|
40
|
-
static isValid(input:
|
|
41
|
+
static isValid(input: UuidInput): boolean;
|
|
41
42
|
/**
|
|
42
43
|
* Inserts hyphens into a 32‑character hex string to produce an RFC 4122 string.
|
|
43
44
|
* @param hex - The plain hexadecimal UUID.
|
|
@@ -69,19 +70,19 @@ export declare class UUID {
|
|
|
69
70
|
* @param hex - The hex representation of the UUID.
|
|
70
71
|
* @returns A new {@link UUID} object.
|
|
71
72
|
*/
|
|
72
|
-
static fromHex(hex: string):
|
|
73
|
+
static fromHex<T extends typeof UUID>(hex: string): InstanceType<T>;
|
|
73
74
|
/**
|
|
74
75
|
* Creates a UUID instance from an RFC 4122 formatted string.
|
|
75
76
|
* @param str - The UUID string with hyphens.
|
|
76
77
|
* @returns A new {@link UUID} object.
|
|
77
78
|
*/
|
|
78
|
-
static fromString(str: string):
|
|
79
|
+
static fromString<T extends typeof UUID>(str: string): InstanceType<T>;
|
|
79
80
|
/**
|
|
80
81
|
* Creates a UUID instance from raw bytes.
|
|
81
82
|
* @param bytes - An ArrayBufferView containing 16 bytes.
|
|
82
83
|
* @returns A new {@link UUID} object.
|
|
83
84
|
*/
|
|
84
|
-
static fromBytes(bytes: ArrayBufferView):
|
|
85
|
+
static fromBytes<T extends typeof UUID>(bytes: ArrayBufferView): InstanceType<T>;
|
|
85
86
|
/**
|
|
86
87
|
* Parses any supported input (string or bytes) and returns the raw byte array.
|
|
87
88
|
* @param input - The value to parse.
|
|
@@ -93,11 +94,11 @@ export declare class UUID {
|
|
|
93
94
|
* @param input - The value to parse.
|
|
94
95
|
* @returns A new {@link UUID} object.
|
|
95
96
|
*/
|
|
96
|
-
static from(
|
|
97
|
+
static from<T extends typeof UUID>(this: T, input: UuidInput): InstanceType<T>;
|
|
97
98
|
/** Returns the nil (all zero) UUID. */
|
|
98
|
-
static nil():
|
|
99
|
+
static nil<T extends typeof UUID>(this: T): InstanceType<T>;
|
|
99
100
|
/** Returns a UUID consisting of all 0xFF bytes. */
|
|
100
|
-
static max():
|
|
101
|
+
static max<T extends typeof UUID>(this: T): InstanceType<T>;
|
|
101
102
|
/**
|
|
102
103
|
* Compares multiple UUIDs for strict equality.
|
|
103
104
|
* @param uuids - The UUIDs to compare (at least two required).
|
|
@@ -119,7 +120,7 @@ export declare class UUID {
|
|
|
119
120
|
* Constructs a new {@link UUID} instance from any supported input type.
|
|
120
121
|
* @param input - The value to parse (string or ArrayBufferView).
|
|
121
122
|
*/
|
|
122
|
-
constructor(input:
|
|
123
|
+
constructor(input: UuidInput);
|
|
123
124
|
/** Instance wrapper for {@link equals}. */
|
|
124
125
|
equals(...uuids: UUID[]): boolean;
|
|
125
126
|
/** Instance wrapper for {@link compare}. */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fallbackIfMatch<V, C, F>(value: V, condition: C, fallback: F): V | F;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fallbackIfNullish<T, F>(value: T | null | undefined, fallback: F): T | F;
|
|
@@ -5,8 +5,9 @@ export * from './btoa';
|
|
|
5
5
|
export * from './castArray';
|
|
6
6
|
export * from './circularDistance';
|
|
7
7
|
export * from './sleep';
|
|
8
|
-
export * from './
|
|
8
|
+
export * from './fallbackIfMatch';
|
|
9
9
|
export * from './fallbackIfNull';
|
|
10
|
+
export * from './fallbackIfNullish';
|
|
10
11
|
export * from './fallbackIfUndefined';
|
|
11
12
|
export * from './firstNonNullish';
|
|
12
13
|
export * from './flat';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UUID = void 0;
|
|
4
|
-
const UUID_1 = require("../../
|
|
4
|
+
const UUID_1 = require("../../shared/utils/UUID");
|
|
5
5
|
class UUID extends UUID_1.UUID {
|
|
6
6
|
toBuffer() {
|
|
7
7
|
return Buffer.from(this.bytes);
|