@nakanoaas/uuid58 0.0.4 → 0.0.6

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
@@ -61,6 +61,33 @@ const encoded = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf");
61
61
  // Convert Base58 back to UUID
62
62
  const decoded = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2");
63
63
  // => "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
64
+
65
+ import {
66
+ Uuid58DecodeError,
67
+ uuid58DecodeSafe,
68
+ Uuid58EncodeError,
69
+ uuid58EncodeSafe,
70
+ } from "@nakanoaas/uuid58";
71
+
72
+ // Safe encoding: returns error object instead of throwing
73
+ const encodedSafe = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf");
74
+ if (encodedSafe instanceof Uuid58EncodeError) {
75
+ // handle error
76
+ console.error(encodedSafe.message);
77
+ return;
78
+ }
79
+ // use encoded string
80
+ console.log(encodedSafe);
81
+
82
+ // Safe decoding: returns error object instead of throwing
83
+ const decodedSafe = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2");
84
+ if (decodedSafe instanceof Uuid58DecodeError) {
85
+ // handle error
86
+ console.error(decodedSafe.message);
87
+ return;
88
+ }
89
+ // use decoded UUID
90
+ console.log(decodedSafe);
64
91
  ```
65
92
 
66
93
  ## API Reference
@@ -84,7 +111,22 @@ function uuid58Encode(uuid: string): string;
84
111
  - **Parameters:**
85
112
  - `uuid`: The UUID string to encode (with or without hyphens)
86
113
  - **Returns:** A Base58-encoded string
87
- - **Throws:** `InvalidUuidError` if the input is not a valid UUID
114
+ - **Throws:** `Uuid58EncodeError` if the input is not a valid UUID
115
+
116
+ ### `uuid58EncodeSafe(uuid: string)`
117
+
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.
120
+
121
+ ```typescript
122
+ function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
123
+ ```
124
+
125
+ - **Parameters:**
126
+ - `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
+ - **Note:** This function does not throw; it returns the error object instead.
88
130
 
89
131
  ### `uuid58Decode(encoded: string)`
90
132
 
@@ -97,9 +139,24 @@ function uuid58Decode(encoded: string): string;
97
139
  - **Parameters:**
98
140
  - `encoded`: The Base58-encoded string to decode
99
141
  - **Returns:** A standard UUID string (lowercase, with hyphens)
100
- - **Throws:** `InvalidBase58Error` if the input contains invalid Base58
142
+ - **Throws:** `Uuid58DecodeError` if the input contains invalid Base58
101
143
  characters
102
144
 
145
+ ### `uuid58DecodeSafe(encoded: string)`
146
+
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
+
150
+ ```typescript
151
+ function uuid58DecodeSafe(encoded: string): string | Uuid58DecodeError;
152
+ ```
153
+
154
+ - **Parameters:**
155
+ - `encoded`: The Base58-encoded string to decode
156
+ - **Returns:** A standard UUID string (lowercase, with hyphens), or an
157
+ `Uuid58DecodeError` if the input contains invalid Base58 characters
158
+ - **Note:** This function does not throw; it returns the error object instead.
159
+
103
160
  ## Why uuid58?
104
161
 
105
162
  Standard UUIDs are 36 characters long (including hyphens), which can be
package/esm/decode.d.ts CHANGED
@@ -2,15 +2,31 @@
2
2
  * Error thrown when an invalid Base58 string is provided for decoding.
3
3
  * This includes strings containing characters not in the Base58 alphabet.
4
4
  */
