@nakanoaas/uuid58 0.0.3 → 0.0.5

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
@@ -86,6 +86,21 @@ function uuid58Encode(uuid: string): string;
86
86
  - **Returns:** A Base58-encoded string
87
87
  - **Throws:** `InvalidUuidError` if the input is not a valid UUID
88
88
 
89
+ ### `uuid58EncodeSafe(uuid: string)`
90
+
91
+ Converts a standard UUID string to a Base58-encoded format, but instead of
92
+ throwing an error for invalid input, it returns an `InvalidUuidError` instance.
93
+
94
+ ```typescript
95
+ function uuid58EncodeSafe(uuid: string): string | InvalidUuidError;
96
+ ```
97
+
98
+ - **Parameters:**
99
+ - `uuid`: The UUID string to encode (with or without hyphens)
100
+ - **Returns:** A Base58-encoded string, or an `InvalidUuidError` if the input is
101
+ not a valid UUID
102
+ - **Note:** This function does not throw; it returns the error object instead.
103
+
89
104
  ### `uuid58Decode(encoded: string)`
90
105
 
91
106
  Converts a Base58-encoded string back to a standard UUID format.
@@ -100,6 +115,22 @@ function uuid58Decode(encoded: string): string;
100
115
  - **Throws:** `InvalidBase58Error` if the input contains invalid Base58
101
116
  characters
102
117
 
118
+ ### `uuid58DecodeSafe(encoded: string)`
119
+
120
+ Converts a Base58-encoded string back to a standard UUID format, but instead of
121
+ throwing an error for invalid input, it returns an `InvalidBase58Error`
122
+ instance.
123
+
124
+ ```typescript
125
+ function uuid58DecodeSafe(encoded: string): string | InvalidBase58Error;
126
+ ```
127
+
128
+ - **Parameters:**
129
+ - `encoded`: The Base58-encoded string to decode
130
+ - **Returns:** A standard UUID string (lowercase, with hyphens), or an
131
+ `InvalidBase58Error` if the input contains invalid Base58 characters
132
+ - **Note:** This function does not throw; it returns the error object instead.
133
+
103
134
  ## Why uuid58?
104
135
 
105
136
  Standard UUIDs are 36 characters long (including hyphens), which can be
package/esm/decode.d.ts CHANGED
@@ -5,6 +5,23 @@
5
5
  export declare class InvalidBase58Error extends Error {
6
6
  constructor(base58: string);
7
7
  }
8
+ /**
9
+ * Converts a Base58-encoded string back to a standard UUID format, but instead of
10
+ * throwing an error for invalid input, it returns an `InvalidBase58Error`
11
+ * instance.
12
+ *
13
+ * @param base58 - The Base58-encoded string to decode
14
+ * @returns A standard UUID string (lowercase, with hyphens), or an
15
+ * `InvalidBase58Error` if the input contains invalid Base58 characters
16
+ * @note This function does not throw; it returns the error object instead.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
21
+ * const error = uuid58DecodeSafe("invalid"); // returns InvalidBase58Error
22
+ * ```
23
+ */
24
+ export declare function uuid58DecodeSafe(base58: string): string | InvalidBase58Error;
8
25
  /**
9
26
  * Converts a Base58-encoded string back to a standard UUID string format.
10
27
  *
@@ -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,CAuBnD"}
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;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CA0B5E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA0BnD"}
package/esm/decode.js CHANGED
@@ -18,6 +18,45 @@ _a = InvalidBase58Error;
18
18
  (() => {
19
19
  _a.prototype.name = "InvalidBase58Error";
20
20
  })();
21
+ /**
22
+ * Converts a Base58-encoded string back to a standard UUID format, but instead of
23
+ * throwing an error for invalid input, it returns an `InvalidBase58Error`
24
+ * instance.
25
+ *
26
+ * @param base58 - The Base58-encoded string to decode
27
+ * @returns A standard UUID string (lowercase, with hyphens), or an
28
+ * `InvalidBase58Error` if the input contains invalid Base58 characters
29
+ * @note This function does not throw; it returns the error object instead.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
34
+ * const error = uuid58DecodeSafe("invalid"); // returns InvalidBase58Error
35
+ * ```
36
+ */
37
+ export function uuid58DecodeSafe(base58) {
38
+ let num = 0n;
39
+ for (const char of base58) {
40
+ const index = BASE58_MAP[char];
41
+ if (index === undefined) {
42
+ return new InvalidBase58Error(base58);
43
+ }
44
+ num = num * ALPHABET_LENGTH + index;
45
+ }
46
+ const hex = num.toString(16).padStart(32, "0");
47
+ if (hex.length !== 32) {
48
+ return new InvalidBase58Error(base58);
49
+ }
50
+ return (hex.slice(0, 8) +
51
+ "-" +
52
+ hex.slice(8, 12) +
53
+ "-" +
54
+ hex.slice(12, 16) +
55
+ "-" +
56
+ hex.slice(16, 20) +
57
+ "-" +
58
+ hex.slice(20));
59
+ }
21
60
  /**
22
61
  * Converts a Base58-encoded string back to a standard UUID string format.
23
62
  *
@@ -31,14 +70,18 @@ _a = InvalidBase58Error;
31
70
  * ```
32
71
  */
