@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 +31 -28
- package/esm/decode.d.ts +8 -8
- package/esm/decode.d.ts.map +1 -1
- package/esm/decode.js +18 -31
- package/esm/encode.d.ts +4 -4
- package/esm/encode.d.ts.map +1 -1
- package/esm/encode.js +9 -21
- package/esm/uuid58.d.ts +3 -3
- package/esm/uuid58.js +4 -4
- package/package.json +1 -1
- package/script/decode.d.ts +8 -8
- package/script/decode.d.ts.map +1 -1
- package/script/decode.js +18 -31
- package/script/encode.d.ts +4 -4
- package/script/encode.d.ts.map +1 -1
- package/script/encode.js +9 -21
- package/script/uuid58.d.ts +3 -3
- package/script/uuid58.js +4 -4
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 (
|
|
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
|
|
119
|
-
throwing an error for invalid input, it returns an
|
|
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`
|
|
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(
|
|
138
|
+
function uuid58Decode(uuid58: string): string;
|
|
137
139
|
```
|
|
138
140
|
|
|
139
141
|
- **Parameters:**
|
|
140
|
-
- `
|
|
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
|
|
143
|
-
|
|
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,
|
|
148
|
-
throwing an error for invalid input, it returns an
|
|
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(
|
|
154
|
+
function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
|
|
152
155
|
```
|
|
153
156
|
|
|
154
157
|
- **Parameters:**
|
|
155
|
-
- `
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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(
|
|
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
|
|
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
|
|
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(
|
|
36
|
+
export declare function uuid58Decode(uuid58: string): string;
|
|
37
37
|
//# sourceMappingURL=decode.d.ts.map
|
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,iBAAkB,SAAQ,KAAK;CAI3C;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,
|
|
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
|
|
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
|
|
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(
|
|
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
|
|
39
|
+
for (const char of uuid58) {
|
|
37
40
|
const index = BASE58_MAP[char];
|
|
38
41
|
if (index === undefined) {
|
|
39
|
-
return new Uuid58DecodeError(`
|
|
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(`
|
|
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
|
|
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
|
|
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(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
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
|
|
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
|
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,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,
|
|
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
|
|
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
|
|
74
|
-
if (
|
|
75
|
-
throw
|
|
73
|
+
const result = uuid58EncodeSafe(uuid);
|
|
74
|
+
if (result instanceof Uuid58EncodeError) {
|
|
75
|
+
throw result;
|
|
76
76
|
}
|
|
77
|
-
|
|
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
|
|
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
|
|
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
package/script/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
|
|
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
|
|
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(
|
|
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
|
|
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
|
|
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(
|
|
36
|
+
export declare function uuid58Decode(uuid58: string): string;
|
|
37
37
|
//# sourceMappingURL=decode.d.ts.map
|
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,iBAAkB,SAAQ,KAAK;CAI3C;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,
|
|
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
|
|
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
|
|
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(
|
|
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
|
|
45
|
+
for (const char of uuid58) {
|
|
43
46
|
const index = BASE58_MAP[char];
|
|
44
47
|
if (index === undefined) {
|
|
45
|
-
return new Uuid58DecodeError(`
|
|
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(`
|
|
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
|
|
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
|
|
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(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
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
|
}
|
package/script/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
|
|
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
|
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,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,
|
|
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
|
|
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
|
|
80
|
-
if (
|
|
81
|
-
throw
|
|
79
|
+
const result = uuid58EncodeSafe(uuid);
|
|
80
|
+
if (result instanceof Uuid58EncodeError) {
|
|
81
|
+
throw result;
|
|
82
82
|
}
|
|
83
|
-
|
|
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
|
}
|
package/script/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
|
|
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
|
|
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
|
}
|