@nakanoaas/uuid58 1.0.5 → 1.2.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/README.md CHANGED
@@ -94,6 +94,15 @@ if (decodedSafe instanceof Uuid58DecodeError) {
94
94
  }
95
95
  // use decoded UUID
96
96
  console.log(decodedSafe);
97
+
98
+ import { isUuid58, UUID58_REGEX } from "@nakanoaas/uuid58";
99
+
100
+ // Validate UUID58 string without decoding
101
+ const isValid = isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
102
+ const isValid2 = isUuid58("invalid"); // false
103
+
104
+ // Or use regex pattern for validation
105
+ const isValid3 = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
97
106
  ```
98
107
 
99
108
  ## API Reference
@@ -165,6 +174,95 @@ function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
165
174
  `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
166
175
  - **Note:** This function does not throw; it returns the error object instead.
167
176
 
177
+ ### Error Classes
178
+
179
+ #### `Uuid58EncodeError`
180
+
181
+ Error thrown when an invalid UUID string is provided for encoding. This includes
182
+ strings that don't match the standard UUID format (32 hexadecimal characters
183
+ with optional hyphens).
184
+
185
+ ```typescript
186
+ class Uuid58EncodeError extends Error;
187
+ ```
188
+
189
+ #### `Uuid58DecodeError`
190
+
191
+ Error thrown when an invalid Base58 string is provided for decoding. This
192
+ includes strings containing characters not in the Base58 alphabet or strings
193
+ that are not exactly 22 characters long.
194
+
195
+ ```typescript
196
+ class Uuid58DecodeError extends Error;
197
+ ```
198
+
199
+ ### `isUuid58(uuid58: string)`
200
+
201
+ Checks if a given string is a valid UUID58-encodable string. This function
202
+ efficiently validates without performing full decoding.
203
+
204
+ ```typescript
205
+ function isUuid58(uuid58: string): boolean;
206
+ ```
207
+
208
+ - **Parameters:**
209
+ - `uuid58`: The string to check
210
+ - **Returns:** `true` if the string is a valid UUID58 string, `false` otherwise
211
+ - **Note:** This function validates:
212
+ - Length (must be exactly 22 characters)
213
+ - Base58 alphabet characters only
214
+ - That the decoded value fits within 128 bits (UUID size)
215
+
216
+ **Example:**
217
+
218
+ ```typescript
219
+ import { isUuid58 } from "@nakanoaas/uuid58";
220
+
221
+ isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
222
+ isUuid58("invalid"); // false
223
+ isUuid58("O0lI"); // false (contains invalid characters)
224
+ isUuid58("short"); // false (too short)
225
+ ```
226
+
227
+ ### Alphabet and Utilities
228
+
229
+ #### `UUID58_ALPHABET`
230
+
231
+ Character set for Base58 encoding (Bitcoin compatible). This alphabet excludes
232
+ characters that can be easily confused: `0` (zero) and `O` (capital O), `I`
233
+ (capital i) and `l` (lowercase L). The alphabet consists of 58 characters:
234
+ digits 1-9 and letters A-Z, a-z (excluding the ambiguous characters mentioned
235
+ above).
236
+
237
+ ```typescript
238
+ const UUID58_ALPHABET: string; // "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
239
+ ```
240
+
241
+ #### `UUID58_REGEX`
242
+
243
+ Regular expression pattern for validating UUID58 strings. This regex ensures
244
+ that a string contains exactly 22 characters and uses only characters from the
245
+ Base58 alphabet.
246
+
247
+ ```typescript
248
+ const UUID58_REGEX: RegExp; // /^[1-9A-HJ-NP-Za-km-z]{22}$/
249
+ ```
250
+
251
+ **Note:** While `UUID58_REGEX` can validate the format, `isUuid58()` provides
252
+ more comprehensive validation by also checking that the decoded value fits
253
+ within 128 bits. For simple format checks, the regex is sufficient; for complete
254
+ validation, use `isUuid58()`.
255
+
256
+ **Example:**
257
+
258
+ ```typescript
259
+ import { UUID58_REGEX } from "@nakanoaas/uuid58";
260
+
261
+ const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
262
+ const isValid = UUID58_REGEX.test("invalid"); // false
263
+ const isValid = UUID58_REGEX.test("123"); // false (too short)
264
+ ```
265
+
168
266
  ## Why uuid58?
169
267
 
170
268
  Standard UUIDs are 36 characters long (including hyphens), which can be
