@nakanoaas/uuid58 1.2.1 → 1.2.3
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/README.md +28 -0
- package/dist/index.cjs +262 -0
- package/dist/index.d.cts +152 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +252 -0
- package/package.json +42 -15
- package/esm/alphabet.d.ts +0 -24
- package/esm/alphabet.d.ts.map +0 -1
- package/esm/alphabet.js +0 -23
- package/esm/check.d.ts +0 -40
- package/esm/check.d.ts.map +0 -1
- package/esm/check.js +0 -43
- package/esm/decode.d.ts +0 -37
- package/esm/decode.d.ts.map +0 -1
- package/esm/decode.js +0 -78
- package/esm/encode.d.ts +0 -49
- package/esm/encode.d.ts.map +0 -1
- package/esm/encode.js +0 -78
- package/esm/generate.d.ts +0 -15
- package/esm/generate.d.ts.map +0 -1
- package/esm/generate.js +0 -26
- package/esm/index.d.ts +0 -6
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -5
- package/esm/package.json +0 -3
- package/script/alphabet.d.ts +0 -24
- package/script/alphabet.d.ts.map +0 -1
- package/script/alphabet.js +0 -26
- package/script/check.d.ts +0 -40
- package/script/check.d.ts.map +0 -1
- package/script/check.js +0 -47
- package/script/decode.d.ts +0 -37
- package/script/decode.d.ts.map +0 -1
- package/script/decode.js +0 -84
- package/script/encode.d.ts +0 -49
- package/script/encode.d.ts.map +0 -1
- package/script/encode.js +0 -84
- package/script/generate.d.ts +0 -15
- package/script/generate.d.ts.map +0 -1
- package/script/generate.js +0 -29
- package/script/index.d.ts +0 -6
- package/script/index.d.ts.map +0 -1
- package/script/index.js +0 -23
- package/script/package.json +0 -3
package/README.md
CHANGED
|
@@ -272,6 +272,34 @@ characters. The Base58 alphabet used in this library is the same as Bitcoin's,
|
|
|
272
272
|
which excludes similar-looking characters (0, O, I, l) to prevent visual
|
|
273
273
|
ambiguity and confusion.
|
|
274
274
|
|
|
275
|
+
## Development
|
|
276
|
+
|
|
277
|
+
This project uses [Vite+](https://viteplus.dev/) (`vp`) and pnpm.
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
pnpm install
|
|
281
|
+
pnpm check # format, lint and type check
|
|
282
|
+
pnpm test # run tests
|
|
283
|
+
pnpm bench # build and benchmark (see bench/README.md)
|
|
284
|
+
pnpm build # build the npm package into dist/
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
The version lives only in `package.json`. `jsr.json` is generated from it
|
|
288
|
+
by `vp pack` and is not committed.
|
|
289
|
+
|
|
290
|
+
### Releasing
|
|
291
|
+
|
|
292
|
+
1. Bump `version` in `package.json` and merge the change into `main`.
|
|
293
|
+
2. Tag that commit and push the tag:
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
git tag v1.2.3 && git push origin v1.2.3
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
The `Publish` workflow then checks that the tag matches `package.json` and
|
|
300
|
+
is on `main`, runs the tests, publishes to JSR and npm, and creates the
|
|
301
|
+
GitHub Release.
|
|
302
|
+
|
|
275
303
|
## License
|
|
276
304
|
|
|
277
305
|
MIT License - see the [LICENSE](LICENSE) file for details
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// alphabet.ts
|
|
3
|
+
/**
|
|
4
|
+
* Character set for Base58 encoding (Bitcoin compatible).
|
|
5
|
+
*
|
|
6
|
+
* This alphabet excludes characters that can be easily confused:
|
|
7
|
+
* - `0` (zero) and `O` (capital O)
|
|
8
|
+
* - `I` (capital i) and `l` (lowercase L)
|
|
9
|
+
*
|
|
10
|
+
* The alphabet consists of 58 characters: digits 1-9 and letters A-Z, a-z
|
|
11
|
+
* (excluding the ambiguous characters mentioned above).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const char = UUID58_ALPHABET[0]; // "1"
|
|
16
|
+
* const char = UUID58_ALPHABET[9]; // "A"
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
const UUID58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
20
|
+
// check.ts
|
|
21
|
+
const MAX_UUID58 = "YcVfxkQb6JRzqk5kF2tNLv";
|
|
22
|
+
/**
|
|
23
|
+
* Regular expression pattern for validating UUID58 strings.
|
|
24
|
+
*
|
|
25
|
+
* This regex ensures that a string:
|
|
26
|
+
* - Contains exactly 22 characters
|
|
27
|
+
* - Uses only characters from the Base58 alphabet
|
|
28
|
+
*
|
|
29
|
+
* **Note:** This regex does NOT validate that the decoded value fits within
|
|
30
|
+
* 128 bits (UUID size). For complete validation including 128-bit range
|
|
31
|
+
* checking, use {@link isUuid58} instead.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
|
|
36
|
+
* const isValid = UUID58_REGEX.test("invalid"); // false
|
|
37
|
+
* const isValid = UUID58_REGEX.test("123"); // false (too short)
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const UUID58_REGEX = /^[1-9A-HJ-NP-Za-km-z]{22}$/u;
|
|
41
|
+
/**
|
|
42
|
+
* Checks if a given string is a valid UUID58-encodable string.
|
|
43
|
+
* This function efficiently validates without performing full decoding.
|
|
44
|
+
*
|
|
45
|
+
* A valid UUID58 string must:
|
|
46
|
+
* - Be exactly 22 characters long
|
|
47
|
+
* - Contain only characters from the Base58 alphabet
|
|
48
|
+
* - Decode to a value that fits within 128 bits (UUID size)
|
|
49
|
+
*
|
|
50
|
+
* @param uuid58 - The string to check
|
|
51
|
+
* @returns `true` if the string is a valid UUID58 string, `false` otherwise
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
|
|
56
|
+
* isUuid58("invalid"); // false
|
|
57
|
+
* isUuid58("O0lI"); // false (contains invalid characters)
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
function isUuid58(uuid58) {
|
|
61
|
+
return UUID58_REGEX.test(uuid58) && uuid58 <= MAX_UUID58;
|
|
62
|
+
}
|
|
63
|
+
// internal.ts
|
|
64
|
+
const MAX_PREVIEW_LENGTH = 40;
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
* 58^9, the largest power of 58 below 2^53. A 9-digit Base58 chunk therefore
|
|
68
|
+
* fits in a Number, so most digit arithmetic can avoid slow BigInt division.
|
|
69
|
+
*/
|
|
70
|
+
const BASE58_CHUNK = 7427658739644928n;
|
|
71
|
+
/** Appends `digits` Base58 digits of `value` (most significant first). */
|
|
72
|
+
function appendDigits(out, value, digits) {
|
|
73
|
+
let chunk = "";
|
|
74
|
+
for (let i = 0; i < digits; i++) {
|
|
75
|
+
const quotient = Math.floor(value / 58);
|
|
76
|
+
chunk = UUID58_ALPHABET[value - quotient * 58] + chunk;
|
|
77
|
+
value = quotient;
|
|
78
|
+
}
|
|
79
|
+
return out + chunk;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @internal
|
|
83
|
+
* Encodes an unsigned 128-bit integer as a 22-character Base58 string.
|
|
84
|
+
*
|
|
85
|
+
* The value is split into 4 + 9 + 9 digit chunks with two BigInt divisions,
|
|
86
|
+
* instead of 22 BigInt divisions (one per digit).
|
|
87
|
+
*/
|
|
88
|
+
function encodeUint128(num) {
|
|
89
|
+
const low = Number(num % BASE58_CHUNK);
|
|
90
|
+
num /= BASE58_CHUNK;
|
|
91
|
+
const mid = Number(num % BASE58_CHUNK);
|
|
92
|
+
return appendDigits(appendDigits(appendDigits("", Number(num / BASE58_CHUNK), 4), mid, 9), low, 9);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* @internal
|
|
96
|
+
* Formats untrusted input for inclusion in an error message.
|
|
97
|
+
*
|
|
98
|
+
* The input is truncated so huge strings do not bloat error messages or logs,
|
|
99
|
+
* and JSON-escaped so control characters (e.g. newlines) cannot forge log
|
|
100
|
+
* lines.
|
|
101
|
+
*/
|
|
102
|
+
function previewInput(input) {
|
|
103
|
+
return input.length > MAX_PREVIEW_LENGTH ? `${JSON.stringify(input.slice(0, MAX_PREVIEW_LENGTH))}... (${input.length} characters)` : JSON.stringify(input);
|
|
104
|
+
}
|
|
105
|
+
// decode.ts
|
|
106
|
+
var _Uuid58DecodeError;
|
|
107
|
+
const BASE58_INDEX = (/* @__PURE__ */ new Int8Array(128)).fill(-1);
|
|
108
|
+
for (let i = 0; i < 58; i++) BASE58_INDEX[UUID58_ALPHABET.charCodeAt(i)] = i;
|
|
109
|
+
/**
|
|
110
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
111
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
112
|
+
*/
|
|
113
|
+
var Uuid58DecodeError = class extends Error {};
|
|
114
|
+
_Uuid58DecodeError = Uuid58DecodeError;
|
|
115
|
+
_Uuid58DecodeError.prototype.name = "Uuid58DecodeError";
|
|
116
|
+
/**
|
|
117
|
+
* Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
|
|
118
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
119
|
+
* instance.
|
|
120
|
+
*
|
|
121
|
+
* @param uuid58 - The 22-character Base58-encoded UUID string to decode
|
|
122
|
+
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
123
|
+
* `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
|
|
124
|
+
* @note This function does not throw; it returns the error object instead.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
129
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
function uuid58DecodeSafe(uuid58) {
|
|
133
|
+
if (uuid58.length !== 22) return new Uuid58DecodeError(`Expected Base58 string of length 22, but received string of length ${uuid58.length}: ${previewInput(uuid58)}`);
|
|
134
|
+
let num = 0n;
|
|
135
|
+
for (let start = 0; start < 22;) {
|
|
136
|
+
const end = start === 0 ? 4 : start + 9;
|
|
137
|
+
let chunk = 0;
|
|
138
|
+
for (let i = start; i < end; i++) {
|
|
139
|
+
const code = uuid58.charCodeAt(i);
|
|
140
|
+
const index = code < 128 ? BASE58_INDEX[code] : -1;
|
|
141
|
+
if (index < 0) {
|
|
142
|
+
const char = String.fromCodePoint(uuid58.codePointAt(i));
|
|
143
|
+
return new Uuid58DecodeError(`Invalid Base58 character ${JSON.stringify(char)} found in input: ${previewInput(uuid58)}`);
|
|
144
|
+
}
|
|
145
|
+
chunk = chunk * 58 + index;
|
|
146
|
+
}
|
|
147
|
+
num = num * BASE58_CHUNK + BigInt(chunk);
|
|
148
|
+
start = end;
|
|
149
|
+
}
|
|
150
|
+
const hex = num.toString(16).padStart(32, "0");
|
|
151
|
+
if (hex.length !== 32) return new Uuid58DecodeError(`Decoded value exceeds 128 bits of UUID: ${previewInput(uuid58)}`);
|
|
152
|
+
return hex.slice(0, 8) + "-" + hex.slice(8, 12) + "-" + hex.slice(12, 16) + "-" + hex.slice(16, 20) + "-" + hex.slice(20);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Converts a 22-character Base58-encoded string back to a standard UUID string format.
|
|
156
|
+
*
|
|
157
|
+
* @param uuid58 - The 22-character Base58-encoded string to decode
|
|
158
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
159
|
+
* @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
function uuid58Decode(uuid58) {
|
|
167
|
+
const result = uuid58DecodeSafe(uuid58);
|
|
168
|
+
if (result instanceof Uuid58DecodeError) throw result;
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
// encode.ts
|
|
172
|
+
var _Uuid58EncodeError;
|
|
173
|
+
const HEX32_REGEX = /^[0-9a-f]{32}$/i;
|
|
174
|
+
/**
|
|
175
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
176
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
177
|
+
*/
|
|
178
|
+
var Uuid58EncodeError = class extends Error {};
|
|
179
|
+
_Uuid58EncodeError = Uuid58EncodeError;
|
|
180
|
+
_Uuid58EncodeError.prototype.name = "Uuid58EncodeError";
|
|
181
|
+
/**
|
|
182
|
+
* Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
|
|
183
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
184
|
+
*
|
|
185
|
+
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
186
|
+
* @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
187
|
+
* not a valid UUID
|
|
188
|
+
* @note This function does not throw; it returns the error object instead.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* // With hyphens
|
|
193
|
+
* const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
194
|
+
* // Without hyphens
|
|
195
|
+
* const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
196
|
+
* // Uppercase UUID is also accepted
|
|
197
|
+
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
198
|
+
* // Invalid UUID format
|
|
199
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
function uuid58EncodeSafe(uuid) {
|
|
203
|
+
const hex = uuid.replace(/-/g, "");
|
|
204
|
+
if (hex.length !== 32) return new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in ${previewInput(uuid)}`);
|
|
205
|
+
if (!HEX32_REGEX.test(hex)) return new Uuid58EncodeError(`Invalid UUID format: ${previewInput(uuid)} contains non-hexadecimal characters`);
|
|
206
|
+
return encodeUint128(BigInt("0x" + hex));
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Converts a standard UUID string to a 22-character Base58-encoded format.
|
|
210
|
+
*
|
|
211
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
212
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
213
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
214
|
+
* @returns A 22-character Base58-encoded string representation of the UUID
|
|
215
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* // With hyphens
|
|
220
|
+
* const base58 = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
221
|
+
* // Without hyphens
|
|
222
|
+
* const base58 = uuid58Encode("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
223
|
+
* // Uppercase UUID is also accepted
|
|
224
|
+
* const base58 = uuid58Encode("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
function uuid58Encode(uuid) {
|
|
228
|
+
const result = uuid58EncodeSafe(uuid);
|
|
229
|
+
if (result instanceof Uuid58EncodeError) throw result;
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
// generate.ts
|
|
233
|
+
const random = /* @__PURE__ */ new BigUint64Array(2);
|
|
234
|
+
/**
|
|
235
|
+
* Generates a new Base58-encoded UUID (always 22 characters).
|
|
236
|
+
*
|
|
237
|
+
* This function combines the standard UUID generation (using crypto.getRandomValues())
|
|
238
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
239
|
+
*
|
|
240
|
+
* @returns A 22-character Base58-encoded string representing a newly generated UUID
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
function uuid58() {
|
|
248
|
+
crypto.getRandomValues(random);
|
|
249
|
+
const high = random[0] & 18446744073709490175n | 16384n;
|
|
250
|
+
const low = random[1] & 4611686018427387903n | 9223372036854775808n;
|
|
251
|
+
return encodeUint128(high << 64n | low);
|
|
252
|
+
}
|
|
253
|
+
exports.UUID58_ALPHABET = UUID58_ALPHABET;
|
|
254
|
+
exports.UUID58_REGEX = UUID58_REGEX;
|
|
255
|
+
exports.Uuid58DecodeError = Uuid58DecodeError;
|
|
256
|
+
exports.Uuid58EncodeError = Uuid58EncodeError;
|
|
257
|
+
exports.isUuid58 = isUuid58;
|
|
258
|
+
exports.uuid58 = uuid58;
|
|
259
|
+
exports.uuid58Decode = uuid58Decode;
|
|
260
|
+
exports.uuid58DecodeSafe = uuid58DecodeSafe;
|
|
261
|
+
exports.uuid58Encode = uuid58Encode;
|
|
262
|
+
exports.uuid58EncodeSafe = uuid58EncodeSafe;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character set for Base58 encoding (Bitcoin compatible).
|
|
3
|
+
*
|
|
4
|
+
* This alphabet excludes characters that can be easily confused:
|
|
5
|
+
* - `0` (zero) and `O` (capital O)
|
|
6
|
+
* - `I` (capital i) and `l` (lowercase L)
|
|
7
|
+
*
|
|
8
|
+
* The alphabet consists of 58 characters: digits 1-9 and letters A-Z, a-z
|
|
9
|
+
* (excluding the ambiguous characters mentioned above).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const char = UUID58_ALPHABET[0]; // "1"
|
|
14
|
+
* const char = UUID58_ALPHABET[9]; // "A"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const UUID58_ALPHABET: string;
|
|
18
|
+
/**
|
|
19
|
+
* Regular expression pattern for validating UUID58 strings.
|
|
20
|
+
*
|
|
21
|
+
* This regex ensures that a string:
|
|
22
|
+
* - Contains exactly 22 characters
|
|
23
|
+
* - Uses only characters from the Base58 alphabet
|
|
24
|
+
*
|
|
25
|
+
* **Note:** This regex does NOT validate that the decoded value fits within
|
|
26
|
+
* 128 bits (UUID size). For complete validation including 128-bit range
|
|
27
|
+
* checking, use {@link isUuid58} instead.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
|
|
32
|
+
* const isValid = UUID58_REGEX.test("invalid"); // false
|
|
33
|
+
* const isValid = UUID58_REGEX.test("123"); // false (too short)
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const UUID58_REGEX: RegExp;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a given string is a valid UUID58-encodable string.
|
|
39
|
+
* This function efficiently validates without performing full decoding.
|
|
40
|
+
*
|
|
41
|
+
* A valid UUID58 string must:
|
|
42
|
+
* - Be exactly 22 characters long
|
|
43
|
+
* - Contain only characters from the Base58 alphabet
|
|
44
|
+
* - Decode to a value that fits within 128 bits (UUID size)
|
|
45
|
+
*
|
|
46
|
+
* @param uuid58 - The string to check
|
|
47
|
+
* @returns `true` if the string is a valid UUID58 string, `false` otherwise
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
|
|
52
|
+
* isUuid58("invalid"); // false
|
|
53
|
+
* isUuid58("O0lI"); // false (contains invalid characters)
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function isUuid58(uuid58: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
59
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
60
|
+
*/
|
|
61
|
+
export declare class Uuid58DecodeError extends Error {}
|
|
62
|
+
/**
|
|
63
|
+
* Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
|
|
64
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
65
|
+
* instance.
|
|
66
|
+
*
|
|
67
|
+
* @param uuid58 - The 22-character Base58-encoded UUID string to decode
|
|
68
|
+
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
69
|
+
* `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
|
|
70
|
+
* @note This function does not throw; it returns the error object instead.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
75
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
|
|
79
|
+
/**
|
|
80
|
+
* Converts a 22-character Base58-encoded string back to a standard UUID string format.
|
|
81
|
+
*
|
|
82
|
+
* @param uuid58 - The 22-character Base58-encoded string to decode
|
|
83
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
84
|
+
* @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function uuid58Decode(uuid58: string): string;
|
|
92
|
+
/**
|
|
93
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
94
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
95
|
+
*/
|
|
96
|
+
export declare class Uuid58EncodeError extends Error {}
|
|
97
|
+
/**
|
|
98
|
+
* Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
|
|
99
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
100
|
+
*
|
|
101
|
+
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
102
|
+
* @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
103
|
+
* not a valid UUID
|
|
104
|
+
* @note This function does not throw; it returns the error object instead.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // With hyphens
|
|
109
|
+
* const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
110
|
+
* // Without hyphens
|
|
111
|
+
* const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
112
|
+
* // Uppercase UUID is also accepted
|
|
113
|
+
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
114
|
+
* // Invalid UUID format
|
|
115
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
|
|
119
|
+
/**
|
|
120
|
+
* Converts a standard UUID string to a 22-character Base58-encoded format.
|
|
121
|
+
*
|
|
122
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
123
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
124
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
125
|
+
* @returns A 22-character Base58-encoded string representation of the UUID
|
|
126
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* // With hyphens
|
|
131
|
+
* const base58 = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
132
|
+
* // Without hyphens
|
|
133
|
+
* const base58 = uuid58Encode("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
134
|
+
* // Uppercase UUID is also accepted
|
|
135
|
+
* const base58 = uuid58Encode("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function uuid58Encode(uuid: string): string;
|
|
139
|
+
/**
|
|
140
|
+
* Generates a new Base58-encoded UUID (always 22 characters).
|
|
141
|
+
*
|
|
142
|
+
* This function combines the standard UUID generation (using crypto.getRandomValues())
|
|
143
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
144
|
+
*
|
|
145
|
+
* @returns A 22-character Base58-encoded string representing a newly generated UUID
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function uuid58(): string;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character set for Base58 encoding (Bitcoin compatible).
|
|
3
|
+
*
|
|
4
|
+
* This alphabet excludes characters that can be easily confused:
|
|
5
|
+
* - `0` (zero) and `O` (capital O)
|
|
6
|
+
* - `I` (capital i) and `l` (lowercase L)
|
|
7
|
+
*
|
|
8
|
+
* The alphabet consists of 58 characters: digits 1-9 and letters A-Z, a-z
|
|
9
|
+
* (excluding the ambiguous characters mentioned above).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const char = UUID58_ALPHABET[0]; // "1"
|
|
14
|
+
* const char = UUID58_ALPHABET[9]; // "A"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const UUID58_ALPHABET: string;
|
|
18
|
+
/**
|
|
19
|
+
* Regular expression pattern for validating UUID58 strings.
|
|
20
|
+
*
|
|
21
|
+
* This regex ensures that a string:
|
|
22
|
+
* - Contains exactly 22 characters
|
|
23
|
+
* - Uses only characters from the Base58 alphabet
|
|
24
|
+
*
|
|
25
|
+
* **Note:** This regex does NOT validate that the decoded value fits within
|
|
26
|
+
* 128 bits (UUID size). For complete validation including 128-bit range
|
|
27
|
+
* checking, use {@link isUuid58} instead.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
|
|
32
|
+
* const isValid = UUID58_REGEX.test("invalid"); // false
|
|
33
|
+
* const isValid = UUID58_REGEX.test("123"); // false (too short)
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const UUID58_REGEX: RegExp;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a given string is a valid UUID58-encodable string.
|
|
39
|
+
* This function efficiently validates without performing full decoding.
|
|
40
|
+
*
|
|
41
|
+
* A valid UUID58 string must:
|
|
42
|
+
* - Be exactly 22 characters long
|
|
43
|
+
* - Contain only characters from the Base58 alphabet
|
|
44
|
+
* - Decode to a value that fits within 128 bits (UUID size)
|
|
45
|
+
*
|
|
46
|
+
* @param uuid58 - The string to check
|
|
47
|
+
* @returns `true` if the string is a valid UUID58 string, `false` otherwise
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
|
|
52
|
+
* isUuid58("invalid"); // false
|
|
53
|
+
* isUuid58("O0lI"); // false (contains invalid characters)
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function isUuid58(uuid58: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
59
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
60
|
+
*/
|
|
61
|
+
export declare class Uuid58DecodeError extends Error {}
|
|
62
|
+
/**
|
|
63
|
+
* Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
|
|
64
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
65
|
+
* instance.
|
|
66
|
+
*
|
|
67
|
+
* @param uuid58 - The 22-character Base58-encoded UUID string to decode
|
|
68
|
+
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
69
|
+
* `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
|
|
70
|
+
* @note This function does not throw; it returns the error object instead.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
75
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
|
|
79
|
+
/**
|
|
80
|
+
* Converts a 22-character Base58-encoded string back to a standard UUID string format.
|
|
81
|
+
*
|
|
82
|
+
* @param uuid58 - The 22-character Base58-encoded string to decode
|
|
83
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
84
|
+
* @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function uuid58Decode(uuid58: string): string;
|
|
92
|
+
/**
|
|
93
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
94
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
95
|
+
*/
|
|
96
|
+
export declare class Uuid58EncodeError extends Error {}
|
|
97
|
+
/**
|
|
98
|
+
* Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
|
|
99
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
100
|
+
*
|
|
101
|
+
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
102
|
+
* @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
103
|
+
* not a valid UUID
|
|
104
|
+
* @note This function does not throw; it returns the error object instead.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // With hyphens
|
|
109
|
+
* const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
110
|
+
* // Without hyphens
|
|
111
|
+
* const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
112
|
+
* // Uppercase UUID is also accepted
|
|
113
|
+
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
114
|
+
* // Invalid UUID format
|
|
115
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
|
|
119
|
+
/**
|
|
120
|
+
* Converts a standard UUID string to a 22-character Base58-encoded format.
|
|
121
|
+
*
|
|
122
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
123
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
124
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
125
|
+
* @returns A 22-character Base58-encoded string representation of the UUID
|
|
126
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* // With hyphens
|
|
131
|
+
* const base58 = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
132
|
+
* // Without hyphens
|
|
133
|
+
* const base58 = uuid58Encode("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
134
|
+
* // Uppercase UUID is also accepted
|
|
135
|
+
* const base58 = uuid58Encode("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function uuid58Encode(uuid: string): string;
|
|
139
|
+
/**
|
|
140
|
+
* Generates a new Base58-encoded UUID (always 22 characters).
|
|
141
|
+
*
|
|
142
|
+
* This function combines the standard UUID generation (using crypto.getRandomValues())
|
|
143
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
144
|
+
*
|
|
145
|
+
* @returns A 22-character Base58-encoded string representing a newly generated UUID
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function uuid58(): string;
|