@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/dist/index.js ADDED
@@ -0,0 +1,252 @@
1
+ // alphabet.ts
2
+ /**
3
+ * Character set for Base58 encoding (Bitcoin compatible).
4
+ *
5
+ * This alphabet excludes characters that can be easily confused:
6
+ * - `0` (zero) and `O` (capital O)
7
+ * - `I` (capital i) and `l` (lowercase L)
8
+ *
9
+ * The alphabet consists of 58 characters: digits 1-9 and letters A-Z, a-z
10
+ * (excluding the ambiguous characters mentioned above).
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const char = UUID58_ALPHABET[0]; // "1"
15
+ * const char = UUID58_ALPHABET[9]; // "A"
16
+ * ```
17
+ */
18
+ const UUID58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
19
+ // check.ts
20
+ const MAX_UUID58 = "YcVfxkQb6JRzqk5kF2tNLv";
21
+ /**
22
+ * Regular expression pattern for validating UUID58 strings.
23
+ *
24
+ * This regex ensures that a string:
25
+ * - Contains exactly 22 characters
26
+ * - Uses only characters from the Base58 alphabet
27
+ *
28
+ * **Note:** This regex does NOT validate that the decoded value fits within
29
+ * 128 bits (UUID size). For complete validation including 128-bit range
30
+ * checking, use {@link isUuid58} instead.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
35
+ * const isValid = UUID58_REGEX.test("invalid"); // false
36
+ * const isValid = UUID58_REGEX.test("123"); // false (too short)
37
+ * ```
38
+ */
39
+ const UUID58_REGEX = /^[1-9A-HJ-NP-Za-km-z]{22}$/u;
40
+ /**
41
+ * Checks if a given string is a valid UUID58-encodable string.
42
+ * This function efficiently validates without performing full decoding.
43
+ *
44
+ * A valid UUID58 string must:
45
+ * - Be exactly 22 characters long
46
+ * - Contain only characters from the Base58 alphabet
47
+ * - Decode to a value that fits within 128 bits (UUID size)
48
+ *
49
+ * @param uuid58 - The string to check
50
+ * @returns `true` if the string is a valid UUID58 string, `false` otherwise
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
55
+ * isUuid58("invalid"); // false
56
+ * isUuid58("O0lI"); // false (contains invalid characters)
57
+ * ```
58
+ */
59
+ function isUuid58(uuid58) {
60
+ return UUID58_REGEX.test(uuid58) && uuid58 <= MAX_UUID58;
61
+ }
62
+ // internal.ts
63
+ const MAX_PREVIEW_LENGTH = 40;
64
+ /**
65
+ * @internal
66
+ * 58^9, the largest power of 58 below 2^53. A 9-digit Base58 chunk therefore
67
+ * fits in a Number, so most digit arithmetic can avoid slow BigInt division.
68
+ */
69
+ const BASE58_CHUNK = 7427658739644928n;
70
+ /** Appends `digits` Base58 digits of `value` (most significant first). */
71
+ function appendDigits(out, value, digits) {
72
+ let chunk = "";
73
+ for (let i = 0; i < digits; i++) {
74
+ const quotient = Math.floor(value / 58);
75
+ chunk = UUID58_ALPHABET[value - quotient * 58] + chunk;
76
+ value = quotient;
77
+ }
78
+ return out + chunk;
79
+ }
80
+ /**
81
+ * @internal
82
+ * Encodes an unsigned 128-bit integer as a 22-character Base58 string.
83
+ *
84
+ * The value is split into 4 + 9 + 9 digit chunks with two BigInt divisions,
85
+ * instead of 22 BigInt divisions (one per digit).
86
+ */
87
+ function encodeUint128(num) {
88
+ const low = Number(num % BASE58_CHUNK);
89
+ num /= BASE58_CHUNK;
90
+ const mid = Number(num % BASE58_CHUNK);
91
+ return appendDigits(appendDigits(appendDigits("", Number(num / BASE58_CHUNK), 4), mid, 9), low, 9);
92
+ }
93
+ /**
94
+ * @internal
95
+ * Formats untrusted input for inclusion in an error message.
96
+ *
97
+ * The input is truncated so huge strings do not bloat error messages or logs,
98
+ * and JSON-escaped so control characters (e.g. newlines) cannot forge log
99
+ * lines.
100
+ */
101
+ function previewInput(input) {
102
+ return input.length > MAX_PREVIEW_LENGTH ? `${JSON.stringify(input.slice(0, MAX_PREVIEW_LENGTH))}... (${input.length} characters)` : JSON.stringify(input);
103
+ }
104
+ // decode.ts
105
+ var _Uuid58DecodeError;
106
+ const BASE58_INDEX = (/* @__PURE__ */ new Int8Array(128)).fill(-1);
107
+ for (let i = 0; i < 58; i++) BASE58_INDEX[UUID58_ALPHABET.charCodeAt(i)] = i;
108
+ /**
109
+ * Error thrown when an invalid Base58 string is provided for decoding.
110
+ * This includes strings containing characters not in the Base58 alphabet.
111
+ */
112
+ var Uuid58DecodeError = class extends Error {};
113
+ _Uuid58DecodeError = Uuid58DecodeError;
114
+ _Uuid58DecodeError.prototype.name = "Uuid58DecodeError";
115
+ /**
116
+ * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
117
+ * throwing an error for invalid input, it returns an `Uuid58DecodeError`
118
+ * instance.
119
+ *
120
+ * @param uuid58 - The 22-character Base58-encoded UUID string to decode
121
+ * @returns A standard UUID string (lowercase, with hyphens), or an
122
+ * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
123
+ * @note This function does not throw; it returns the error object instead.
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
128
+ * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
129
+ * ```
130
+ */
131
+ function uuid58DecodeSafe(uuid58) {
132
+ if (uuid58.length !== 22) return new Uuid58DecodeError(`Expected Base58 string of length 22, but received string of length ${uuid58.length}: ${previewInput(uuid58)}`);
133
+ let num = 0n;
134
+ for (let start = 0; start < 22;) {
135
+ const end = start === 0 ? 4 : start + 9;
136
+ let chunk = 0;
137
+ for (let i = start; i < end; i++) {
138
+ const code = uuid58.charCodeAt(i);
139
+ const index = code < 128 ? BASE58_INDEX[code] : -1;
140
+ if (index < 0) {
141
+ const char = String.fromCodePoint(uuid58.codePointAt(i));
142
+ return new Uuid58DecodeError(`Invalid Base58 character ${JSON.stringify(char)} found in input: ${previewInput(uuid58)}`);
143
+ }
144
+ chunk = chunk * 58 + index;
145
+ }
146
+ num = num * BASE58_CHUNK + BigInt(chunk);
147
+ start = end;
148
+ }
149
+ const hex = num.toString(16).padStart(32, "0");
150
+ if (hex.length !== 32) return new Uuid58DecodeError(`Decoded value exceeds 128 bits of UUID: ${previewInput(uuid58)}`);
151
+ return hex.slice(0, 8) + "-" + hex.slice(8, 12) + "-" + hex.slice(12, 16) + "-" + hex.slice(16, 20) + "-" + hex.slice(20);
152
+ }
153
+ /**
154
+ * Converts a 22-character Base58-encoded string back to a standard UUID string format.
155
+ *
156
+ * @param uuid58 - The 22-character Base58-encoded string to decode
157
+ * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
158
+ * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
163
+ * ```
164
+ */
165
+ function uuid58Decode(uuid58) {
166
+ const result = uuid58DecodeSafe(uuid58);
167
+ if (result instanceof Uuid58DecodeError) throw result;
168
+ return result;
169
+ }
170
+ // encode.ts
171
+ var _Uuid58EncodeError;
172
+ const HEX32_REGEX = /^[0-9a-f]{32}$/i;
173
+ /**
174
+ * Error thrown when an invalid UUID string is provided for encoding.
175
+ * This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
176
+ */
177
+ var Uuid58EncodeError = class extends Error {};
178
+ _Uuid58EncodeError = Uuid58EncodeError;
179
+ _Uuid58EncodeError.prototype.name = "Uuid58EncodeError";
180
+ /**
181
+ * Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
182
+ * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
183
+ *
184
+ * @param uuid - The UUID string to encode (with or without hyphens)
185
+ * @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
186
+ * not a valid UUID
187
+ * @note This function does not throw; it returns the error object instead.
188
+ *
189
+ * @example
190
+ * ```typescript
191
+ * // With hyphens
192
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
193
+ * // Without hyphens
194
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
195
+ * // Uppercase UUID is also accepted
196
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
197
+ * // Invalid UUID format
198
+ * const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
199
+ * ```
200
+ */
201
+ function uuid58EncodeSafe(uuid) {
202
+ const hex = uuid.replace(/-/g, "");
203
+ if (hex.length !== 32) return new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in ${previewInput(uuid)}`);
204
+ if (!HEX32_REGEX.test(hex)) return new Uuid58EncodeError(`Invalid UUID format: ${previewInput(uuid)} contains non-hexadecimal characters`);
205
+ return encodeUint128(BigInt("0x" + hex));
206
+ }
207
+ /**
208
+ * Converts a standard UUID string to a 22-character Base58-encoded format.
209
+ *
210
+ * @param uuid - The UUID string to encode. Can be provided with or without hyphens
211
+ * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
212
+ * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
213
+ * @returns A 22-character Base58-encoded string representation of the UUID
214
+ * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
215
+ *
216
+ * @example
217
+ * ```typescript
218
+ * // With hyphens
219
+ * const base58 = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
220
+ * // Without hyphens
221
+ * const base58 = uuid58Encode("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
222
+ * // Uppercase UUID is also accepted
223
+ * const base58 = uuid58Encode("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
224
+ * ```
225
+ */
226
+ function uuid58Encode(uuid) {
227
+ const result = uuid58EncodeSafe(uuid);
228
+ if (result instanceof Uuid58EncodeError) throw result;
229
+ return result;
230
+ }
231
+ // generate.ts
232
+ const random = /* @__PURE__ */ new BigUint64Array(2);
233
+ /**
234
+ * Generates a new Base58-encoded UUID (always 22 characters).
235
+ *
236
+ * This function combines the standard UUID generation (using crypto.getRandomValues())
237
+ * with Base58 encoding to create a shorter, URL-safe identifier.
238
+ *
239
+ * @returns A 22-character Base58-encoded string representing a newly generated UUID
240
+ *
241
+ * @example
242
+ * ```typescript
243
+ * const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
244
+ * ```
245
+ */
246
+ function uuid58() {
247
+ crypto.getRandomValues(random);
248
+ const high = random[0] & 18446744073709490175n | 16384n;
249
+ const low = random[1] & 4611686018427387903n | 9223372036854775808n;
250
+ return encodeUint128(high << 64n | low);
251
+ }
252
+ export { UUID58_ALPHABET, UUID58_REGEX, Uuid58DecodeError, Uuid58EncodeError, isUuid58, uuid58, uuid58Decode, uuid58DecodeSafe, uuid58Encode, uuid58EncodeSafe };
package/package.json CHANGED
@@ -1,25 +1,52 @@
1
1
  {
2
2
  "name": "@nakanoaas/uuid58",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "A tiny, zero-dependency library for generating and converting UUIDs to Base58-encoded strings.",
5
+ "bugs": {
6
+ "url": "https://github.com/nakanoasaservice/uuid58/issues"
7
+ },
8
+ "license": "MIT",
5
9
  "repository": {
6
10
  "type": "git",
7
11
  "url": "git+https://github.com/nakanoasaservice/uuid58.git"
8
12
  },
9
- "license": "MIT",
10
- "bugs": {
11
- "url": "https://github.com/nakanoasaservice/uuid58/issues"
12
- },
13
- "main": "./script/index.js",
14
- "module": "./esm/index.js",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "type": "module",
17
+ "sideEffects": false,
18
+ "main": "./dist/index.cjs",
19
+ "module": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
15
21
  "exports": {
16
22
  ".": {
17
- "import": "./esm/index.js",
18
- "require": "./script/index.js"
19
- }
23
+ "import": {
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "require": {
28
+ "types": "./dist/index.d.cts",
29
+ "default": "./dist/index.cjs"
30
+ }
31
+ },
32
+ "./package.json": "./package.json"
20
33
  },
21
- "scripts": {},
22
- "sideEffects": false,
23
- "type": "module",
24
- "_generatedBy": "dnt@dev"
25
- }
34
+ "scripts": {
35
+ "build": "vp pack",
36
+ "check": "vp check",
37
+ "test": "vp test run",
38
+ "bench": "vp pack && vp test bench --run --reporter=default",
39
+ "bench:baseline": "sh bench/save-baseline.sh",
40
+ "jsr:dry-run": "vp pack && jsr publish --dry-run"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^26.6.6",
44
+ "jsr": "^0.14.3",
45
+ "typescript": "^7.0.2",
46
+ "vite-plus": "^1.6.0"
47
+ },
48
+ "engines": {
49
+ "node": ">=20"
50
+ },
51
+ "packageManager": "pnpm@10.34.5"
52
+ }
package/esm/alphabet.d.ts DELETED
@@ -1,24 +0,0 @@
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
- * @internal
20
- * The length of the Base58 alphabet as BigInt for encoding/decoding operations.
21
- * This is exported for internal use but not part of the public API.
22
- */
23
- export declare const UUID58_LENGTH: bigint;
24
- //# sourceMappingURL=alphabet.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"alphabet.d.ts","sourceRoot":"","sources":["../src/alphabet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,eAAe,EAAE,MACgC,CAAC;AAE/D;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,MAAuC,CAAC"}
package/esm/alphabet.js DELETED
@@ -1,23 +0,0 @@
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 const UUID58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
18
- /**
19
- * @internal
20
- * The length of the Base58 alphabet as BigInt for encoding/decoding operations.
21
- * This is exported for internal use but not part of the public API.
22
- */
23
- export const UUID58_LENGTH = BigInt(UUID58_ALPHABET.length);
package/esm/check.d.ts DELETED
@@ -1,40 +0,0 @@
1
- /**
2
- * Regular expression pattern for validating UUID58 strings.
3
- *
4
- * This regex ensures that a string:
5
- * - Contains exactly 22 characters
6
- * - Uses only characters from the Base58 alphabet
7
- *
8
- * **Note:** This regex does NOT validate that the decoded value fits within
9
- * 128 bits (UUID size). For complete validation including 128-bit range
10
- * checking, use {@link isUuid58} instead.
11
- *
12
- * @example
13
- * ```typescript
14
- * const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
15
- * const isValid = UUID58_REGEX.test("invalid"); // false
16
- * const isValid = UUID58_REGEX.test("123"); // false (too short)
17
- * ```
18
- */
19
- export declare const UUID58_REGEX: RegExp;
20
- /**
21
- * Checks if a given string is a valid UUID58-encodable string.
22
- * This function efficiently validates without performing full decoding.
23
- *
24
- * A valid UUID58 string must:
25
- * - Be exactly 22 characters long
26
- * - Contain only characters from the Base58 alphabet
27
- * - Decode to a value that fits within 128 bits (UUID size)
28
- *
29
- * @param uuid58 - The string to check
30
- * @returns `true` if the string is a valid UUID58 string, `false` otherwise
31
- *
32
- * @example
33
- * ```typescript
34
- * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
35
- * isUuid58("invalid"); // false
36
- * isUuid58("O0lI"); // false (contains invalid characters)
37
- * ```
38
- */
39
- export declare function isUuid58(uuid58: string): boolean;
40
- //# sourceMappingURL=check.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,YAAY,EAAE,MAAsC,CAAC;AAElE;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEhD"}
package/esm/check.js DELETED
@@ -1,43 +0,0 @@
1
- // UUID max value encoded in Base58 (length 22)
2
- const MAX_UUID58 = "YcVfxkQb6JRzqk5kF2tNLv"; // cspell:disable-line
3
- /**
4
- * Regular expression pattern for validating UUID58 strings.
5
- *
6
- * This regex ensures that a string:
7
- * - Contains exactly 22 characters
8
- * - Uses only characters from the Base58 alphabet
9
- *
10
- * **Note:** This regex does NOT validate that the decoded value fits within
11
- * 128 bits (UUID size). For complete validation including 128-bit range
12
- * checking, use {@link isUuid58} instead.
13
- *
14
- * @example
15
- * ```typescript
16
- * const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
17
- * const isValid = UUID58_REGEX.test("invalid"); // false
18
- * const isValid = UUID58_REGEX.test("123"); // false (too short)
19
- * ```
20
- */
21
- export const UUID58_REGEX = /^[1-9A-HJ-NP-Za-km-z]{22}$/u;
22
- /**
23
- * Checks if a given string is a valid UUID58-encodable string.
24
- * This function efficiently validates without performing full decoding.
25
- *
26
- * A valid UUID58 string must:
27
- * - Be exactly 22 characters long
28
- * - Contain only characters from the Base58 alphabet
29
- * - Decode to a value that fits within 128 bits (UUID size)
30
- *
31
- * @param uuid58 - The string to check
32
- * @returns `true` if the string is a valid UUID58 string, `false` otherwise
33
- *
34
- * @example
35
- * ```typescript
36
- * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
37
- * isUuid58("invalid"); // false
38
- * isUuid58("O0lI"); // false (contains invalid characters)
39
- * ```
40
- */
41
- export function isUuid58(uuid58) {
42
- return UUID58_REGEX.test(uuid58) && uuid58 <= MAX_UUID58;
43
- }
package/esm/decode.d.ts DELETED
@@ -1,37 +0,0 @@
1
- /**
2
- * Error thrown when an invalid Base58 string is provided for decoding.
3
- * This includes strings containing characters not in the Base58 alphabet.
4
- */
5
- export declare class Uuid58DecodeError extends Error {
6
- }
7
- /**
8
- * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
9
- * throwing an error for invalid input, it returns an `Uuid58DecodeError`
10
- * instance.
11
- *
12
- * @param uuid58 - The 22-character Base58-encoded UUID string to decode
13
- * @returns A standard UUID string (lowercase, with hyphens), or an
14
- * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
15
- * @note This function does not throw; it returns the error object instead.
16
- *
17
- * @example
18
- * ```typescript
19
- * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
20
- * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
21
- * ```
22
- */
23
- export declare function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
24
- /**
25
- * Converts a 22-character Base58-encoded string back to a standard UUID string format.
26
- *
27
- * @param uuid58 - The 22-character Base58-encoded string to decode
28
- * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
29
- * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
30
- *
31
- * @example
32
- * ```typescript
33
- * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
34
- * ```
35
- */
36
- export declare function uuid58Decode(uuid58: string): string;
37
- //# sourceMappingURL=decode.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;CAI3C;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAoC3E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMnD"}
package/esm/decode.js DELETED
@@ -1,78 +0,0 @@
1
- var _a;
2
- import { UUID58_ALPHABET, UUID58_LENGTH } from "./alphabet.js";
3
- // Fast lookup map from alphabet characters to their indices
4
- const BASE58_MAP = {};
5
- for (let i = 0; i < UUID58_ALPHABET.length; i++) {
6
- BASE58_MAP[UUID58_ALPHABET[i]] = BigInt(i);
7
- }
8
- /**
9
- * Error thrown when an invalid Base58 string is provided for decoding.
10
- * This includes strings containing characters not in the Base58 alphabet.
11
- */
12
- export class Uuid58DecodeError extends Error {
13
- }
14
- _a = Uuid58DecodeError;
15
- (() => {
16
- _a.prototype.name = "Uuid58DecodeError";
17
- })();
18
- /**
19
- * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
20
- * throwing an error for invalid input, it returns an `Uuid58DecodeError`
21
- * instance.
22
- *
23
- * @param uuid58 - The 22-character Base58-encoded UUID string to decode
24
- * @returns A standard UUID string (lowercase, with hyphens), or an
25
- * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
26
- * @note This function does not throw; it returns the error object instead.
27
- *
28
- * @example
29
- * ```typescript
30
- * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
31
- * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
32
- * ```
33
- */
34
- export function uuid58DecodeSafe(uuid58) {
35
- if (uuid58.length !== 22) {
36
- return new Uuid58DecodeError(`Expected Base58 string of length 22, but received string of length ${uuid58.length}: ${uuid58}`);
37
- }
38
- let num = 0n;
39
- for (const char of uuid58) {
40
- const index = BASE58_MAP[char];
41
- if (index === undefined) {
42
- return new Uuid58DecodeError(`Invalid Base58 character '${char}' found in input: ${uuid58}`);
43
- }
44
- num = num * UUID58_LENGTH + index;
45
- }
46
- const hex = num.toString(16).padStart(32, "0");
47
- if (hex.length !== 32) {
48
- return new Uuid58DecodeError(`Decoded value exceeds 128 bits of UUID: ${uuid58}`);
49
- }
50
- return (hex.slice(0, 8) +
51
- "-" +
52
- hex.slice(8, 12) +
53
- "-" +
54
- hex.slice(12, 16) +
55
- "-" +
56
- hex.slice(16, 20) +
57
- "-" +
58
- hex.slice(20));
59
- }
60
- /**
61
- * Converts a 22-character Base58-encoded string back to a standard UUID string format.
62
- *
63
- * @param uuid58 - The 22-character Base58-encoded string to decode
64
- * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
65
- * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
66
- *
67
- * @example
68
- * ```typescript
69
- * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
70
- * ```
71
- */
72
- export function uuid58Decode(uuid58) {
73
- const result = uuid58DecodeSafe(uuid58);
74
- if (result instanceof Uuid58DecodeError) {
75
- throw result;
76
- }
77
- return result;
78
- }
package/esm/encode.d.ts DELETED
@@ -1,49 +0,0 @@
1
- /**
2
- * Error thrown when an invalid UUID string is provided for encoding.
3
- * This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
4
- */
5
- export declare class Uuid58EncodeError extends Error {
6
- }
7
- /**
8
- * Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
9
- * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
10
- *
11
- * @param uuid - The UUID string to encode (with or without hyphens)
12
- * @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
13
- * not a valid UUID
14
- * @note This function does not throw; it returns the error object instead.
15
- *
16
- * @example
17
- * ```typescript
18
- * // With hyphens
19
- * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
20
- * // Without hyphens
21
- * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
22
- * // Uppercase UUID is also accepted
23
- * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
24
- * // Invalid UUID format
25
- * const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
26
- * ```
27
- */
28
- export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
29
- /**
30
- * Converts a standard UUID string to a 22-character Base58-encoded format.
31
- *
32
- * @param uuid - The UUID string to encode. Can be provided with or without hyphens
33
- * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
34
- * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
35
- * @returns A 22-character Base58-encoded string representation of the UUID
36
- * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
37
- *
38
- * @example
39
- * ```typescript
40
- * // With hyphens
41
- * const base58 = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
42
- * // Without hyphens
43
- * const base58 = uuid58Encode("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
44
- * // Uppercase UUID is also accepted
45
- * const base58 = uuid58Encode("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
46
- * ```
47
- */
48
- export declare function uuid58Encode(uuid: string): string;
49
- //# sourceMappingURL=encode.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;CAI3C;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAwBzE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAMjD"}