5
- export declare class InvalidBase58Error extends Error {
6
- constructor(base58: string);
5
+ export declare class Uuid58DecodeError extends Error {
7
6
  }
7
+ /**
8
+ * Converts a 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 base58 - The Base58-encoded string to decode
13
+ * @returns A standard UUID string (lowercase, with hyphens), or an
14
+ * `Uuid58DecodeError` if the input contains invalid Base58 characters
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(base58: string): string | Uuid58DecodeError;
8
24
  /**
9
25
  * Converts a Base58-encoded string back to a standard UUID string format.
10
26
  *
11
27
  * @param base58 - The Base58-encoded string to decode
12
28
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
13
- * @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
29
+ * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
14
30
  *
15
31
  * @example
16
32
  * ```typescript
@@ -1 +1 @@
1
- {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;gBAK/B,MAAM,EAAE,MAAM;CAG3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA0BnD"}
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,CA0BnD"}
package/esm/decode.js CHANGED
@@ -9,21 +9,57 @@ for (let i = 0; i < BASE58_ALPHABET.length; i++) {
9
9
  * Error thrown when an invalid Base58 string is provided for decoding.
10
10
  * This includes strings containing characters not in the Base58 alphabet.
11
11
  */
12
- export class InvalidBase58Error extends Error {
13
- constructor(base58) {
14
- super(`Invalid Base58 string: ${base58}`);
15
- }
12
+ export class Uuid58DecodeError extends Error {
16
13
  }
17
- _a = InvalidBase58Error;
14
+ _a = Uuid58DecodeError;
18
15
  (() => {
19
- _a.prototype.name = "InvalidBase58Error";
16
+ _a.prototype.name = "Uuid58DecodeError";
20
17
  })();
18
+ /**
19
+ * Converts a 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 base58 - The Base58-encoded string to decode
24
+ * @returns A standard UUID string (lowercase, with hyphens), or an
25
+ * `Uuid58DecodeError` if the input contains invalid Base58 characters
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(base58) {
35
+ let num = 0n;
36
+ for (const char of base58) {
37
+ const index = BASE58_MAP[char];
38
+ if (index === undefined) {
39
+ return new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
40
+ }
41
+ num = num * ALPHABET_LENGTH + index;
42
+ }
43
+ const hex = num.toString(16).padStart(32, "0");
44
+ if (hex.length !== 32) {
45
+ return new Uuid58DecodeError(`length not 32: ${base58}`);
46
+ }
47
+ return (hex.slice(0, 8) +
48
+ "-" +
49
+ hex.slice(8, 12) +
50
+ "-" +
51
+ hex.slice(12, 16) +
52
+ "-" +
53
+ hex.slice(16, 20) +
54
+ "-" +
55
+ hex.slice(20));
56
+ }
21
57
  /**
22
58
  * Converts a Base58-encoded string back to a standard UUID string format.
23
59
  *
24
60
  * @param base58 - The Base58-encoded string to decode
25
61
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
26
- * @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
62
+ * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
27
63
  *
28
64
  * @example
29
65
  * ```typescript
@@ -35,13 +71,13 @@ export function uuid58Decode(base58) {
35
71
  for (const char of base58) {
36
72
  const index = BASE58_MAP[char];
37
73
  if (index === undefined) {
38
- throw new InvalidBase58Error(base58);
74
+ throw new Uuid58DecodeError(base58);
39
75
  }
40
76
  num = num * ALPHABET_LENGTH + index;
41
77
  }
42
78
  const hex = num.toString(16).padStart(32, "0");
43
79
  if (hex.length !== 32) {
44
- throw new InvalidBase58Error(base58);
80
+ throw new Uuid58DecodeError(base58);
45
81
  }
46
82
  return (hex.slice(0, 8) +
47
83
  "-" +
package/esm/encode.d.ts CHANGED
@@ -2,9 +2,30 @@
2
2
  * Error thrown when an invalid UUID string is provided for encoding.
3
3
  * This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
4
4
  */
5
- export declare class InvalidUuidError extends Error {
6
- constructor(uuid: string);
5
+ export declare class Uuid58EncodeError extends Error {
7
6
  }
7
+ /**
8
+ * Converts a standard UUID string to a 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 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;
8
29
  /**
9
30
  * Converts a standard UUID string to a shortened Base58-encoded format.
10
31
  *
@@ -12,7 +33,7 @@ export declare class InvalidUuidError extends Error {
12
33
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
13
34
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
14
35
  * @returns A Base58-encoded string representation of the UUID
15
- * @throws {InvalidUuidError} If the input string is not a valid UUID format
36
+ * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
16
37
  *
17
38
  * @example
18
39
  * ```typescript
@@ -1 +1 @@
1
- {"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAK7B,IAAI,EAAE,MAAM;CAGzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAqBjD"}
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,CAoBjD"}
package/esm/encode.js CHANGED
@@ -4,15 +4,52 @@ import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
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).
6
6
  */
7
- export class InvalidUuidError extends Error {
8
- constructor(uuid) {
9
- super(`Invalid UUID format: ${uuid}`);
10
- }
7
+ export class Uuid58EncodeError extends Error {
11
8
  }
12
- _a = InvalidUuidError;
9
+ _a = Uuid58EncodeError;
13
10
  (() => {
14
- _a.prototype.name = "InvalidUuidError";
11
+ _a.prototype.name = "Uuid58EncodeError";
15
12
  })();
13
+ /**
14
+ * Converts a standard UUID string to a Base58-encoded format, but instead of
15
+ * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
16
+ *
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
19
+ * not a valid UUID
20
+ * @note This function does not throw; it returns the error object instead.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * // With hyphens
25
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
26
+ * // Without hyphens
27
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
28
+ * // Uppercase UUID is also accepted
29
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
30
+ * // Invalid UUID format
31
+ * const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
32
+ * ```
33
+ */
34
+ export function uuid58EncodeSafe(uuid) {
35
+ const hex = uuid.replaceAll("-", "");
36
+ if (hex.length !== 32) {
37
+ return new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
38
+ }
39
+ let num;
40
+ try {
41
+ num = BigInt("0x" + hex);
42
+ }
43
+ catch {
44
+ return new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
45
+ }
46
+ let encoded = "";
47
+ do {
48
+ encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
49
+ num /= ALPHABET_LENGTH;
50
+ } while (num > 0n);
51
+ return encoded;
52
+ }
16
53
  /**
17
54
  * Converts a standard UUID string to a shortened Base58-encoded format.
18
55
  *
@@ -20,7 +57,7 @@ _a = InvalidUuidError;
20
57
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
21
58
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
22
59
  * @returns A Base58-encoded string representation of the UUID
23
- * @throws {InvalidUuidError} If the input string is not a valid UUID format
60
+ * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
24
61
  *
25
62
  * @example
26
63
  * ```typescript
@@ -33,21 +70,20 @@ _a = InvalidUuidError;
33
70
  * ```
34
71
  */
35
72
  export function uuid58Encode(uuid) {
36
- const hex = uuid.replace(/-/g, "").toLowerCase();
73
+ const hex = uuid.replaceAll("-", "");
37
74
  if (hex.length !== 32) {
38
- throw new InvalidUuidError(uuid);
75
+ throw new Uuid58EncodeError(uuid);
39
76
  }
40
77
  let num;
41
78
  try {
42
79
  num = BigInt("0x" + hex);
43
80
  }
44
81
  catch {
45
- throw new InvalidUuidError(uuid);
82
+ throw new Uuid58EncodeError(uuid);
46
83
  }
47
84
  let encoded = "";
48
85
  do {
49
- const rem = Number(num % ALPHABET_LENGTH);
50
- encoded = BASE58_ALPHABET[rem] + encoded;
86
+ encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
51
87
  num /= ALPHABET_LENGTH;
52
88
  } while (num > 0n);
53
89
  return encoded;
@@ -1 +1 @@
1
- {"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAE/B"}
1
+ {"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAU/B"}
package/esm/uuid58.js CHANGED
@@ -1,4 +1,4 @@
1
- import { uuid58Encode } from "./encode.js";
1
+ import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
2
2
  /**
3
3
  * Generates a new Base58-encoded UUID.
4
4
  *
@@ -13,5 +13,11 @@ import { uuid58Encode } from "./encode.js";
13
13
  * ```
14
14
  */
15
15
  export function uuid58() {
16
- return uuid58Encode(crypto.randomUUID());
16
+ let num = BigInt("0x" + crypto.randomUUID().replaceAll("-", ""));
17
+ let encoded = "";
18
+ do {
19
+ encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
20
+ num /= ALPHABET_LENGTH;
21
+ } while (num > 0n);
22
+ return encoded;
17
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakanoaas/uuid58",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "A tiny, zero-dependency library for generating and converting UUIDs to Base58-encoded strings.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -2,15 +2,31 @@
2
2
  * Error thrown when an invalid Base58 string is provided for decoding.
3
3
  * This includes strings containing characters not in the Base58 alphabet.
4
4
  */
5
- export declare class InvalidBase58Error extends Error {
6
- constructor(base58: string);
5
+ export declare class Uuid58DecodeError extends Error {
7
6
  }
7
+ /**
8
+ * Converts a 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 base58 - The Base58-encoded string to decode
13
+ * @returns A standard UUID string (lowercase, with hyphens), or an
14
+ * `Uuid58DecodeError` if the input contains invalid Base58 characters
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(base58: string): string | Uuid58DecodeError;
8
24
  /**
9
25
  * Converts a Base58-encoded string back to a standard UUID string format.
10
26
  *
11
27
  * @param base58 - The Base58-encoded string to decode
12
28
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
13
- * @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
29
+ * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
14
30
  *
15
31
  * @example
16
32
  * ```typescript
@@ -1 +1 @@
1
- {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;gBAK/B,MAAM,EAAE,MAAM;CAG3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA0BnD"}
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,CA0BnD"}
package/script/decode.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.InvalidBase58Error = void 0;
4
+ exports.Uuid58DecodeError = void 0;
5
+ exports.uuid58DecodeSafe = uuid58DecodeSafe;
5
6
  exports.uuid58Decode = uuid58Decode;
6
7
  const alphabet_js_1 = require("./alphabet.js");
7
8
  // Fast lookup map from alphabet characters to their indices
@@ -13,22 +14,58 @@ for (let i = 0; i < alphabet_js_1.BASE58_ALPHABET.length; i++) {
13
14
  * Error thrown when an invalid Base58 string is provided for decoding.
14
15
  * This includes strings containing characters not in the Base58 alphabet.
15
16
  */
16
- class InvalidBase58Error extends Error {
17
- constructor(base58) {
18
- super(`Invalid Base58 string: ${base58}`);
19
- }
17
+ class Uuid58DecodeError extends Error {
20
18
  }
21
- exports.InvalidBase58Error = InvalidBase58Error;
22
- _a = InvalidBase58Error;
19
+ exports.Uuid58DecodeError = Uuid58DecodeError;
20
+ _a = Uuid58DecodeError;
23
21
  (() => {
24
- _a.prototype.name = "InvalidBase58Error";
22
+ _a.prototype.name = "Uuid58DecodeError";
25
23
  })();
24
+ /**
25
+ * Converts a Base58-encoded string back to a standard UUID format, but instead of
26
+ * throwing an error for invalid input, it returns an `Uuid58DecodeError`
27
+ * instance.
28
+ *
29
+ * @param base58 - The Base58-encoded string to decode
30
+ * @returns A standard UUID string (lowercase, with hyphens), or an
31
+ * `Uuid58DecodeError` if the input contains invalid Base58 characters
32
+ * @note This function does not throw; it returns the error object instead.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
37
+ * const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
38
+ * ```
39
+ */
40
+ function uuid58DecodeSafe(base58) {
41
+ let num = 0n;
42
+ for (const char of base58) {
43
+ const index = BASE58_MAP[char];
44
+ if (index === undefined) {
45
+ return new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
46
+ }
47
+ num = num * alphabet_js_1.ALPHABET_LENGTH + index;
48
+ }
49
+ const hex = num.toString(16).padStart(32, "0");
50
+ if (hex.length !== 32) {
51
+ return new Uuid58DecodeError(`length not 32: ${base58}`);
52
+ }
53
+ return (hex.slice(0, 8) +
54
+ "-" +
55
+ hex.slice(8, 12) +
56
+ "-" +
57
+ hex.slice(12, 16) +
58
+ "-" +
59
+ hex.slice(16, 20) +
60
+ "-" +
61
+ hex.slice(20));
62
+ }
26
63
  /**
27
64
  * Converts a Base58-encoded string back to a standard UUID string format.
28
65
  *
29
66
  * @param base58 - The Base58-encoded string to decode
30
67
  * @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
31
- * @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
68
+ * @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
32
69
  *
33
70
  * @example
34
71
  * ```typescript
@@ -40,13 +77,13 @@ function uuid58Decode(base58) {
40
77
  for (const char of base58) {
41
78
  const index = BASE58_MAP[char];
42
79
  if (index === undefined) {
43
- throw new InvalidBase58Error(base58);
80
+ throw new Uuid58DecodeError(base58);
44
81
  }
45
82
  num = num * alphabet_js_1.ALPHABET_LENGTH + index;
46
83
  }
47
84
  const hex = num.toString(16).padStart(32, "0");
48
85
  if (hex.length !== 32) {
49
- throw new InvalidBase58Error(base58);
86
+ throw new Uuid58DecodeError(base58);
50
87
  }
51
88
  return (hex.slice(0, 8) +
52
89
  "-" +
@@ -2,9 +2,30 @@
2
2
  * Error thrown when an invalid UUID string is provided for encoding.
3
3
  * This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
4
4
  */
5
- export declare class InvalidUuidError extends Error {
6
- constructor(uuid: string);
5
+ export declare class Uuid58EncodeError extends Error {
7
6
  }
7
+ /**
8
+ * Converts a standard UUID string to a 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 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;
8
29
  /**
9
30
  * Converts a standard UUID string to a shortened Base58-encoded format.
10
31
  *
@@ -12,7 +33,7 @@ export declare class InvalidUuidError extends Error {
12
33
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
13
34
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
14
35
  * @returns A Base58-encoded string representation of the UUID
15
- * @throws {InvalidUuidError} If the input string is not a valid UUID format
36
+ * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
16
37
  *
17
38
  * @example
18
39
  * ```typescript
@@ -1 +1 @@
1
- {"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAK7B,IAAI,EAAE,MAAM;CAGzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAqBjD"}
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,CAoBjD"}
package/script/encode.js CHANGED
@@ -1,23 +1,61 @@
1
1
  "use strict";
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.InvalidUuidError = void 0;
4
+ exports.Uuid58EncodeError = void 0;
5
+ exports.uuid58EncodeSafe = uuid58EncodeSafe;
5
6
  exports.uuid58Encode = uuid58Encode;
6
7
  const alphabet_js_1 = require("./alphabet.js");
7
8
  /**
8
9
  * Error thrown when an invalid UUID string is provided for encoding.
9
10
  * This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
10
11
  */
11
- class InvalidUuidError extends Error {
12
- constructor(uuid) {
13
- super(`Invalid UUID format: ${uuid}`);
14
- }
12
+ class Uuid58EncodeError extends Error {
15
13
  }
16
- exports.InvalidUuidError = InvalidUuidError;
17
- _a = InvalidUuidError;
14
+ exports.Uuid58EncodeError = Uuid58EncodeError;
15
+ _a = Uuid58EncodeError;
18
16
  (() => {
19
- _a.prototype.name = "InvalidUuidError";
17
+ _a.prototype.name = "Uuid58EncodeError";
20
18
  })();
19
+ /**
20
+ * Converts a standard UUID string to a Base58-encoded format, but instead of
21
+ * throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
22
+ *
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
25
+ * not a valid UUID
26
+ * @note This function does not throw; it returns the error object instead.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * // With hyphens
31
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
32
+ * // Without hyphens
33
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
34
+ * // Uppercase UUID is also accepted
35
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
36
+ * // Invalid UUID format
37
+ * const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
38
+ * ```
39
+ */
40
+ function uuid58EncodeSafe(uuid) {
41
+ const hex = uuid.replaceAll("-", "");
42
+ if (hex.length !== 32) {
43
+ return new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
44
+ }
45
+ let num;
46
+ try {
47
+ num = BigInt("0x" + hex);
48
+ }
49
+ catch {
50
+ return new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
51
+ }
52
+ let encoded = "";
53
+ do {
54
+ encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
55
+ num /= alphabet_js_1.ALPHABET_LENGTH;
56
+ } while (num > 0n);
57
+ return encoded;
58
+ }
21
59
  /**
22
60
  * Converts a standard UUID string to a shortened Base58-encoded format.
23
61
  *
@@ -25,7 +63,7 @@ _a = InvalidUuidError;
25
63
  * (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
26
64
  * The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
27
65
  * @returns A Base58-encoded string representation of the UUID
28
- * @throws {InvalidUuidError} If the input string is not a valid UUID format
66
+ * @throws {Uuid58EncodeError} If the input string is not a valid UUID format
29
67
  *
30
68
  * @example
31
69
  * ```typescript
@@ -38,21 +76,20 @@ _a = InvalidUuidError;
38
76
  * ```
39
77
  */
40
78
  function uuid58Encode(uuid) {
41
- const hex = uuid.replace(/-/g, "").toLowerCase();
79
+ const hex = uuid.replaceAll("-", "");
42
80
  if (hex.length !== 32) {
43
- throw new InvalidUuidError(uuid);
81
+ throw new Uuid58EncodeError(uuid);
44
82
  }
45
83
  let num;
46
84
  try {
47
85
  num = BigInt("0x" + hex);
48
86
  }
49
87
  catch {
50
- throw new InvalidUuidError(uuid);
88
+ throw new Uuid58EncodeError(uuid);
51
89
  }
52
90
  let encoded = "";
53
91
  do {
54
- const rem = Number(num % alphabet_js_1.ALPHABET_LENGTH);
55
- encoded = alphabet_js_1.BASE58_ALPHABET[rem] + encoded;
92
+ encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
56
93
  num /= alphabet_js_1.ALPHABET_LENGTH;
57
94
  } while (num > 0n);
58
95
  return encoded;
@@ -1 +1 @@
1
- {"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAE/B"}
1
+ {"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAU/B"}
package/script/uuid58.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.uuid58 = uuid58;
4
- const encode_js_1 = require("./encode.js");
4
+ const alphabet_js_1 = require("./alphabet.js");
5
5
  /**
6
6
  * Generates a new Base58-encoded UUID.
7
7
  *
@@ -16,5 +16,11 @@ const encode_js_1 = require("./encode.js");
16
16
  * ```
17
17
  */
18
18
  function uuid58() {
19
- return (0, encode_js_1.uuid58Encode)(crypto.randomUUID());
19
+ let num = BigInt("0x" + crypto.randomUUID().replaceAll("-", ""));
20
+ let encoded = "";
21
+ do {
22
+ encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
23
+ num /= alphabet_js_1.ALPHABET_LENGTH;
24
+ } while (num > 0n);
25
+ return encoded;
20
26
  }