@nakanoaas/uuid58 0.0.8 â 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 +30 -28
- package/esm/decode.d.ts +8 -8
- package/esm/decode.d.ts.map +1 -1
- package/esm/decode.js +15 -12
- package/esm/encode.d.ts +4 -4
- package/esm/encode.js +5 -5
- 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 +15 -12
- package/script/encode.d.ts +4 -4
- package/script/encode.js +5 -5
- package/script/uuid58.d.ts +3 -3
- package/script/uuid58.js +4 -4
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ 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
|
|
@@ -51,13 +51,13 @@ pnpm add @nakanoaas/uuid58
|
|
|
51
51
|
```typescript
|
|
52
52
|
import { uuid58, uuid58Decode, uuid58Encode } from "@nakanoaas/uuid58";
|
|
53
53
|
|
|
54
|
-
// Generate a new Base58-encoded UUID
|
|
54
|
+
// Generate a new Base58-encoded UUID (always 22 characters)
|
|
55
55
|
const id = uuid58();
|
|
56
|
-
// => "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
56
|
+
// => "XDY9dmBbcMBXqcRvYw8xJ2" (22 characters)
|
|
57
57
|
|
|
58
|
-
// Convert existing UUID to Base58
|
|
58
|
+
// Convert existing UUID to Base58 (always 22 characters)
|
|
59
59
|
const encoded = uuid58Encode("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf");
|
|
60
|
-
// => "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
60
|
+
// => "XDY9dmBbcMBXqcRvYw8xJ2" (22 characters)
|
|
61
61
|
|
|
62
62
|
// Convert Base58 back to UUID
|
|
63
63
|
const decoded = uuid58Decode("XDY9dmBbcMBXqcRvYw8xJ2");
|
|
@@ -77,7 +77,7 @@ if (encodedSafe instanceof Uuid58EncodeError) {
|
|
|
77
77
|
console.error(encodedSafe.message);
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
|
-
// use encoded string
|
|
80
|
+
// use encoded string (22 characters)
|
|
81
81
|
console.log(encodedSafe);
|
|
82
82
|
|
|
83
83
|
// Safe decoding: returns error object instead of throwing
|
|
@@ -95,74 +95,76 @@ console.log(decodedSafe);
|
|
|
95
95
|
|
|
96
96
|
### `uuid58()`
|
|
97
97
|
|
|
98
|
-
Generates a new Base58-encoded UUID.
|
|
98
|
+
Generates a new Base58-encoded UUID (always 22 characters).
|
|
99
99
|
|
|
100
100
|
```typescript
|
|
101
|
-
function uuid58(): string;
|
|
101
|
+
function uuid58(): string; // always returns a 22-character string
|
|
102
102
|
```
|
|
103
103
|
|
|
104
104
|
### `uuid58Encode(uuid: string)`
|
|
105
105
|
|
|
106
|
-
Converts a standard UUID string to a Base58-encoded format.
|
|
106
|
+
Converts a standard UUID string to a 22-character Base58-encoded format.
|
|
107
107
|
|
|
108
108
|
```typescript
|
|
109
|
-
function uuid58Encode(uuid: string): string;
|
|
109
|
+
function uuid58Encode(uuid: string): string; // always returns a 22-character string
|
|
110
110
|
```
|
|
111
111
|
|
|
112
112
|
- **Parameters:**
|
|
113
113
|
- `uuid`: The UUID string to encode (with or without hyphens)
|
|
114
|
-
- **Returns:** A Base58-encoded string
|
|
114
|
+
- **Returns:** A 22-character Base58-encoded string
|
|
115
115
|
- **Throws:** `Uuid58EncodeError` if the input is not a valid UUID
|
|
116
116
|
|
|
117
117
|
### `uuid58EncodeSafe(uuid: string)`
|
|
118
118
|
|
|
119
|
-
Converts a standard UUID string to a Base58-encoded format, but
|
|
120
|
-
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.
|
|
121
122
|
|
|
122
123
|
```typescript
|
|
123
|
-
function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError;
|
|
124
|
+
function uuid58EncodeSafe(uuid: string): string | Uuid58EncodeError; // string is always 22 characters
|
|
124
125
|
```
|
|
125
126
|
|
|
126
127
|
- **Parameters:**
|
|
127
128
|
- `uuid`: The UUID string to encode (with or without hyphens)
|
|
128
|
-
- **Returns:** A Base58-encoded string, or an `Uuid58EncodeError`
|
|
129
|
-
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
|
|
130
131
|
- **Note:** This function does not throw; it returns the error object instead.
|
|
131
132
|
|
|
132
133
|
### `uuid58Decode(encoded: string)`
|
|
133
134
|
|
|
134
|
-
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.
|
|
135
136
|
|
|
136
137
|
```typescript
|
|
137
|
-
function uuid58Decode(
|
|
138
|
+
function uuid58Decode(uuid58: string): string;
|
|
138
139
|
```
|
|
139
140
|
|
|
140
141
|
- **Parameters:**
|
|
141
|
-
- `
|
|
142
|
+
- `uuid58`: The 22-character Base58-encoded string to decode
|
|
142
143
|
- **Returns:** A standard UUID string (lowercase, with hyphens)
|
|
143
|
-
- **Throws:** `Uuid58DecodeError` if the input
|
|
144
|
-
|
|
144
|
+
- **Throws:** `Uuid58DecodeError` if the input is not a valid 22-character
|
|
145
|
+
Base58 string
|
|
145
146
|
|
|
146
147
|
### `uuid58DecodeSafe(encoded: string)`
|
|
147
148
|
|
|
148
|
-
Converts a Base58-encoded string back to a standard UUID format,
|
|
149
|
-
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.
|
|
150
152
|
|
|
151
153
|
```typescript
|
|
152
|
-
function uuid58DecodeSafe(
|
|
154
|
+
function uuid58DecodeSafe(uuid58: string): string | Uuid58DecodeError;
|
|
153
155
|
```
|
|
154
156
|
|
|
155
157
|
- **Parameters:**
|
|
156
|
-
- `
|
|
158
|
+
- `uuid58`: The 22-character Base58-encoded string to decode
|
|
157
159
|
- **Returns:** A standard UUID string (lowercase, with hyphens), or an
|
|
158
|
-
`Uuid58DecodeError` if the input
|
|
160
|
+
`Uuid58DecodeError` if the input is not a valid 22-character Base58 string
|
|
159
161
|
- **Note:** This function does not throw; it returns the error object instead.
|
|
160
162
|
|
|
161
163
|
## Why uuid58?
|
|
162
164
|
|
|
163
165
|
Standard UUIDs are 36 characters long (including hyphens), which can be
|
|
164
|
-
cumbersome in URLs or when space is limited. Base58 encoding reduces this to
|
|
165
|
-
|
|
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
|
|
166
168
|
characters. The Base58 alphabet used in this library is the same as Bitcoin's,
|
|
167
169
|
which excludes similar-looking characters (0, O, I, l) to prevent visual
|
|
168
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,19 +58,19 @@ 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
|
-
const result = uuid58DecodeSafe(
|
|
72
|
+
export function uuid58Decode(uuid58) {
|
|
73
|
+
const result = uuid58DecodeSafe(uuid58);
|
|
71
74
|
if (result instanceof Uuid58DecodeError) {
|
|
72
75
|
throw result;
|
|
73
76
|
}
|
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.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
|
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,19 +64,19 @@ 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
|
-
const result = uuid58DecodeSafe(
|
|
78
|
+
function uuid58Decode(uuid58) {
|
|
79
|
+
const result = uuid58DecodeSafe(uuid58);
|
|
77
80
|
if (result instanceof Uuid58DecodeError) {
|
|
78
81
|
throw result;
|
|
79
82
|
}
|
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.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
|
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
|
}
|