package/esm/alphabet.d.ts CHANGED
@@ -1,3 +1,24 @@
1
- export declare const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2
- export declare const ALPHABET_LENGTH: bigint;
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;
3
24
  //# sourceMappingURL=alphabet.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"alphabet.d.ts","sourceRoot":"","sources":["../src/alphabet.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,eAAe,+DACkC,CAAC;AAC/D,eAAO,MAAM,eAAe,QAAiC,CAAC"}
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 CHANGED
@@ -1,3 +1,23 @@
1
- // Character set for Base58 (Bitcoin compatible)
2
- export const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; // cspell:disable-line
3
- export const ALPHABET_LENGTH = BigInt(BASE58_ALPHABET.length);
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 ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Checks if a given string is a valid UUID58-encodable string.
3
+ * This function efficiently validates without performing full decoding.
4
+ *
5
+ * A valid UUID58 string must:
6
+ * - Be exactly 22 characters long
7
+ * - Contain only characters from the Base58 alphabet
8
+ * - Decode to a value that fits within 128 bits (UUID size)
9
+ *
10
+ * @param uuid58 - The string to check
11
+ * @returns `true` if the string is a valid UUID58 string, `false` otherwise
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
16
+ * isUuid58("invalid"); // false
17
+ * isUuid58("O0lI"); // false (contains invalid characters)
18
+ * ```
19
+ */
20
+ export declare function isUuid58(uuid58: string): boolean;
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
+ export declare const UUID58_REGEX: RegExp;
40
+ //# sourceMappingURL=check.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAmChD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,YAAY,EAAE,MAAsC,CAAC"}
package/esm/check.js ADDED
@@ -0,0 +1,73 @@
1
+ const MAX_UUID58_CODES = Array.from(
2
+ // UUID max value encoded in Base58 (length 22)
3
+ "YcVfxkQb6JRzqk5kF2tNLv", // cspell:disable-line
4
+ (char) => char.charCodeAt(0));
5
+ /**
6
+ * Checks if a given string is a valid UUID58-encodable string.
7
+ * This function efficiently validates without performing full decoding.
8
+ *
9
+ * A valid UUID58 string must:
10
+ * - Be exactly 22 characters long
11
+ * - Contain only characters from the Base58 alphabet
12
+ * - Decode to a value that fits within 128 bits (UUID size)
13
+ *
14
+ * @param uuid58 - The string to check
15
+ * @returns `true` if the string is a valid UUID58 string, `false` otherwise
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
20
+ * isUuid58("invalid"); // false
21
+ * isUuid58("O0lI"); // false (contains invalid characters)
22
+ * ```
23
+ */
24
+ export function isUuid58(uuid58) {
25
+ // Check length: UUID58 strings must be exactly 22 characters
26
+ if (uuid58.length !== 22) {
27
+ return false;
28
+ }
29
+ // Compare chars against the max-UUID Base58 string using ASCII order
30
+ let isBelowMax = false;
31
+ for (let i = 0; i < 22; i++) {
32
+ const code = uuid58.charCodeAt(i);
33
+ // Check if character is in the Bitcoin Base58 alphabet
34
+ if (code < 49 ||
35
+ (code >= 58 && code <= 64) || // ':'..'@'
36
+ code === 73 || // 'I'
37
+ code === 79 || // 'O'
38
+ (code >= 91 && code <= 96) || // '['..'`'
39
+ code === 108 || // 'l'
40
+ code > 122) {
41
+ return false;
42
+ }
43
+ if (!isBelowMax) {
44
+ const maxCode = MAX_UUID58_CODES[i];
45
+ if (code < maxCode) {
46
+ isBelowMax = true;
47
+ }
48
+ else if (code > maxCode) {
49
+ return false;
50
+ }
51
+ }
52
+ }
53
+ return true;
54
+ }
55
+ /**
56
+ * Regular expression pattern for validating UUID58 strings.
57
+ *
58
+ * This regex ensures that a string:
59
+ * - Contains exactly 22 characters
60
+ * - Uses only characters from the Base58 alphabet
61
+ *
62
+ * **Note:** This regex does NOT validate that the decoded value fits within
63
+ * 128 bits (UUID size). For complete validation including 128-bit range
64
+ * checking, use {@link isUuid58} instead.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
69
+ * const isValid = UUID58_REGEX.test("invalid"); // false
70
+ * const isValid = UUID58_REGEX.test("123"); // false (too short)
71
+ * ```
72
+ */
73
+ export const UUID58_REGEX = /^[1-9A-HJ-NP-Za-km-z]{22}$/u;
package/esm/decode.js CHANGED
@@ -1,9 +1,9 @@
1
1
  var _a;
2
- import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
2
+ import { UUID58_ALPHABET, UUID58_LENGTH } from "./alphabet.js";
3
3
  // Fast lookup map from alphabet characters to their indices
