@nakanoaas/uuid58 0.0.7 → 1.0.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
@@ -10,12 +10,13 @@ shorter, URL-safe identifiers while maintaining the uniqueness of UUIDs.
10
10
 
11
11
  ## Features
12
12
 
13
- - 🚀 Generate short, URL-safe identifiers (~22 characters)
13
+ - 🚀 Generate short, URL-safe identifiers (**fixed 22 characters**)
14
14
  - 🔄 Bidirectional conversion between UUID and Base58
15
15
  - ðŸŠķ Zero dependencies
16
16
  - 💊 Type-safe with TypeScript
17
17
  - 🔒 Uses native `crypto.randomUUID()` for secure UUID generation
18
18
  - ðŸ“Ķ Supports both ESM and CommonJS
19
+ - ðŸ›Ąïļ Provides safe APIs that return error objects instead of throwing exceptions
19
20
 
20
21
  ## Installation
21
22
 
@@ -50,13 +51,13 @@ pnpm add @nakanoaas/uuid58
50
51
  ```typescript
51
52
  import { uuid58, uuid58Decode, uuid58Encode } from "@nakanoaas/uuid58";
52
53
 
53
- // Generate a new Base58-encoded UUID
54
+ // Generate a new Base58-encoded UUID (always 22 characters)
54
55
  const id = uuid58();
55
- // => "XDY9dmBbcMBXqcRvYw8xJ2"
56
+ // => "XDY9dmBbcMBXqcRvYw8xJ2" (22 characters)
56
57
 
57
- // Convert existing UUID to Base58
58
+ // Convert existing UUID to Base58 (always 22 characters)
58
59
  const encoded = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf");
59
- // => "XDY9dmBbcMBXqcRvYw8xJ2"
60
+ // => "XDY9dmBbcMBXqcRvYw8xJ2" (22 characters)
60
61
 
61
62
  // Convert Base58 back to UUID
62
63
  const decoded = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2");
@@ -76,7 +77,7 @@ if (encodedSafe instanceof Uuid58EncodeError) {
76
77
  console.error(encodedSafe.message);
77
78
  return;
78
79
  }
79
- // use encoded string
80
+ // use encoded string (22 characters)
80
81
  console.log(encodedSafe);
81
82
 
82
83
  // Safe decoding: returns error object instead of throwing
@@ -94,74 +95,76 @@ console.log(decodedSafe);
94
95
 
95
96
  ### `uuid58()`
96
97
 
97
- Generates a new Base58-encoded UUID.
98
+ Generates a new Base58-encoded UUID (always 22 characters).
98
99
 
99
100
  ```typescript
100
- function uuid58(): string;
101
+ function uuid58(): string; // always returns a 22-character string
101
102
  ```
102
103
 
103
104
  ### `uuid58Encode(uuid: string)`
104
105
 
105
- Converts a standard UUID string to a Base58-encoded format.
106
+ Converts a standard UUID string to a 22-character Base58-encoded format.
106
107
 
107
108
  ```typescript
108
- function uuid58Encode(uuid: string): string;
109
+ function uuid58Encode(uuid: string): string; // always returns a 22-character string
109
110
  ```
110
111
 
111
112
  - **Parameters:**
112
113
  - `uuid`: The UUID string to encode (with or without hyphens)
113
- - **Returns:** A Base58-encoded string
114
+ - **Returns:** A 22-character Base58-encoded string
114
115
  - **Throws:** `Uuid58EncodeError` if the input is not a valid UUID
115
116
 
116
117
  ### `uuid58EncodeSafe(uuid: string)`
117
118
 
118
- Converts a standard UUID string to a Base58-encoded format, but instead of
119
- throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
119
+ Converts a standard UUID string to a 22-character Base58-encoded format, but
120
+ instead of throwing an error for invalid input, it returns an
121
+ `Uuid58EncodeError` instance.
120
122
 
121
123
  ```typescript