33
72
  export function uuid58Decode(base58) {
34
- if (!/^[1-9A-HJ-NP-Za-km-z]+$/.test(base58)) {
35
- throw new InvalidBase58Error(base58);
36
- }
37
73
  let num = 0n;
38
74
  for (const char of base58) {
39
- num = num * ALPHABET_LENGTH + BASE58_MAP[char];
75
+ const index = BASE58_MAP[char];
76
+ if (index === undefined) {
77
+ throw new InvalidBase58Error(base58);
78
+ }
79
+ num = num * ALPHABET_LENGTH + index;
40
80
  }
41
81
  const hex = num.toString(16).padStart(32, "0");
82
+ if (hex.length !== 32) {
83
+ throw new InvalidBase58Error(base58);
84
+ }
42
85
  return (hex.slice(0, 8) +
43
86
  "-" +
44
87
  hex.slice(8, 12) +
package/esm/encode.d.ts CHANGED
@@ -5,6 +5,28 @@
5
5
  export declare class InvalidUuidError extends Error {
6
6
  constructor(uuid: string);
7
7
  }
8
+ /**
9
+ * Converts a standard UUID string to a Base58-encoded format, but instead of
10
+ * throwing an error for invalid input, it returns an `InvalidUuidError` instance.
11
+ *
12
+ * @param uuid - The UUID string to encode (with or without hyphens)
13
+ * @returns A Base58-encoded string, or an `InvalidUuidError` if the input is
14
+ * not a valid UUID
15
+ * @note This function does not throw; it returns the error object instead.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // With hyphens
20
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
21
+ * // Without hyphens
22
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
23
+ * // Uppercase UUID is also accepted
24
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
25
+ * // Invalid UUID format
26
+ * const error = uuid58EncodeSafe("invalid"); // returns InvalidUuidError
27
+ * ```
28
+ */
29
+ export declare function uuid58EncodeSafe(uuid: string): string | InvalidUuidError;
8
30
  /**
9
31
  * Converts a standard UUID string to a shortened Base58-encoded format.
10
32
  *
@@ -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,CAejD"}
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;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,gBAAgB,CAoBxE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAoBjD"}
package/esm/encode.js CHANGED
@@ -13,6 +13,46 @@ _a = InvalidUuidError;
13
13
  (() => {
14
14
  _a.prototype.name = "InvalidUuidError";
15
15
  })();
16
+ /**
17
+ * Converts a standard UUID string to a Base58-encoded format, but instead of
18
+ * throwing an error for invalid input, it returns an `InvalidUuidError` instance.
19
+ *
20
+ * @param uuid - The UUID string to encode (with or without hyphens)
21
+ * @returns A Base58-encoded string, or an `InvalidUuidError` if the input is
22
+ * not a valid UUID
23
+ * @note This function does not throw; it returns the error object instead.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * // With hyphens
28
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
29
+ * // Without hyphens
30
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
31
+ * // Uppercase UUID is also accepted
32
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
33
+ * // Invalid UUID format
34
+ * const error = uuid58EncodeSafe("invalid"); // returns InvalidUuidError
35
+ * ```
36
+ */
37
+ export function uuid58EncodeSafe(uuid) {
38
+ const hex = uuid.replace(/-/g, "").toLowerCase();
39
+ if (hex.length !== 32) {
40
+ return new InvalidUuidError(uuid);
41
+ }
42
+ let num;
43
+ try {
44
+ num = BigInt("0x" + hex);
45
+ }
46
+ catch {
47
+ return new InvalidUuidError(uuid);
48
+ }
49
+ let encoded = "";
50
+ do {
51
+ encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
52
+ num /= ALPHABET_LENGTH;
53
+ } while (num > 0n);
54
+ return encoded;
55
+ }
16
56
  /**
17
57
  * Converts a standard UUID string to a shortened Base58-encoded format.
18
58
  *
@@ -34,14 +74,19 @@ _a = InvalidUuidError;
34
74
  */
35
75
  export function uuid58Encode(uuid) {
36
76
  const hex = uuid.replace(/-/g, "").toLowerCase();
37
- if (!/^[0-9a-f]{32}$/.test(hex)) {
77
+ if (hex.length !== 32) {
78
+ throw new InvalidUuidError(uuid);
79
+ }
80
+ let num;
81
+ try {
82
+ num = BigInt("0x" + hex);
83
+ }
84
+ catch {
38
85
  throw new InvalidUuidError(uuid);
39
86
  }
40
- let num = BigInt("0x" + hex);
41
87
  let encoded = "";
42
88
  do {
43
- const rem = Number(num % ALPHABET_LENGTH);
44
- encoded = BASE58_ALPHABET[rem] + encoded;
89
+ encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
45
90
  num /= ALPHABET_LENGTH;
46
91
  } while (num > 0n);
47
92
  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().replace(/-/g, ""));
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.3",
3
+ "version": "0.0.5",
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,6 +5,23 @@
5
5
  export declare class InvalidBase58Error extends Error {
6
6
  constructor(base58: string);
7
7
  }
8
+ /**
9
+ * Converts a Base58-encoded string back to a standard UUID format, but instead of
10
+ * throwing an error for invalid input, it returns an `InvalidBase58Error`
11
+ * instance.
12
+ *
13
+ * @param base58 - The Base58-encoded string to decode
14
+ * @returns A standard UUID string (lowercase, with hyphens), or an
15
+ * `InvalidBase58Error` if the input contains invalid Base58 characters
16
+ * @note This function does not throw; it returns the error object instead.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
21
+ * const error = uuid58DecodeSafe("invalid"); // returns InvalidBase58Error
22
+ * ```
23
+ */
24
+ export declare function uuid58DecodeSafe(base58: string): string | InvalidBase58Error;
8
25
  /**
9
26
  * Converts a Base58-encoded string back to a standard UUID string format.
10
27
  *
@@ -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,CAuBnD"}
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;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CA0B5E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA0BnD"}
package/script/decode.js CHANGED
@@ -2,6 +2,7 @@
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.InvalidBase58Error = 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
@@ -23,6 +24,45 @@ _a = InvalidBase58Error;
23
24
  (() => {
24
25
  _a.prototype.name = "InvalidBase58Error";
25
26
  })();
27
+ /**
28
+ * Converts a Base58-encoded string back to a standard UUID format, but instead of
29
+ * throwing an error for invalid input, it returns an `InvalidBase58Error`
30
+ * instance.
31
+ *
32
+ * @param base58 - The Base58-encoded string to decode
33
+ * @returns A standard UUID string (lowercase, with hyphens), or an
34
+ * `InvalidBase58Error` if the input contains invalid Base58 characters
35
+ * @note This function does not throw; it returns the error object instead.
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
40
+ * const error = uuid58DecodeSafe("invalid"); // returns InvalidBase58Error
41
+ * ```
42
+ */
43
+ function uuid58DecodeSafe(base58) {
44
+ let num = 0n;
45
+ for (const char of base58) {
46
+ const index = BASE58_MAP[char];
47
+ if (index === undefined) {
48
+ return new InvalidBase58Error(base58);
49
+ }
50
+ num = num * alphabet_js_1.ALPHABET_LENGTH + index;
51
+ }
52
+ const hex = num.toString(16).padStart(32, "0");
53
+ if (hex.length !== 32) {
54
+ return new InvalidBase58Error(base58);
55
+ }
56
+ return (hex.slice(0, 8) +
57
+ "-" +
58
+ hex.slice(8, 12) +
59
+ "-" +
60
+ hex.slice(12, 16) +
61
+ "-" +
62
+ hex.slice(16, 20) +
63
+ "-" +
64
+ hex.slice(20));
65
+ }
26
66
  /**
27
67
  * Converts a Base58-encoded string back to a standard UUID string format.
28
68
  *
@@ -36,14 +76,18 @@ _a = InvalidBase58Error;
36
76
  * ```
37
77
  */
38
78
  function uuid58Decode(base58) {
39
- if (!/^[1-9A-HJ-NP-Za-km-z]+$/.test(base58)) {
40
- throw new InvalidBase58Error(base58);
41
- }
42
79
  let num = 0n;
43
80
  for (const char of base58) {
44
- num = num * alphabet_js_1.ALPHABET_LENGTH + BASE58_MAP[char];
81
+ const index = BASE58_MAP[char];
82
+ if (index === undefined) {
83
+ throw new InvalidBase58Error(base58);
84
+ }
85
+ num = num * alphabet_js_1.ALPHABET_LENGTH + index;
45
86
  }
46
87
  const hex = num.toString(16).padStart(32, "0");
88
+ if (hex.length !== 32) {
89
+ throw new InvalidBase58Error(base58);
90
+ }
47
91
  return (hex.slice(0, 8) +
48
92
  "-" +
49
93
  hex.slice(8, 12) +
@@ -5,6 +5,28 @@
5
5
  export declare class InvalidUuidError extends Error {
6
6
  constructor(uuid: string);
7
7
  }
8
+ /**
9
+ * Converts a standard UUID string to a Base58-encoded format, but instead of
10
+ * throwing an error for invalid input, it returns an `InvalidUuidError` instance.
11
+ *
12
+ * @param uuid - The UUID string to encode (with or without hyphens)
13
+ * @returns A Base58-encoded string, or an `InvalidUuidError` if the input is
14
+ * not a valid UUID
15
+ * @note This function does not throw; it returns the error object instead.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // With hyphens
20
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
21
+ * // Without hyphens
22
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
23
+ * // Uppercase UUID is also accepted
24
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
25
+ * // Invalid UUID format
26
+ * const error = uuid58EncodeSafe("invalid"); // returns InvalidUuidError
27
+ * ```
28
+ */
29
+ export declare function uuid58EncodeSafe(uuid: string): string | InvalidUuidError;
8
30
  /**
9
31
  * Converts a standard UUID string to a shortened Base58-encoded format.
10
32
  *
@@ -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,CAejD"}
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;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,gBAAgB,CAoBxE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAoBjD"}
package/script/encode.js CHANGED
@@ -2,6 +2,7 @@
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.InvalidUuidError = void 0;
5
+ exports.uuid58EncodeSafe = uuid58EncodeSafe;
5
6
  exports.uuid58Encode = uuid58Encode;
6
7
  const alphabet_js_1 = require("./alphabet.js");
7
8
  /**
@@ -18,6 +19,46 @@ _a = InvalidUuidError;
18
19
  (() => {
19
20
  _a.prototype.name = "InvalidUuidError";
20
21
  })();
22
+ /**
23
+ * Converts a standard UUID string to a Base58-encoded format, but instead of
24
+ * throwing an error for invalid input, it returns an `InvalidUuidError` instance.
25
+ *
26
+ * @param uuid - The UUID string to encode (with or without hyphens)
27
+ * @returns A Base58-encoded string, or an `InvalidUuidError` if the input is
28
+ * not a valid UUID
29
+ * @note This function does not throw; it returns the error object instead.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * // With hyphens
34
+ * const base58 = uuid58EncodeSafe("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
35
+ * // Without hyphens
36
+ * const base58 = uuid58EncodeSafe("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
37
+ * // Uppercase UUID is also accepted
38
+ * const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
39
+ * // Invalid UUID format
40
+ * const error = uuid58EncodeSafe("invalid"); // returns InvalidUuidError
41
+ * ```
42
+ */
43
+ function uuid58EncodeSafe(uuid) {
44
+ const hex = uuid.replace(/-/g, "").toLowerCase();
45
+ if (hex.length !== 32) {
46
+ return new InvalidUuidError(uuid);
47
+ }
48
+ let num;
49
+ try {
50
+ num = BigInt("0x" + hex);
51
+ }
52
+ catch {
53
+ return new InvalidUuidError(uuid);
54
+ }
55
+ let encoded = "";
56
+ do {
57
+ encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
58
+ num /= alphabet_js_1.ALPHABET_LENGTH;
59
+ } while (num > 0n);
60
+ return encoded;
61
+ }
21
62
  /**
22
63
  * Converts a standard UUID string to a shortened Base58-encoded format.
23
64
  *
@@ -39,14 +80,19 @@ _a = InvalidUuidError;
39
80
  */
40
81
  function uuid58Encode(uuid) {
41
82
  const hex = uuid.replace(/-/g, "").toLowerCase();
42
- if (!/^[0-9a-f]{32}$/.test(hex)) {
83
+ if (hex.length !== 32) {
84
+ throw new InvalidUuidError(uuid);
85
+ }
86
+ let num;
87
+ try {
88
+ num = BigInt("0x" + hex);
89
+ }
90
+ catch {
43
91
  throw new InvalidUuidError(uuid);
44
92
  }
45
- let num = BigInt("0x" + hex);
46
93
  let encoded = "";
47
94
  do {
48
- const rem = Number(num % alphabet_js_1.ALPHABET_LENGTH);
49
- encoded = alphabet_js_1.BASE58_ALPHABET[rem] + encoded;
95
+ encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
50
96
  num /= alphabet_js_1.ALPHABET_LENGTH;
51
97
  } while (num > 0n);
52
98
  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().replace(/-/g, ""));
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
  }