4
4
  const BASE58_MAP = {};
5
- for (let i = 0; i < BASE58_ALPHABET.length; i++) {
6
- BASE58_MAP[BASE58_ALPHABET[i]] = BigInt(i);
5
+ for (let i = 0; i < UUID58_ALPHABET.length; i++) {
6
+ BASE58_MAP[UUID58_ALPHABET[i]] = BigInt(i);
7
7
  }
8
8
  /**
9
9
  * Error thrown when an invalid Base58 string is provided for decoding.
@@ -41,7 +41,7 @@ export function uuid58DecodeSafe(uuid58) {
41
41
  if (index === undefined) {
42
42
  return new Uuid58DecodeError(`Invalid Base58 character '${char}' found in input: ${uuid58}`);
43
43
  }
44
- num = num * ALPHABET_LENGTH + index;
44
+ num = num * UUID58_LENGTH + index;
45
45
  }
46
46
  const hex = num.toString(16).padStart(32, "0");
47
47
  if (hex.length !== 32) {
package/esm/encode.js CHANGED
@@ -1,5 +1,5 @@
1
1
  var _a;
2
- import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
2
+ import { UUID58_ALPHABET, UUID58_LENGTH } from "./alphabet.js";
3
3
  /**
4
4
  * Error thrown when an invalid UUID string is provided for encoding.
5
5
  * This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
@@ -45,10 +45,10 @@ export function uuid58EncodeSafe(uuid) {
45
45
  }
46
46
  let encoded = "";
47
47
  do {
48
- encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
49
- num /= ALPHABET_LENGTH;
48
+ encoded = UUID58_ALPHABET[Number(num % UUID58_LENGTH)] + encoded;
49
+ num /= UUID58_LENGTH;
50
50
  } while (num > 0n);
51
- return encoded.padStart(22, BASE58_ALPHABET[0]);
51
+ return encoded.padStart(22, UUID58_ALPHABET[0]);
52
52
  }
53
53
  /**
54
54
  * Converts a standard UUID string to a 22-character Base58-encoded format.
@@ -12,4 +12,4 @@
12
12
  * ```
13
13
  */
14
14
  export declare function uuid58(): string;
15
- //# sourceMappingURL=uuid58.d.ts.map
15
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAe/B"}
@@ -1,4 +1,4 @@
1
- import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
1
+ import { UUID58_ALPHABET, UUID58_LENGTH } from "./alphabet.js";
2
2
  /**
3
3
  * Generates a new Base58-encoded UUID (always 22 characters).
4
4
  *
@@ -19,8 +19,8 @@ export function uuid58() {
19
19
  let num = bytes[0] << 64n | bytes[1];
20
20
  let encoded = "";
21
21
  do {
22
- encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
23
- num /= ALPHABET_LENGTH;
22
+ encoded = UUID58_ALPHABET[Number(num % UUID58_LENGTH)] + encoded;
23
+ num /= UUID58_LENGTH;
24
24
  } while (num > 0n);
25
- return encoded.padStart(22, BASE58_ALPHABET[0]);
25
+ return encoded.padStart(22, UUID58_ALPHABET[0]);
26
26
  }
package/esm/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ export { UUID58_ALPHABET } from "./alphabet.js";
2
+ export * from "./check.js";
1
3
  export * from "./decode.js";
2
4
  export * from "./encode.js";
3
- export * from "./uuid58.js";
5
+ export * from "./generate.js";
4
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
package/esm/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ export { UUID58_ALPHABET } from "./alphabet.js";
2
+ export * from "./check.js";
1
3
  export * from "./decode.js";
2
4
  export * from "./encode.js";
3
- export * from "./uuid58.js";
5
+ export * from "./generate.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakanoaas/uuid58",
3
- "version": "1.0.5",
3
+ "version": "1.2.0",
4
4
  "description": "A tiny, zero-dependency library for generating and converting UUIDs to Base58-encoded strings.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,3 +1,24 @@
1
- export declare const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2
- export declare const ALPHABET_LENGTH: bigint;
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;
3
24
  //# sourceMappingURL=alphabet.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"alphabet.d.ts","sourceRoot":"","sources":["../src/alphabet.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,eAAe,+DACkC,CAAC;AAC/D,eAAO,MAAM,eAAe,QAAiC,CAAC"}
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"}
@@ -1,6 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ALPHABET_LENGTH = exports.BASE58_ALPHABET = void 0;
4
- // Character set for Base58 (Bitcoin compatible)
5
- exports.BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; // cspell:disable-line
6
- exports.ALPHABET_LENGTH = BigInt(exports.BASE58_ALPHABET.length);
3
+ exports.UUID58_LENGTH = exports.UUID58_ALPHABET = void 0;
4
+ /**
5
+ * Character set for Base58 encoding (Bitcoin compatible).
6
+ *
7
+ * This alphabet excludes characters that can be easily confused:
8
+ * - `0` (zero) and `O` (capital O)
9
+ * - `I` (capital i) and `l` (lowercase L)
10
+ *
11
+ * The alphabet consists of 58 characters: digits 1-9 and letters A-Z, a-z
12
+ * (excluding the ambiguous characters mentioned above).
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const char = UUID58_ALPHABET[0]; // "1"
17
+ * const char = UUID58_ALPHABET[9]; // "A"
18
+ * ```
19
+ */
20
+ exports.UUID58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
21
+ /**
22
+ * @internal
23
+ * The length of the Base58 alphabet as BigInt for encoding/decoding operations.
24
+ * This is exported for internal use but not part of the public API.
25
+ */
26
+ exports.UUID58_LENGTH = BigInt(exports.UUID58_ALPHABET.length);
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Checks if a given string is a valid UUID58-encodable string.
3
+ * This function efficiently validates without performing full decoding.
4
+ *
5
+ * A valid UUID58 string must:
6
+ * - Be exactly 22 characters long
7
+ * - Contain only characters from the Base58 alphabet
8
+ * - Decode to a value that fits within 128 bits (UUID size)
9
+ *
10
+ * @param uuid58 - The string to check
11
+ * @returns `true` if the string is a valid UUID58 string, `false` otherwise
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
16
+ * isUuid58("invalid"); // false
17
+ * isUuid58("O0lI"); // false (contains invalid characters)
18
+ * ```
19
+ */
20
+ export declare function isUuid58(uuid58: string): boolean;
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
+ export declare const UUID58_REGEX: RegExp;
40
+ //# sourceMappingURL=check.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAmChD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,YAAY,EAAE,MAAsC,CAAC"}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UUID58_REGEX = void 0;
4
+ exports.isUuid58 = isUuid58;
5
+ const MAX_UUID58_CODES = Array.from(
6
+ // UUID max value encoded in Base58 (length 22)
7
+ "YcVfxkQb6JRzqk5kF2tNLv", // cspell:disable-line
8
+ (char) => char.charCodeAt(0));
9
+ /**
10
+ * Checks if a given string is a valid UUID58-encodable string.
11
+ * This function efficiently validates without performing full decoding.
12
+ *
13
+ * A valid UUID58 string must:
14
+ * - Be exactly 22 characters long
15
+ * - Contain only characters from the Base58 alphabet
16
+ * - Decode to a value that fits within 128 bits (UUID size)
17
+ *
18
+ * @param uuid58 - The string to check
19
+ * @returns `true` if the string is a valid UUID58 string, `false` otherwise
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * isUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // true
24
+ * isUuid58("invalid"); // false
25
+ * isUuid58("O0lI"); // false (contains invalid characters)
26
+ * ```
27
+ */
28
+ function isUuid58(uuid58) {
29
+ // Check length: UUID58 strings must be exactly 22 characters
30
+ if (uuid58.length !== 22) {
31
+ return false;
32
+ }
33
+ // Compare chars against the max-UUID Base58 string using ASCII order
34
+ let isBelowMax = false;
35
+ for (let i = 0; i < 22; i++) {
36
+ const code = uuid58.charCodeAt(i);
37
+ // Check if character is in the Bitcoin Base58 alphabet
38
+ if (code < 49 ||
39
+ (code >= 58 && code <= 64) || // ':'..'@'
40
+ code === 73 || // 'I'
41
+ code === 79 || // 'O'
42
+ (code >= 91 && code <= 96) || // '['..'`'
43
+ code === 108 || // 'l'
44
+ code > 122) {
45
+ return false;
46
+ }
47
+ if (!isBelowMax) {
48
+ const maxCode = MAX_UUID58_CODES[i];
49
+ if (code < maxCode) {
50
+ isBelowMax = true;
51
+ }
52
+ else if (code > maxCode) {
53
+ return false;
54
+ }
55
+ }
56
+ }
57
+ return true;
58
+ }
59
+ /**
60
+ * Regular expression pattern for validating UUID58 strings.
61
+ *
62
+ * This regex ensures that a string:
63
+ * - Contains exactly 22 characters
64
+ * - Uses only characters from the Base58 alphabet
65
+ *
66
+ * **Note:** This regex does NOT validate that the decoded value fits within
67
+ * 128 bits (UUID size). For complete validation including 128-bit range
68
+ * checking, use {@link isUuid58} instead.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const isValid = UUID58_REGEX.test("XDY9dmBbcMBXqcRvYw8xJ2"); // true
73
+ * const isValid = UUID58_REGEX.test("invalid"); // false
74
+ * const isValid = UUID58_REGEX.test("123"); // false (too short)
75
+ * ```
76
+ */
77
+ exports.UUID58_REGEX = /^[1-9A-HJ-NP-Za-km-z]{22}$/u;
package/script/decode.js CHANGED
@@ -7,8 +7,8 @@ exports.uuid58Decode = uuid58Decode;
7
7
  const alphabet_js_1 = require("./alphabet.js");
8
8
  // Fast lookup map from alphabet characters to their indices
9
9
  const BASE58_MAP = {};
10
- for (let i = 0; i < alphabet_js_1.BASE58_ALPHABET.length; i++) {
11
- BASE58_MAP[alphabet_js_1.BASE58_ALPHABET[i]] = BigInt(i);
10
+ for (let i = 0; i < alphabet_js_1.UUID58_ALPHABET.length; i++) {
11
+ BASE58_MAP[alphabet_js_1.UUID58_ALPHABET[i]] = BigInt(i);
12
12
  }
13
13
  /**
14
14
  * Error thrown when an invalid Base58 string is provided for decoding.
@@ -47,7 +47,7 @@ function uuid58DecodeSafe(uuid58) {
47
47
  if (index === undefined) {
48
48
  return new Uuid58DecodeError(`Invalid Base58 character '${char}' found in input: ${uuid58}`);
49
49
  }
50
- num = num * alphabet_js_1.ALPHABET_LENGTH + index;
50
+ num = num * alphabet_js_1.UUID58_LENGTH + index;
51
51
  }
52
52
  const hex = num.toString(16).padStart(32, "0");
53
53
  if (hex.length !== 32) {
package/script/encode.js CHANGED
@@ -51,10 +51,10 @@ function uuid58EncodeSafe(uuid) {
51
51
  }
52
52
  let encoded = "";
53
53
  do {
54
- encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
55
- num /= alphabet_js_1.ALPHABET_LENGTH;
54
+ encoded = alphabet_js_1.UUID58_ALPHABET[Number(num % alphabet_js_1.UUID58_LENGTH)] + encoded;
55
+ num /= alphabet_js_1.UUID58_LENGTH;
56
56
  } while (num > 0n);
57
- return encoded.padStart(22, alphabet_js_1.BASE58_ALPHABET[0]);
57
+ return encoded.padStart(22, alphabet_js_1.UUID58_ALPHABET[0]);
58
58
  }
59
59
  /**
60
60
  * Converts a standard UUID string to a 22-character Base58-encoded format.
@@ -12,4 +12,4 @@
12
12
  * ```
13
13
  */
14
14
  export declare function uuid58(): string;
15
- //# sourceMappingURL=uuid58.d.ts.map
15
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAe/B"}
@@ -22,8 +22,8 @@ function uuid58() {
22
22
  let num = bytes[0] << 64n | bytes[1];
23
23
  let encoded = "";
24
24
  do {
25
- encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
26
- num /= alphabet_js_1.ALPHABET_LENGTH;
25
+ encoded = alphabet_js_1.UUID58_ALPHABET[Number(num % alphabet_js_1.UUID58_LENGTH)] + encoded;
26
+ num /= alphabet_js_1.UUID58_LENGTH;
27
27
  } while (num > 0n);
28
- return encoded.padStart(22, alphabet_js_1.BASE58_ALPHABET[0]);
28
+ return encoded.padStart(22, alphabet_js_1.UUID58_ALPHABET[0]);
29
29
  }
package/script/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ export { UUID58_ALPHABET } from "./alphabet.js";
2
+ export * from "./check.js";
1
3
  export * from "./decode.js";
2
4
  export * from "./encode.js";
3
- export * from "./uuid58.js";
5
+ export * from "./generate.js";
4
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
package/script/index.js CHANGED
@@ -14,6 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.UUID58_ALPHABET = void 0;
18
+ var alphabet_js_1 = require("./alphabet.js");
19
+ Object.defineProperty(exports, "UUID58_ALPHABET", { enumerable: true, get: function () { return alphabet_js_1.UUID58_ALPHABET; } });
20
+ __exportStar(require("./check.js"), exports);
17
21
  __exportStar(require("./decode.js"), exports);
18
22
  __exportStar(require("./encode.js"), exports);
19
- __exportStar(require("./uuid58.js"), exports);
23
+ __exportStar(require("./generate.js"), exports);
@@ -1 +0,0 @@
1
- {"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAe/B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAe/B"}