122
- function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
124
+ function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError; // string is always 22 characters
123
125
  ```
124
126
 
125
127
  - **Parameters:**
126
128
  - `uuid`: The UUID string to encode (with or without hyphens)
127
- - **Returns:** A Base58-encoded string, or an `Uuid58EncodeError` if the input
128
- is not a valid UUID
129
+ - **Returns:** A 22-character Base58-encoded string, or an `Uuid58EncodeError`
130
+ if the input is not a valid UUID
129
131
  - **Note:** This function does not throw; it returns the error object instead.
130
132
 
131
133
  ### `uuid58Decode(encoded: string)`
132
134
 
133
- Converts a Base58-encoded string back to a standard UUID format.
135
+ Converts a 22-character Base58-encoded string back to a standard UUID format.
134
136
 
135
137
  ```typescript
136
- function uuid58Decode(encoded: string): string;
138
+ function uuid58Decode(uuid58: string): string;
137
139
  ```
138
140
 
139
141
  - **Parameters:**
140
- - `encoded`: The Base58-encoded string to decode
142
+ - `uuid58`: The 22-character Base58-encoded string to decode
141
143
  - **Returns:** A standard UUID string (lowercase, with hyphens)
142
- - **Throws:** `Uuid58DecodeError` if the input contains invalid Base58
143
- characters
144
+ - **Throws:** `Uuid58DecodeError` if the input is not a valid 22-character
145
+ Base58 string
144
146
 
145
147
  ### `uuid58DecodeSafe(encoded: string)`
146
148
 
147
- Converts a Base58-encoded string back to a standard UUID format, but instead of
148
- throwing an error for invalid input, it returns an `Uuid58DecodeError` instance.
149
+ Converts a 22-character Base58-encoded string back to a standard UUID format,
150
+ but instead of throwing an error for invalid input, it returns an
151
+ `Uuid58DecodeError` instance.
149
152
 
150
153
  ```typescript
151
- function uuid58DecodeSafe(encoded: string): string | Uuid58DecodeError;
154
+ function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
152
155
  ```
153
156
 
154
157
  - **Parameters:**
155
- - `encoded`: The Base58-encoded string to decode
158
+ - `uuid58`: The 22-character Base58-encoded string to decode
156
159
  - **Returns:** A standard UUID string (lowercase, with hyphens), or an
157
- `Uuid58DecodeError` if the input contains invalid Base58 characters
160
+ `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
158
161
  - **Note:** This function does not throw; it returns the error object instead.
159
162
 
160
163
  ## Why uuid58?
161
164
 
162
165
  Standard UUIDs are 36 characters long (including hyphens), which can be
163
- cumbersome in URLs or when space is limited. Base58 encoding reduces this to
164
- around 22 characters while maintaining uniqueness and using only URL-safe
166
+ cumbersome in URLs or when space is limited. Base58 encoding reduces this to a
167
+ fixed 22 characters while maintaining uniqueness and using only URL-safe
165
168
  characters. The Base58 alphabet used in this library is the same as Bitcoin's,
166
169
  which excludes similar-looking characters (0, O, I, l) to prevent visual
167
170
  ambiguity and confusion.
package/esm/decode.d.ts CHANGED
@@ -5,13 +5,13 @@
5
5
  export declare class Uuid58DecodeError extends Error {
6
6
  }
7
7
  /**
8
- * Converts a Base58-encoded string back to a standard UUID format, but instead of
8
+ * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
9
9
  * throwing an error for invalid input, it returns an `Uuid58DecodeError`
10
10
  * instance.
11
11
  *
12
- * @param base58 - The Base58-encoded string to decode
12
+ * @param uuid58 - The 22-character Base58-encoded UUID string to decode
13
13
  * @returns A standard UUID string (lowercase, with hyphens), or an
14
- * `Uuid58DecodeError` if the input contains invalid Base58 characters
14
+ * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
15
15
  * @note This function does not throw; it returns the error object instead.
16
16
  *
17
17
  * @example
@@ -20,18 +20,18 @@ export declare class Uuid58DecodeError extends Error {
20
20
  * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
21
21
  * ```
22
22
  */
23
- export declare function uuid58DecodeSafe(base58: string): string | Uuid58DecodeError;
23
+ export declare function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
24
24
  /**
25
- * Converts a Base58-encoded string back to a standard UUID string format.
25
+ * Converts a 22-character Base58-encoded string back to a standard UUID string format.
26
26
  *
27
- * @param base58 - The Base58-encoded string to decode
27
+ * @param uuid58 - The 22-character Base58-encoded string to decode
28
28
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
29
- * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
29
+ * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
30
30
  *
31
31
  * @example
32
32
  * ```typescript
33
33
  * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
34
34
  * ```
35
35
  */
