@nakanoaas/uuid58 0.0.5 → 0.0.7
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 +36 -10
- package/esm/decode.d.ts +6 -7
- package/esm/decode.d.ts.map +1 -1
- package/esm/decode.js +11 -14
- package/esm/encode.d.ts +6 -7
- package/esm/encode.d.ts.map +1 -1
- package/esm/encode.js +13 -16
- package/esm/uuid58.js +1 -1
- package/package.json +1 -1
- package/script/decode.d.ts +6 -7
- package/script/decode.d.ts.map +1 -1
- package/script/decode.js +13 -16
- package/script/encode.d.ts +6 -7
- package/script/encode.d.ts.map +1 -1
- package/script/encode.js +15 -18
- package/script/uuid58.js +1 -1
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,21 +111,21 @@ 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:** `
|
|
114
|
+
- **Throws:** `Uuid58EncodeError` if the input is not a valid UUID
|
|
88
115
|
|
|
89
116
|
### `uuid58EncodeSafe(uuid: string)`
|
|
90
117
|
|
|
91
118
|
Converts a standard UUID string to a Base58-encoded format, but instead of
|
|
92
|
-
throwing an error for invalid input, it returns an `
|
|
119
|
+
throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
93
120
|
|
|
94
121
|
```typescript
|
|
95
|
-
function uuid58EncodeSafe(uuid: string): string |
|
|
122
|
+
function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
|
|
96
123
|
```
|
|
97
124
|
|
|
98
125
|
- **Parameters:**
|
|
99
126
|
- `uuid`: The UUID string to encode (with or without hyphens)
|
|
100
|
-
- **Returns:** A Base58-encoded string, or an `
|
|
101
|
-
not a valid UUID
|
|
127
|
+
- **Returns:** A Base58-encoded string, or an `Uuid58EncodeError` if the input
|
|
128
|
+
is not a valid UUID
|
|
102
129
|
- **Note:** This function does not throw; it returns the error object instead.
|
|
103
130
|
|
|
104
131
|
### `uuid58Decode(encoded: string)`
|
|
@@ -112,23 +139,22 @@ function uuid58Decode(encoded: string): string;
|
|
|
112
139
|
- **Parameters:**
|
|
113
140
|
- `encoded`: The Base58-encoded string to decode
|
|
114
141
|
- **Returns:** A standard UUID string (lowercase, with hyphens)
|
|
115
|
-
- **Throws:** `
|
|
142
|
+
- **Throws:** `Uuid58DecodeError` if the input contains invalid Base58
|
|
116
143
|
characters
|
|
117
144
|
|
|
118
145
|
### `uuid58DecodeSafe(encoded: string)`
|
|
119
146
|
|
|
120
147
|
Converts a Base58-encoded string back to a standard UUID format, but instead of
|
|
121
|
-
throwing an error for invalid input, it returns an `
|
|
122
|
-
instance.
|
|
148
|
+
throwing an error for invalid input, it returns an `Uuid58DecodeError` instance.
|
|
123
149
|
|
|
124
150
|
```typescript
|
|
125
|
-
function uuid58DecodeSafe(encoded: string): string |
|
|
151
|
+
function uuid58DecodeSafe(encoded: string): string | Uuid58DecodeError;
|
|
126
152
|
```
|
|
127
153
|
|
|
128
154
|
- **Parameters:**
|
|
129
155
|
- `encoded`: The Base58-encoded string to decode
|
|
130
156
|
- **Returns:** A standard UUID string (lowercase, with hyphens), or an
|
|
131
|
-
`
|
|
157
|
+
`Uuid58DecodeError` if the input contains invalid Base58 characters
|
|
132
158
|
- **Note:** This function does not throw; it returns the error object instead.
|
|
133
159
|
|
|
134
160
|
## Why uuid58?
|
package/esm/decode.d.ts
CHANGED
|
@@ -2,32 +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
|
|
6
|
-
constructor(base58: string);
|
|
5
|
+
export declare class Uuid58DecodeError extends Error {
|
|
7
6
|
}
|
|
8
7
|
/**
|
|
9
8
|
* Converts a Base58-encoded string back to a standard UUID format, but instead of
|
|
10
|
-
* throwing an error for invalid input, it returns an `
|
|
9
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
11
10
|
* instance.
|
|
12
11
|
*
|
|
13
12
|
* @param base58 - The Base58-encoded string to decode
|
|
14
13
|
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
15
|
-
* `
|
|
14
|
+
* `Uuid58DecodeError` if the input contains invalid Base58 characters
|
|
16
15
|
* @note This function does not throw; it returns the error object instead.
|
|
17
16
|
*
|
|
18
17
|
* @example
|
|
19
18
|
* ```typescript
|
|
20
19
|
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
21
|
-
* const error = uuid58DecodeSafe("invalid"); // returns
|
|
20
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
22
21
|
* ```
|
|
23
22
|
*/
|
|
24
|
-
export declare function uuid58DecodeSafe(base58: string): string |
|
|
23
|
+
export declare function uuid58DecodeSafe(base58: string): string | Uuid58DecodeError;
|
|
25
24
|
/**
|
|
26
25
|
* Converts a Base58-encoded string back to a standard UUID string format.
|
|
27
26
|
*
|
|
28
27
|
* @param base58 - The Base58-encoded string to decode
|
|
29
28
|
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
30
|
-
* @throws {
|
|
29
|
+
* @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
|
|
31
30
|
*
|
|
32
31
|
* @example
|
|
33
32
|
* ```typescript
|
package/esm/decode.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,
|
|
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"}
|
package/esm/decode.js
CHANGED
|
@@ -9,29 +9,26 @@ 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
|
|
13
|
-
constructor(base58) {
|
|
14
|
-
super(`Invalid Base58 string: ${base58}`);
|
|
15
|
-
}
|
|
12
|
+
export class Uuid58DecodeError extends Error {
|
|
16
13
|
}
|
|
17
|
-
_a =
|
|
14
|
+
_a = Uuid58DecodeError;
|
|
18
15
|
(() => {
|
|
19
|
-
_a.prototype.name = "
|
|
16
|
+
_a.prototype.name = "Uuid58DecodeError";
|
|
20
17
|
})();
|
|
21
18
|
/**
|
|
22
19
|
* Converts a Base58-encoded string back to a standard UUID format, but instead of
|
|
23
|
-
* throwing an error for invalid input, it returns an `
|
|
20
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
24
21
|
* instance.
|
|
25
22
|
*
|
|
26
23
|
* @param base58 - The Base58-encoded string to decode
|
|
27
24
|
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
28
|
-
* `
|
|
25
|
+
* `Uuid58DecodeError` if the input contains invalid Base58 characters
|
|
29
26
|
* @note This function does not throw; it returns the error object instead.
|
|
30
27
|
*
|
|
31
28
|
* @example
|
|
32
29
|
* ```typescript
|
|
33
30
|
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
34
|
-
* const error = uuid58DecodeSafe("invalid"); // returns
|
|
31
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
35
32
|
* ```
|
|
36
33
|
*/
|
|
37
34
|
export function uuid58DecodeSafe(base58) {
|
|
@@ -39,13 +36,13 @@ export function uuid58DecodeSafe(base58) {
|
|
|
39
36
|
for (const char of base58) {
|
|
40
37
|
const index = BASE58_MAP[char];
|
|
41
38
|
if (index === undefined) {
|
|
42
|
-
return new
|
|
39
|
+
return new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
|
|
43
40
|
}
|
|
44
41
|
num = num * ALPHABET_LENGTH + index;
|
|
45
42
|
}
|
|
46
43
|
const hex = num.toString(16).padStart(32, "0");
|
|
47
44
|
if (hex.length !== 32) {
|
|
48
|
-
return new
|
|
45
|
+
return new Uuid58DecodeError(`length not 32: ${base58}`);
|
|
49
46
|
}
|
|
50
47
|
return (hex.slice(0, 8) +
|
|
51
48
|
"-" +
|
|
@@ -62,7 +59,7 @@ export function uuid58DecodeSafe(base58) {
|
|
|
62
59
|
*
|
|
63
60
|
* @param base58 - The Base58-encoded string to decode
|
|
64
61
|
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
65
|
-
* @throws {
|
|
62
|
+
* @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
|
|
66
63
|
*
|
|
67
64
|
* @example
|
|
68
65
|
* ```typescript
|
|
@@ -74,13 +71,13 @@ export function uuid58Decode(base58) {
|
|
|
74
71
|
for (const char of base58) {
|
|
75
72
|
const index = BASE58_MAP[char];
|
|
76
73
|
if (index === undefined) {
|
|
77
|
-
throw new
|
|
74
|
+
throw new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
|
|
78
75
|
}
|
|
79
76
|
num = num * ALPHABET_LENGTH + index;
|
|
80
77
|
}
|
|
81
78
|
const hex = num.toString(16).padStart(32, "0");
|
|
82
79
|
if (hex.length !== 32) {
|
|
83
|
-
throw new
|
|
80
|
+
throw new Uuid58DecodeError(`length not 32: ${base58}`);
|
|
84
81
|
}
|
|
85
82
|
return (hex.slice(0, 8) +
|
|
86
83
|
"-" +
|
package/esm/encode.d.ts
CHANGED
|
@@ -2,15 +2,14 @@
|
|
|
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
|
|
6
|
-
constructor(uuid: string);
|
|
5
|
+
export declare class Uuid58EncodeError extends Error {
|
|
7
6
|
}
|
|
8
7
|
/**
|
|
9
8
|
* Converts a standard UUID string to a Base58-encoded format, but instead of
|
|
10
|
-
* throwing an error for invalid input, it returns an `
|
|
9
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
11
10
|
*
|
|
12
11
|
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
13
|
-
* @returns A Base58-encoded string, or an `
|
|
12
|
+
* @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
14
13
|
* not a valid UUID
|
|
15
14
|
* @note This function does not throw; it returns the error object instead.
|
|
16
15
|
*
|
|
@@ -23,10 +22,10 @@ export declare class InvalidUuidError extends Error {
|
|
|
23
22
|
* // Uppercase UUID is also accepted
|
|
24
23
|
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
25
24
|
* // Invalid UUID format
|
|
26
|
-
* const error = uuid58EncodeSafe("invalid"); // returns
|
|
25
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
27
26
|
* ```
|
|
28
27
|
*/
|
|
29
|
-
export declare function uuid58EncodeSafe(uuid: string): string |
|
|
28
|
+
export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
|
|
30
29
|
/**
|
|
31
30
|
* Converts a standard UUID string to a shortened Base58-encoded format.
|
|
32
31
|
*
|
|
@@ -34,7 +33,7 @@ export declare function uuid58EncodeSafe(uuid: string): string | InvalidUuidErro
|
|
|
34
33
|
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
35
34
|
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
36
35
|
* @returns A Base58-encoded string representation of the UUID
|
|
37
|
-
* @throws {
|
|
36
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
38
37
|
*
|
|
39
38
|
* @example
|
|
40
39
|
* ```typescript
|
package/esm/encode.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,
|
|
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"}
|
package/esm/encode.js
CHANGED
|
@@ -4,21 +4,18 @@ 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
|
|
8
|
-
constructor(uuid) {
|
|
9
|
-
super(`Invalid UUID format: ${uuid}`);
|
|
10
|
-
}
|
|
7
|
+
export class Uuid58EncodeError extends Error {
|
|
11
8
|
}
|
|
12
|
-
_a =
|
|
9
|
+
_a = Uuid58EncodeError;
|
|
13
10
|
(() => {
|
|
14
|
-
_a.prototype.name = "
|
|
11
|
+
_a.prototype.name = "Uuid58EncodeError";
|
|
15
12
|
})();
|
|
16
13
|
/**
|
|
17
14
|
* Converts a standard UUID string to a Base58-encoded format, but instead of
|
|
18
|
-
* throwing an error for invalid input, it returns an `
|
|
15
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
19
16
|
*
|
|
20
17
|
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
21
|
-
* @returns A Base58-encoded string, or an `
|
|
18
|
+
* @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
22
19
|
* not a valid UUID
|
|
23
20
|
* @note This function does not throw; it returns the error object instead.
|
|
24
21
|
*
|
|
@@ -31,20 +28,20 @@ _a = InvalidUuidError;
|
|
|
31
28
|
* // Uppercase UUID is also accepted
|
|
32
29
|
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
33
30
|
* // Invalid UUID format
|
|
34
|
-
* const error = uuid58EncodeSafe("invalid"); // returns
|
|
31
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
35
32
|
* ```
|
|
36
33
|
*/
|
|
37
34
|
export function uuid58EncodeSafe(uuid) {
|
|
38
|
-
const hex = uuid.
|
|
35
|
+
const hex = uuid.replaceAll("-", "");
|
|
39
36
|
if (hex.length !== 32) {
|
|
40
|
-
return new
|
|
37
|
+
return new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
|
|
41
38
|
}
|
|
42
39
|
let num;
|
|
43
40
|
try {
|
|
44
41
|
num = BigInt("0x" + hex);
|
|
45
42
|
}
|
|
46
43
|
catch {
|
|
47
|
-
return new
|
|
44
|
+
return new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
|
|
48
45
|
}
|
|
49
46
|
let encoded = "";
|
|
50
47
|
do {
|
|
@@ -60,7 +57,7 @@ export function uuid58EncodeSafe(uuid) {
|
|
|
60
57
|
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
61
58
|
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
62
59
|
* @returns A Base58-encoded string representation of the UUID
|
|
63
|
-
* @throws {
|
|
60
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
64
61
|
*
|
|
65
62
|
* @example
|
|
66
63
|
* ```typescript
|
|
@@ -73,16 +70,16 @@ export function uuid58EncodeSafe(uuid) {
|
|
|
73
70
|
* ```
|
|
74
71
|
*/
|
|
75
72
|
export function uuid58Encode(uuid) {
|
|
76
|
-
const hex = uuid.
|
|
73
|
+
const hex = uuid.replaceAll("-", "");
|
|
77
74
|
if (hex.length !== 32) {
|
|
78
|
-
throw new
|
|
75
|
+
throw new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
|
|
79
76
|
}
|
|
80
77
|
let num;
|
|
81
78
|
try {
|
|
82
79
|
num = BigInt("0x" + hex);
|
|
83
80
|
}
|
|
84
81
|
catch {
|
|
85
|
-
throw new
|
|
82
|
+
throw new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
|
|
86
83
|
}
|
|
87
84
|
let encoded = "";
|
|
88
85
|
do {
|
package/esm/uuid58.js
CHANGED
|
@@ -13,7 +13,7 @@ import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
|
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
15
|
export function uuid58() {
|
|
16
|
-
let num = BigInt("0x" + crypto.randomUUID().
|
|
16
|
+
let num = BigInt("0x" + crypto.randomUUID().replaceAll("-", ""));
|
|
17
17
|
let encoded = "";
|
|
18
18
|
do {
|
|
19
19
|
encoded = BASE58_ALPHABET[Number(num % ALPHABET_LENGTH)] + encoded;
|
package/package.json
CHANGED
package/script/decode.d.ts
CHANGED
|
@@ -2,32 +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
|
|
6
|
-
constructor(base58: string);
|
|
5
|
+
export declare class Uuid58DecodeError extends Error {
|
|
7
6
|
}
|
|
8
7
|
/**
|
|
9
8
|
* Converts a Base58-encoded string back to a standard UUID format, but instead of
|
|
10
|
-
* throwing an error for invalid input, it returns an `
|
|
9
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
11
10
|
* instance.
|
|
12
11
|
*
|
|
13
12
|
* @param base58 - The Base58-encoded string to decode
|
|
14
13
|
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
15
|
-
* `
|
|
14
|
+
* `Uuid58DecodeError` if the input contains invalid Base58 characters
|
|
16
15
|
* @note This function does not throw; it returns the error object instead.
|
|
17
16
|
*
|
|
18
17
|
* @example
|
|
19
18
|
* ```typescript
|
|
20
19
|
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
21
|
-
* const error = uuid58DecodeSafe("invalid"); // returns
|
|
20
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
22
21
|
* ```
|
|
23
22
|
*/
|
|
24
|
-
export declare function uuid58DecodeSafe(base58: string): string |
|
|
23
|
+
export declare function uuid58DecodeSafe(base58: string): string | Uuid58DecodeError;
|
|
25
24
|
/**
|
|
26
25
|
* Converts a Base58-encoded string back to a standard UUID string format.
|
|
27
26
|
*
|
|
28
27
|
* @param base58 - The Base58-encoded string to decode
|
|
29
28
|
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
30
|
-
* @throws {
|
|
29
|
+
* @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
|
|
31
30
|
*
|
|
32
31
|
* @example
|
|
33
32
|
* ```typescript
|
package/script/decode.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,
|
|
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"}
|
package/script/decode.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
4
|
+
exports.Uuid58DecodeError = void 0;
|
|
5
5
|
exports.uuid58DecodeSafe = uuid58DecodeSafe;
|
|
6
6
|
exports.uuid58Decode = uuid58Decode;
|
|
7
7
|
const alphabet_js_1 = require("./alphabet.js");
|
|
@@ -14,30 +14,27 @@ for (let i = 0; i < alphabet_js_1.BASE58_ALPHABET.length; i++) {
|
|
|
14
14
|
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
15
15
|
* This includes strings containing characters not in the Base58 alphabet.
|
|
16
16
|
*/
|
|
17
|
-
class
|
|
18
|
-
constructor(base58) {
|
|
19
|
-
super(`Invalid Base58 string: ${base58}`);
|
|
20
|
-
}
|
|
17
|
+
class Uuid58DecodeError extends Error {
|
|
21
18
|
}
|
|
22
|
-
exports.
|
|
23
|
-
_a =
|
|
19
|
+
exports.Uuid58DecodeError = Uuid58DecodeError;
|
|
20
|
+
_a = Uuid58DecodeError;
|
|
24
21
|
(() => {
|
|
25
|
-
_a.prototype.name = "
|
|
22
|
+
_a.prototype.name = "Uuid58DecodeError";
|
|
26
23
|
})();
|
|
27
24
|
/**
|
|
28
25
|
* Converts a Base58-encoded string back to a standard UUID format, but instead of
|
|
29
|
-
* throwing an error for invalid input, it returns an `
|
|
26
|
+
* throwing an error for invalid input, it returns an `Uuid58DecodeError`
|
|
30
27
|
* instance.
|
|
31
28
|
*
|
|
32
29
|
* @param base58 - The Base58-encoded string to decode
|
|
33
30
|
* @returns A standard UUID string (lowercase, with hyphens), or an
|
|
34
|
-
* `
|
|
31
|
+
* `Uuid58DecodeError` if the input contains invalid Base58 characters
|
|
35
32
|
* @note This function does not throw; it returns the error object instead.
|
|
36
33
|
*
|
|
37
34
|
* @example
|
|
38
35
|
* ```typescript
|
|
39
36
|
* const uuid = uuid58DecodeSafe("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
40
|
-
* const error = uuid58DecodeSafe("invalid"); // returns
|
|
37
|
+
* const error = uuid58DecodeSafe("invalid"); // returns Uuid58DecodeError
|
|
41
38
|
* ```
|
|
42
39
|
*/
|
|
43
40
|
function uuid58DecodeSafe(base58) {
|
|
@@ -45,13 +42,13 @@ function uuid58DecodeSafe(base58) {
|
|
|
45
42
|
for (const char of base58) {
|
|
46
43
|
const index = BASE58_MAP[char];
|
|
47
44
|
if (index === undefined) {
|
|
48
|
-
return new
|
|
45
|
+
return new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
|
|
49
46
|
}
|
|
50
47
|
num = num * alphabet_js_1.ALPHABET_LENGTH + index;
|
|
51
48
|
}
|
|
52
49
|
const hex = num.toString(16).padStart(32, "0");
|
|
53
50
|
if (hex.length !== 32) {
|
|
54
|
-
return new
|
|
51
|
+
return new Uuid58DecodeError(`length not 32: ${base58}`);
|
|
55
52
|
}
|
|
56
53
|
return (hex.slice(0, 8) +
|
|
57
54
|
"-" +
|
|
@@ -68,7 +65,7 @@ function uuid58DecodeSafe(base58) {
|
|
|
68
65
|
*
|
|
69
66
|
* @param base58 - The Base58-encoded string to decode
|
|
70
67
|
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
71
|
-
* @throws {
|
|
68
|
+
* @throws {Uuid58DecodeError} If the input string contains characters not in the Base58 alphabet
|
|
72
69
|
*
|
|
73
70
|
* @example
|
|
74
71
|
* ```typescript
|
|
@@ -80,13 +77,13 @@ function uuid58Decode(base58) {
|
|
|
80
77
|
for (const char of base58) {
|
|
81
78
|
const index = BASE58_MAP[char];
|
|
82
79
|
if (index === undefined) {
|
|
83
|
-
throw new
|
|
80
|
+
throw new Uuid58DecodeError(`Character '${char}' is not a valid Base58 character in string: ${base58}`);
|
|
84
81
|
}
|
|
85
82
|
num = num * alphabet_js_1.ALPHABET_LENGTH + index;
|
|
86
83
|
}
|
|
87
84
|
const hex = num.toString(16).padStart(32, "0");
|
|
88
85
|
if (hex.length !== 32) {
|
|
89
|
-
throw new
|
|
86
|
+
throw new Uuid58DecodeError(`length not 32: ${base58}`);
|
|
90
87
|
}
|
|
91
88
|
return (hex.slice(0, 8) +
|
|
92
89
|
"-" +
|
package/script/encode.d.ts
CHANGED
|
@@ -2,15 +2,14 @@
|
|
|
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
|
|
6
|
-
constructor(uuid: string);
|
|
5
|
+
export declare class Uuid58EncodeError extends Error {
|
|
7
6
|
}
|
|
8
7
|
/**
|
|
9
8
|
* Converts a standard UUID string to a Base58-encoded format, but instead of
|
|
10
|
-
* throwing an error for invalid input, it returns an `
|
|
9
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
11
10
|
*
|
|
12
11
|
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
13
|
-
* @returns A Base58-encoded string, or an `
|
|
12
|
+
* @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
14
13
|
* not a valid UUID
|
|
15
14
|
* @note This function does not throw; it returns the error object instead.
|
|
16
15
|
*
|
|
@@ -23,10 +22,10 @@ export declare class InvalidUuidError extends Error {
|
|
|
23
22
|
* // Uppercase UUID is also accepted
|
|
24
23
|
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
25
24
|
* // Invalid UUID format
|
|
26
|
-
* const error = uuid58EncodeSafe("invalid"); // returns
|
|
25
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
27
26
|
* ```
|
|
28
27
|
*/
|
|
29
|
-
export declare function uuid58EncodeSafe(uuid: string): string |
|
|
28
|
+
export declare function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
|
|
30
29
|
/**
|
|
31
30
|
* Converts a standard UUID string to a shortened Base58-encoded format.
|
|
32
31
|
*
|
|
@@ -34,7 +33,7 @@ export declare function uuid58EncodeSafe(uuid: string): string | InvalidUuidErro
|
|
|
34
33
|
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
35
34
|
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
36
35
|
* @returns A Base58-encoded string representation of the UUID
|
|
37
|
-
* @throws {
|
|
36
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
38
37
|
*
|
|
39
38
|
* @example
|
|
40
39
|
* ```typescript
|
package/script/encode.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,
|
|
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"}
|
package/script/encode.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
4
|
+
exports.Uuid58EncodeError = void 0;
|
|
5
5
|
exports.uuid58EncodeSafe = uuid58EncodeSafe;
|
|
6
6
|
exports.uuid58Encode = uuid58Encode;
|
|
7
7
|
const alphabet_js_1 = require("./alphabet.js");
|
|
@@ -9,22 +9,19 @@ const alphabet_js_1 = require("./alphabet.js");
|
|
|
9
9
|
* Error thrown when an invalid UUID string is provided for encoding.
|
|
10
10
|
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
11
11
|
*/
|
|
12
|
-
class
|
|
13
|
-
constructor(uuid) {
|
|
14
|
-
super(`Invalid UUID format: ${uuid}`);
|
|
15
|
-
}
|
|
12
|
+
class Uuid58EncodeError extends Error {
|
|
16
13
|
}
|
|
17
|
-
exports.
|
|
18
|
-
_a =
|
|
14
|
+
exports.Uuid58EncodeError = Uuid58EncodeError;
|
|
15
|
+
_a = Uuid58EncodeError;
|
|
19
16
|
(() => {
|
|
20
|
-
_a.prototype.name = "
|
|
17
|
+
_a.prototype.name = "Uuid58EncodeError";
|
|
21
18
|
})();
|
|
22
19
|
/**
|
|
23
20
|
* Converts a standard UUID string to a Base58-encoded format, but instead of
|
|
24
|
-
* throwing an error for invalid input, it returns an `
|
|
21
|
+
* throwing an error for invalid input, it returns an `Uuid58EncodeError` instance.
|
|
25
22
|
*
|
|
26
23
|
* @param uuid - The UUID string to encode (with or without hyphens)
|
|
27
|
-
* @returns A Base58-encoded string, or an `
|
|
24
|
+
* @returns A Base58-encoded string, or an `Uuid58EncodeError` if the input is
|
|
28
25
|
* not a valid UUID
|
|
29
26
|
* @note This function does not throw; it returns the error object instead.
|
|
30
27
|
*
|
|
@@ -37,20 +34,20 @@ _a = InvalidUuidError;
|
|
|
37
34
|
* // Uppercase UUID is also accepted
|
|
38
35
|
* const base58 = uuid58EncodeSafe("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
39
36
|
* // Invalid UUID format
|
|
40
|
-
* const error = uuid58EncodeSafe("invalid"); // returns
|
|
37
|
+
* const error = uuid58EncodeSafe("invalid"); // returns Uuid58EncodeError
|
|
41
38
|
* ```
|
|
42
39
|
*/
|
|
43
40
|
function uuid58EncodeSafe(uuid) {
|
|
44
|
-
const hex = uuid.
|
|
41
|
+
const hex = uuid.replaceAll("-", "");
|
|
45
42
|
if (hex.length !== 32) {
|
|
46
|
-
return new
|
|
43
|
+
return new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
|
|
47
44
|
}
|
|
48
45
|
let num;
|
|
49
46
|
try {
|
|
50
47
|
num = BigInt("0x" + hex);
|
|
51
48
|
}
|
|
52
49
|
catch {
|
|
53
|
-
return new
|
|
50
|
+
return new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
|
|
54
51
|
}
|
|
55
52
|
let encoded = "";
|
|
56
53
|
do {
|
|
@@ -66,7 +63,7 @@ function uuid58EncodeSafe(uuid) {
|
|
|
66
63
|
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
67
64
|
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
68
65
|
* @returns A Base58-encoded string representation of the UUID
|
|
69
|
-
* @throws {
|
|
66
|
+
* @throws {Uuid58EncodeError} If the input string is not a valid UUID format
|
|
70
67
|
*
|
|
71
68
|
* @example
|
|
72
69
|
* ```typescript
|
|
@@ -79,16 +76,16 @@ function uuid58EncodeSafe(uuid) {
|
|
|
79
76
|
* ```
|
|
80
77
|
*/
|
|
81
78
|
function uuid58Encode(uuid) {
|
|
82
|
-
const hex = uuid.
|
|
79
|
+
const hex = uuid.replaceAll("-", "");
|
|
83
80
|
if (hex.length !== 32) {
|
|
84
|
-
throw new
|
|
81
|
+
throw new Uuid58EncodeError(`Invalid UUID length: expected 32 characters (excluding hyphens), got ${hex.length} characters in "${uuid}"`);
|
|
85
82
|
}
|
|
86
83
|
let num;
|
|
87
84
|
try {
|
|
88
85
|
num = BigInt("0x" + hex);
|
|
89
86
|
}
|
|
90
87
|
catch {
|
|
91
|
-
throw new
|
|
88
|
+
throw new Uuid58EncodeError(`Invalid UUID format: "${uuid}" contains non-hexadecimal characters`);
|
|
92
89
|
}
|
|
93
90
|
let encoded = "";
|
|
94
91
|
do {
|
package/script/uuid58.js
CHANGED
|
@@ -16,7 +16,7 @@ const alphabet_js_1 = require("./alphabet.js");
|
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
18
|
function uuid58() {
|
|
19
|
-
let num = BigInt("0x" + crypto.randomUUID().
|
|
19
|
+
let num = BigInt("0x" + crypto.randomUUID().replaceAll("-", ""));
|
|
20
20
|
let encoded = "";
|
|
21
21
|
do {
|
|
22
22
|
encoded = alphabet_js_1.BASE58_ALPHABET[Number(num % alphabet_js_1.ALPHABET_LENGTH)] + encoded;
|