36
- export declare function uuid58Decode(base58: string): string;
36
+ export declare function uuid58Decode(uuid58: string): string;
37
37
  //# sourceMappingURL=decode.d.ts.map
@@ -1 +1 @@
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,CA4B3E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA4BnD"}
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 CHANGED
@@ -16,13 +16,13 @@ _a = Uuid58DecodeError;
16
16
  _a.prototype.name = "Uuid58DecodeError";
17
17
  })();
18
18
  /**
19
- * Converts a Base58-encoded string back to a standard UUID format, but instead of
19
+ * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
20
20
  * throwing an error for invalid input, it returns an `Uuid58DecodeError`
21
21
  * instance.
22
22
  *
23
- * @param base58 - The Base58-encoded string to decode
23
+ * @param uuid58 - The 22-character Base58-encoded UUID string to decode
24
24
  * @returns A standard UUID string (lowercase, with hyphens), or an
25
- * `Uuid58DecodeError` if the input contains invalid Base58 characters
25
+ * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
26
26
  * @note This function does not throw; it returns the error object instead.
27
27
  *
28
28
  * @example
@@ -31,18 +31,21 @@ _a = Uuid58DecodeError;
31
31
  * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
32
32
  * ```
33
33
  */
34
- export function uuid58DecodeSafe(base58) {
34
+ export function uuid58DecodeSafe(uuid58) {
35
+ if (uuid58.length !== 22) {
36
+ return new Uuid58DecodeError(`Base58 string must be exactly 22 characters: ${uuid58}`);
37
+ }
35
38
  let num = 0n;
36
- for (const char of base58) {
39
+ for (const char of uuid58) {
37
40
  const index = BASE58_MAP[char];
38
41
  if (index === undefined) {
39
- return new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
42
+ return new Uuid58DecodeError(`Invalid Base58 character '${char}' found in input: ${uuid58}`);
40
43
  }
41
44
  num = num * ALPHABET_LENGTH + index;
42
45
  }
43
46
  const hex = num.toString(16).padStart(32, "0");
44
47
  if (hex.length !== 32) {
45
- return new Uuid58DecodeError(`length not 32: ${base58}`);
48
+ return new Uuid58DecodeError(`Decoded hexadecimal string is not 32 characters: ${uuid58}`);
46
49
  }
47
50
  return (hex.slice(0, 8) +
48
51
  "-" +
@@ -55,37 +58,21 @@ export function uuid58DecodeSafe(base58) {
55
58
  hex.slice(20));
56
59
  }
57
60
  /**
58
- * Converts a Base58-encoded string back to a standard UUID string format.
61
+ * Converts a 22-character Base58-encoded string back to a standard UUID string format.
59
62
  *
60
- * @param base58 - The Base58-encoded string to decode
63
+ * @param uuid58 - The 22-character Base58-encoded string to decode
61
64
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
62
- * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
65
+ * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
63
66
  *
64
67
  * @example
65
68
  * ```typescript
66
69
  * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
67
70
  * ```
68
71
  */
69
- export function uuid58Decode(base58) {
70
- let num = 0n;
71
- for (const char of base58) {
72
- const index = BASE58_MAP[char];
73
- if (index === undefined) {
74
- throw new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
75
- }
76
- num = num * ALPHABET_LENGTH + index;
77
- }
78
- const hex = num.toString(16).padStart(32, "0");
79
- if (hex.length !== 32) {
80
- throw new Uuid58DecodeError(`length not 32: ${base58}`);
72
+ export function uuid58Decode(uuid58) {
73
+ const result = uuid58DecodeSafe(uuid58);
74
+ if (result instanceof Uuid58DecodeError) {
75
+ throw result;
81
76
  }
82
- return (hex.slice(0, 8) +
83
- "-" +
84
- hex.slice(8, 12) +
85
- "-" +
86
- hex.slice(12, 16) +
87
- "-" +
88
- hex.slice(16, 20) +
89
- "-" +
90
- hex.slice(20));
77
+ return result;
91
78
  }
package/esm/encode.d.ts CHANGED
@@ -5,11 +5,11 @@
5
5
  export declare class Uuid58EncodeError extends Error {
6
6
  }
7
7
  /**
8
- * Converts a standard UUID string to a Base58-encoded format, but instead of
8
+ * Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
9
9
  * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
10
10
  *
11
11
  * @param uuid - The UUID string to encode (with or without hyphens)
12
- * @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
12
+ * @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
13
13
  * not a valid UUID
14
14
  * @note This function does not throw; it returns the error object instead.
15
15
  *
@@ -27,12 +27,12 @@ export declare class Uuid58EncodeError extends Error {
27
27
  */
28
28
  export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
29
29
  /**
30
- * Converts a standard UUID string to a shortened Base58-encoded format.
30
+ * Converts a standard UUID string to a 22-character Base58-encoded format.
31
31
  *
32
32
  * @param uuid - The UUID string to encode. Can be provided with or without hyphens
33
33
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
34
34
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
35
- * @returns A Base58-encoded string representation of the UUID
35
+ * @returns A 22-character Base58-encoded string representation of the UUID
36
36
  * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
37
37
  *
38
38
  * @example
@@ -1 +1 @@
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,CAwBjD"}
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"}
package/esm/encode.js CHANGED
@@ -11,11 +11,11 @@ _a = Uuid58EncodeError;
11
11
  _a.prototype.name = "Uuid58EncodeError";
12
12
  })();
13
13
  /**
14
- * Converts a standard UUID string to a Base58-encoded format, but instead of
14
+ * Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
15
15
  * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
16
16
  *
17
17
  * @param uuid - The UUID string to encode (with or without hyphens)
18
- * @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
18
+ * @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
19
19
  * not a valid UUID
20
20
  * @note This function does not throw; it returns the error object instead.
21
21
  *
@@ -48,15 +48,15 @@ export function uuid58EncodeSafe(uuid) {
48
48
  encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
49
49
  num /= ALPHABET_LENGTH;
50
50
  } while (num > 0n);
51
- return encoded;
51
+ return encoded.padStart(22, BASE58_ALPHABET[0]);
52
52
  }
53
53
  /**
54
- * Converts a standard UUID string to a shortened Base58-encoded format.
54
+ * Converts a standard UUID string to a 22-character Base58-encoded format.
55
55
  *
56
56
  * @param uuid - The UUID string to encode. Can be provided with or without hyphens
57
57
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
58
58
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
59
- * @returns A Base58-encoded string representation of the UUID
59
+ * @returns A 22-character Base58-encoded string representation of the UUID
60
60
  * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
61
61
  *
62
62
  * @example
@@ -70,21 +70,9 @@ export function uuid58EncodeSafe(uuid) {
70
70
  * ```
71
71
  */
72
72
  export function uuid58Encode(uuid) {
73
- const hex = uuid.replaceAll("-", "");
74
- if (hex.length !== 32) {
75
- throw new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
73
+ const result = uuid58EncodeSafe(uuid);
74
+ if (result instanceof Uuid58EncodeError) {
75
+ throw result;
76
76
  }
77
- let num;
78
- try {
79
- num = BigInt("0x" + hex);
80
- }
81
- catch {
82
- throw new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
83
- }
84
- let encoded = "";
85
- do {
86
- encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
87
- num /= ALPHABET_LENGTH;
88
- } while (num > 0n);
89
- return encoded;
77
+ return result;
90
78
  }
package/esm/uuid58.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  /**
2
- * Generates a new Base58-encoded UUID.
2
+ * Generates a new Base58-encoded UUID (always 22 characters).
3
3
  *
4
4
  * This function combines the standard UUID generation (using crypto.randomUUID())
5
5
  * with Base58 encoding to create a shorter, URL-safe identifier.
6
6
  *
7
- * @returns A Base58-encoded string representing a newly generated UUID
7
+ * @returns A 22-character Base58-encoded string representing a newly generated UUID
8
8
  *
9
9
  * @example
10
10
  * ```typescript
11
- * const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
11
+ * const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
12
12
  * ```
13
13
  */
14
14
  export declare function uuid58(): string;
package/esm/uuid58.js CHANGED
@@ -1,15 +1,15 @@
1
1
  import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
2
2
  /**
3
- * Generates a new Base58-encoded UUID.
3
+ * Generates a new Base58-encoded UUID (always 22 characters).
4
4
  *
5
5
  * This function combines the standard UUID generation (using crypto.randomUUID())
6
6
  * with Base58 encoding to create a shorter, URL-safe identifier.
7
7
  *
8
- * @returns A Base58-encoded string representing a newly generated UUID
8
+ * @returns A 22-character Base58-encoded string representing a newly generated UUID
9
9
  *
10
10
  * @example
11
11
  * ```typescript
12
- * const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
12
+ * const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
13
13
  * ```
14
14
  */
15
15
  export function uuid58() {
@@ -19,5 +19,5 @@ export function uuid58() {
19
19
  encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
20
20
  num /= ALPHABET_LENGTH;
21
21
  } while (num > 0n);
22
- return encoded;
22
+ return encoded.padStart(22, BASE58_ALPHABET[0]);
23
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakanoaas/uuid58",
3
- "version": "0.0.7",
3
+ "version": "1.0.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",
@@ -5,13 +5,13 @@
5
5
  export declare class Uuid58DecodeError extends Error {
6
6
  }
7
7
  /**
8
- * Converts a Base58-encoded string back to a standard UUID format, but instead of
8
+ * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
9
9
  * throwing an error for invalid input, it returns an `Uuid58DecodeError`
10
10
  * instance.
11
11
  *
12
- * @param base58 - The Base58-encoded string to decode
12
+ * @param uuid58 - The 22-character Base58-encoded UUID string to decode
13
13
  * @returns A standard UUID string (lowercase, with hyphens), or an
14
- * `Uuid58DecodeError` if the input contains invalid Base58 characters
14
+ * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
15
15
  * @note This function does not throw; it returns the error object instead.
16
16
  *
17
17
  * @example
@@ -20,18 +20,18 @@ export declare class Uuid58DecodeError extends Error {
20
20
  * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
21
21
  * ```
22
22
  */
23
- export declare function uuid58DecodeSafe(base58: string): string | Uuid58DecodeError;
23
+ export declare function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
24
24
  /**
25
- * Converts a Base58-encoded string back to a standard UUID string format.
25
+ * Converts a 22-character Base58-encoded string back to a standard UUID string format.
26
26
  *
27
- * @param base58 - The Base58-encoded string to decode
27
+ * @param uuid58 - The 22-character Base58-encoded string to decode
28
28
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
29
- * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
29
+ * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
30
30
  *
31
31
  * @example
32
32
  * ```typescript
33
33
  * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
34
34
  * ```
35
35
  */
36
- export declare function uuid58Decode(base58: string): string;
36
+ export declare function uuid58Decode(uuid58: string): string;
37
37
  //# sourceMappingURL=decode.d.ts.map
@@ -1 +1 @@
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,CA4B3E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA4BnD"}
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/script/decode.js CHANGED
@@ -22,13 +22,13 @@ _a = Uuid58DecodeError;
22
22
  _a.prototype.name = "Uuid58DecodeError";
23
23
  })();
24
24
  /**
25
- * Converts a Base58-encoded string back to a standard UUID format, but instead of
25
+ * Converts a 22-character Base58-encoded string back to a standard UUID format, but instead of
26
26
  * throwing an error for invalid input, it returns an `Uuid58DecodeError`
27
27
  * instance.
28
28
  *
29
- * @param base58 - The Base58-encoded string to decode
29
+ * @param uuid58 - The 22-character Base58-encoded UUID string to decode
30
30
  * @returns A standard UUID string (lowercase, with hyphens), or an
31
- * `Uuid58DecodeError` if the input contains invalid Base58 characters
31
+ * `Uuid58DecodeError` if the input is not a valid 22-character Base58 string
32
32
  * @note This function does not throw; it returns the error object instead.
33
33
  *
34
34
  * @example
@@ -37,18 +37,21 @@ _a = Uuid58DecodeError;
37
37
  * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
38
38
  * ```
39
39
  */
40
- function uuid58DecodeSafe(base58) {
40
+ function uuid58DecodeSafe(uuid58) {
41
+ if (uuid58.length !== 22) {
42
+ return new Uuid58DecodeError(`Base58 string must be exactly 22 characters: ${uuid58}`);
43
+ }
41
44
  let num = 0n;
42
- for (const char of base58) {
45
+ for (const char of uuid58) {
43
46
  const index = BASE58_MAP[char];
44
47
  if (index === undefined) {
45
- return new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
48
+ return new Uuid58DecodeError(`Invalid Base58 character '${char}' found in input: ${uuid58}`);
46
49
  }
47
50
  num = num * alphabet_js_1.ALPHABET_LENGTH + index;
48
51
  }
49
52
  const hex = num.toString(16).padStart(32, "0");
50
53
  if (hex.length !== 32) {
51
- return new Uuid58DecodeError(`length not 32: ${base58}`);
54
+ return new Uuid58DecodeError(`Decoded hexadecimal string is not 32 characters: ${uuid58}`);
52
55
  }
53
56
  return (hex.slice(0, 8) +
54
57
  "-" +
@@ -61,37 +64,21 @@ function uuid58DecodeSafe(base58) {
61
64
  hex.slice(20));
62
65
  }
63
66
  /**
64
- * Converts a Base58-encoded string back to a standard UUID string format.
67
+ * Converts a 22-character Base58-encoded string back to a standard UUID string format.
65
68
  *
66
- * @param base58 - The Base58-encoded string to decode
69
+ * @param uuid58 - The 22-character Base58-encoded string to decode
67
70
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
68
- * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
71
+ * @throws {Uuid58DecodeError} If the input string is not a valid 22-character Base58 string
69
72
  *
70
73
  * @example
71
74
  * ```typescript
72
75
  * const uuid = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
73
76
  * ```
74
77
  */
75
- function uuid58Decode(base58) {
76
- let num = 0n;
77
- for (const char of base58) {
78
- const index = BASE58_MAP[char];
79
- if (index === undefined) {
80
- throw new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
81
- }
82
- num = num * alphabet_js_1.ALPHABET_LENGTH + index;
83
- }
84
- const hex = num.toString(16).padStart(32, "0");
85
- if (hex.length !== 32) {
86
- throw new Uuid58DecodeError(`length not 32: ${base58}`);
78
+ function uuid58Decode(uuid58) {
79
+ const result = uuid58DecodeSafe(uuid58);
80
+ if (result instanceof Uuid58DecodeError) {
81
+ throw result;
87
82
  }
88
- return (hex.slice(0, 8) +
89
- "-" +
90
- hex.slice(8, 12) +
91
- "-" +
92
- hex.slice(12, 16) +
93
- "-" +
94
- hex.slice(16, 20) +
95
- "-" +
96
- hex.slice(20));
83
+ return result;
97
84
  }
@@ -5,11 +5,11 @@
5
5
  export declare class Uuid58EncodeError extends Error {
6
6
  }
7
7
  /**
8
- * Converts a standard UUID string to a Base58-encoded format, but instead of
8
+ * Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
9
9
  * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
10
10
  *
11
11
  * @param uuid - The UUID string to encode (with or without hyphens)
12
- * @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
12
+ * @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
13
13
  * not a valid UUID
14
14
  * @note This function does not throw; it returns the error object instead.
15
15
  *
@@ -27,12 +27,12 @@ export declare class Uuid58EncodeError extends Error {
27
27
  */
28
28
  export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
29
29
  /**
30
- * Converts a standard UUID string to a shortened Base58-encoded format.
30
+ * Converts a standard UUID string to a 22-character Base58-encoded format.
31
31
  *
32
32
  * @param uuid - The UUID string to encode. Can be provided with or without hyphens
33
33
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
34
34
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
35
- * @returns A Base58-encoded string representation of the UUID
35
+ * @returns A 22-character Base58-encoded string representation of the UUID
36
36
  * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
37
37
  *
38
38
  * @example
@@ -1 +1 @@
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,CAwBjD"}
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"}
package/script/encode.js CHANGED
@@ -17,11 +17,11 @@ _a = Uuid58EncodeError;
17
17
  _a.prototype.name = "Uuid58EncodeError";
18
18
  })();
19
19
  /**
20
- * Converts a standard UUID string to a Base58-encoded format, but instead of
20
+ * Converts a standard UUID string to a 22-character Base58-encoded format, but instead of
21
21
  * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
22
22
  *
23
23
  * @param uuid - The UUID string to encode (with or without hyphens)
24
- * @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
24
+ * @returns A 22-character Base58-encoded string, or an `Uuid58EncodeError` if the input is
25
25
  * not a valid UUID
26
26
  * @note This function does not throw; it returns the error object instead.
27
27
  *
@@ -54,15 +54,15 @@ function uuid58EncodeSafe(uuid) {
54
54
  encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
55
55
  num /= alphabet_js_1.ALPHABET_LENGTH;
56
56
  } while (num > 0n);
57
- return encoded;
57
+ return encoded.padStart(22, alphabet_js_1.BASE58_ALPHABET[0]);
58
58
  }
59
59
  /**
60
- * Converts a standard UUID string to a shortened Base58-encoded format.
60
+ * Converts a standard UUID string to a 22-character Base58-encoded format.
61
61
  *
62
62
  * @param uuid - The UUID string to encode. Can be provided with or without hyphens
63
63
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
64
64
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
65
- * @returns A Base58-encoded string representation of the UUID
65
+ * @returns A 22-character Base58-encoded string representation of the UUID
66
66
  * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
67
67
  *
68
68
  * @example
@@ -76,21 +76,9 @@ function uuid58EncodeSafe(uuid) {
76
76
  * ```
77
77
  */
78
78
  function uuid58Encode(uuid) {
79
- const hex = uuid.replaceAll("-", "");
80
- if (hex.length !== 32) {
81
- throw new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
79
+ const result = uuid58EncodeSafe(uuid);
80
+ if (result instanceof Uuid58EncodeError) {
81
+ throw result;
82
82
  }
83
- let num;
84
- try {
85
- num = BigInt("0x" + hex);
86
- }
87
- catch {
88
- throw new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
89
- }
90
- let encoded = "";
91
- do {
92
- encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
93
- num /= alphabet_js_1.ALPHABET_LENGTH;
94
- } while (num > 0n);
95
- return encoded;
83
+ return result;
96
84
  }
@@ -1,14 +1,14 @@
1
1
  /**
2
- * Generates a new Base58-encoded UUID.
2
+ * Generates a new Base58-encoded UUID (always 22 characters).
3
3
  *
4
4
  * This function combines the standard UUID generation (using crypto.randomUUID())
5
5
  * with Base58 encoding to create a shorter, URL-safe identifier.
6
6
  *
7
- * @returns A Base58-encoded string representing a newly generated UUID
7
+ * @returns A 22-character Base58-encoded string representing a newly generated UUID
8
8
  *
9
9
  * @example
10
10
  * ```typescript
11
- * const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
11
+ * const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
12
12
  * ```
13
13
  */
14
14
  export declare function uuid58(): string;
package/script/uuid58.js CHANGED
@@ -3,16 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.uuid58 = uuid58;
4
4
  const alphabet_js_1 = require("./alphabet.js");
5
5
  /**
6
- * Generates a new Base58-encoded UUID.
6
+ * Generates a new Base58-encoded UUID (always 22 characters).
7
7
  *
8
8
  * This function combines the standard UUID generation (using crypto.randomUUID())
9
9
  * with Base58 encoding to create a shorter, URL-safe identifier.
10
10
  *
11
- * @returns A Base58-encoded string representing a newly generated UUID
11
+ * @returns A 22-character Base58-encoded string representing a newly generated UUID
12
12
  *
13
13
  * @example
14
14
  * ```typescript
15
- * const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
15
+ * const id = uuid58(); // returns a 22-character string like "XDY9dmBbcMBXqcRvYw8xJ2"
16
16
  * ```
17
17
  */
18
18
  function uuid58() {
@@ -22,5 +22,5 @@ function uuid58() {
22
22
  encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
23
23
  num /= alphabet_js_1.ALPHABET_LENGTH;
24
24
  } while (num > 0n);
25
- return encoded;
25
+ return encoded.padStart(22, alphabet_js_1.BASE58_ALPHABET[0]);
26
26
  }