@nakanoaas/uuid58 0.0.1
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/LICENSE +21 -0
- package/README.md +114 -0
- package/esm/alphabet.d.ts +3 -0
- package/esm/alphabet.d.ts.map +1 -0
- package/esm/alphabet.js +3 -0
- package/esm/decode.d.ts +21 -0
- package/esm/decode.d.ts.map +1 -0
- package/esm/decode.js +51 -0
- package/esm/encode.d.ts +28 -0
- package/esm/encode.d.ts.map +1 -0
- package/esm/encode.js +48 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +3 -0
- package/esm/package.json +3 -0
- package/esm/uuid58.d.ts +15 -0
- package/esm/uuid58.d.ts.map +1 -0
- package/esm/uuid58.js +17 -0
- package/package.json +25 -0
- package/script/alphabet.d.ts +3 -0
- package/script/alphabet.d.ts.map +1 -0
- package/script/alphabet.js +6 -0
- package/script/decode.d.ts +21 -0
- package/script/decode.d.ts.map +1 -0
- package/script/decode.js +56 -0
- package/script/encode.d.ts +28 -0
- package/script/encode.d.ts.map +1 -0
- package/script/encode.js +53 -0
- package/script/index.d.ts +4 -0
- package/script/index.d.ts.map +1 -0
- package/script/index.js +9 -0
- package/script/package.json +3 -0
- package/script/uuid58.d.ts +15 -0
- package/script/uuid58.d.ts.map +1 -0
- package/script/uuid58.js +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Nakano as a Service
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# uuid58
|
|
2
|
+
|
|
3
|
+
[](https://jsr.io/@nakanoaas/uuid58)
|
|
4
|
+
[](https://badge.fury.io/js/@nakanoaas%2Fuuid58)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A tiny, zero-dependency library for generating and converting UUIDs to
|
|
8
|
+
Base58-encoded strings (using Bitcoin-style alphabet). Perfect for creating
|
|
9
|
+
shorter, URL-safe identifiers while maintaining the uniqueness of UUIDs.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- ð Generate short, URL-safe identifiers (~22 characters)
|
|
14
|
+
- ð Bidirectional conversion between UUID and Base58
|
|
15
|
+
- ðŠķ Zero dependencies
|
|
16
|
+
- ðŠ Type-safe with TypeScript
|
|
17
|
+
- ð Uses native `crypto.randomUUID()` for secure UUID generation
|
|
18
|
+
- ðĶ Supports both ESM and CommonJS
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
### Deno
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
deno add @nakanoaas/uuid58
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Node.js
|
|
29
|
+
|
|
30
|
+
Using npm:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install @nakanoaas/uuid58
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Using yarn:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
yarn add @nakanoaas/uuid58
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Using pnpm:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pnpm add @nakanoaas/uuid58
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { decodeUuid58, encodeUuid58, uuid58 } from "@nakanoaas/uuid58";
|
|
52
|
+
|
|
53
|
+
// Generate a new Base58-encoded UUID
|
|
54
|
+
const id = uuid58();
|
|
55
|
+
// => "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
56
|
+
|
|
57
|
+
// Convert existing UUID to Base58
|
|
58
|
+
const encoded = encodeUuid58("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf");
|
|
59
|
+
// => "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
60
|
+
|
|
61
|
+
// Convert Base58 back to UUID
|
|
62
|
+
const decoded = decodeUuid58("XDY9dmBbcMBXqcRvYw8xJ2");
|
|
63
|
+
// => "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## API Reference
|
|
67
|
+
|
|
68
|
+
### `uuid58()`
|
|
69
|
+
|
|
70
|
+
Generates a new Base58-encoded UUID.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
function uuid58(): string;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `encodeUuid58(uuid: string)`
|
|
77
|
+
|
|
78
|
+
Converts a standard UUID string to a Base58-encoded format.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
function encodeUuid58(uuid: string): string;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- **Parameters:**
|
|
85
|
+
- `uuid`: The UUID string to encode (with or without hyphens)
|
|
86
|
+
- **Returns:** A Base58-encoded string
|
|
87
|
+
- **Throws:** `InvalidUuidError` if the input is not a valid UUID
|
|
88
|
+
|
|
89
|
+
### `decodeUuid58(encoded: string)`
|
|
90
|
+
|
|
91
|
+
Converts a Base58-encoded string back to a standard UUID format.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
function decodeUuid58(encoded: string): string;
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- **Parameters:**
|
|
98
|
+
- `encoded`: The Base58-encoded string to decode
|
|
99
|
+
- **Returns:** A standard UUID string (lowercase, with hyphens)
|
|
100
|
+
- **Throws:** `InvalidBase58Error` if the input contains invalid Base58
|
|
101
|
+
characters
|
|
102
|
+
|
|
103
|
+
## Why uuid58?
|
|
104
|
+
|
|
105
|
+
Standard UUIDs are 36 characters long (including hyphens), which can be
|
|
106
|
+
cumbersome in URLs or when space is limited. Base58 encoding reduces this to
|
|
107
|
+
around 22 characters while maintaining uniqueness and using only URL-safe
|
|
108
|
+
characters. The Base58 alphabet used in this library is the same as Bitcoin's,
|
|
109
|
+
which excludes similar-looking characters (0, O, I, l) to prevent visual
|
|
110
|
+
ambiguity and confusion.
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT License - see the [LICENSE](LICENSE) file for details
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alphabet.d.ts","sourceRoot":"","sources":["../src/alphabet.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,eAAe,+DACkC,CAAC;AAC/D,eAAO,MAAM,eAAe,QAAiC,CAAC"}
|
package/esm/alphabet.js
ADDED
package/esm/decode.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
3
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
4
|
+
*/
|
|
5
|
+
export declare class InvalidBase58Error extends Error {
|
|
6
|
+
constructor(base58: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converts a Base58-encoded string back to a standard UUID string format.
|
|
10
|
+
*
|
|
11
|
+
* @param base58 - The Base58-encoded string to decode
|
|
12
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
13
|
+
* @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const uuid = decodeUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function decodeUuid58(base58: string): string;
|
|
21
|
+
//# sourceMappingURL=decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;gBAK/B,MAAM,EAAE,MAAM;CAG3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAuBnD"}
|
package/esm/decode.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
|
|
3
|
+
// Fast lookup map from alphabet characters to their indices
|
|
4
|
+
const BASE58_MAP = {};
|
|
5
|
+
for (let i = 0; i < BASE58_ALPHABET.length; i++) {
|
|
6
|
+
BASE58_MAP[BASE58_ALPHABET[i]] = BigInt(i);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
10
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
11
|
+
*/
|
|
12
|
+
export class InvalidBase58Error extends Error {
|
|
13
|
+
constructor(base58) {
|
|
14
|
+
super(`Invalid Base58 string: ${base58}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
_a = InvalidBase58Error;
|
|
18
|
+
(() => {
|
|
19
|
+
_a.prototype.name = "InvalidBase58Error";
|
|
20
|
+
})();
|
|
21
|
+
/**
|
|
22
|
+
* Converts a Base58-encoded string back to a standard UUID string format.
|
|
23
|
+
*
|
|
24
|
+
* @param base58 - The Base58-encoded string to decode
|
|
25
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
26
|
+
* @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const uuid = decodeUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function decodeUuid58(base58) {
|
|
34
|
+
if (!/^[1-9A-HJ-NP-Za-km-z]+$/.test(base58)) {
|
|
35
|
+
throw new InvalidBase58Error(base58);
|
|
36
|
+
}
|
|
37
|
+
let num = 0n;
|
|
38
|
+
for (const char of base58) {
|
|
39
|
+
num = num * ALPHABET_LENGTH + BASE58_MAP[char];
|
|
40
|
+
}
|
|
41
|
+
const hex = num.toString(16).padStart(32, "0");
|
|
42
|
+
return (hex.slice(0, 8) +
|
|
43
|
+
"-" +
|
|
44
|
+
hex.slice(8, 12) +
|
|
45
|
+
"-" +
|
|
46
|
+
hex.slice(12, 16) +
|
|
47
|
+
"-" +
|
|
48
|
+
hex.slice(16, 20) +
|
|
49
|
+
"-" +
|
|
50
|
+
hex.slice(20));
|
|
51
|
+
}
|
package/esm/encode.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
3
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
4
|
+
*/
|
|
5
|
+
export declare class InvalidUuidError extends Error {
|
|
6
|
+
constructor(uuid: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converts a standard UUID string to a shortened Base58-encoded format.
|
|
10
|
+
*
|
|
11
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
12
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
13
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
14
|
+
* @returns A Base58-encoded string representation of the UUID
|
|
15
|
+
* @throws {InvalidUuidError} If the input string is not a valid UUID format
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // With hyphens
|
|
20
|
+
* const base58 = encodeUuid58("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
21
|
+
* // Without hyphens
|
|
22
|
+
* const base58 = encodeUuid58("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
23
|
+
* // Uppercase UUID is also accepted
|
|
24
|
+
* const base58 = encodeUuid58("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function encodeUuid58(uuid: string): string;
|
|
28
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAK7B,IAAI,EAAE,MAAM;CAGzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAejD"}
|
package/esm/encode.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { ALPHABET_LENGTH, BASE58_ALPHABET } from "./alphabet.js";
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
5
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
6
|
+
*/
|
|
7
|
+
export class InvalidUuidError extends Error {
|
|
8
|
+
constructor(uuid) {
|
|
9
|
+
super(`Invalid UUID format: ${uuid}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
_a = InvalidUuidError;
|
|
13
|
+
(() => {
|
|
14
|
+
_a.prototype.name = "InvalidUuidError";
|
|
15
|
+
})();
|
|
16
|
+
/**
|
|
17
|
+
* Converts a standard UUID string to a shortened Base58-encoded format.
|
|
18
|
+
*
|
|
19
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
20
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
21
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
22
|
+
* @returns A Base58-encoded string representation of the UUID
|
|
23
|
+
* @throws {InvalidUuidError} If the input string is not a valid UUID format
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // With hyphens
|
|
28
|
+
* const base58 = encodeUuid58("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
29
|
+
* // Without hyphens
|
|
30
|
+
* const base58 = encodeUuid58("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
31
|
+
* // Uppercase UUID is also accepted
|
|
32
|
+
* const base58 = encodeUuid58("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function encodeUuid58(uuid) {
|
|
36
|
+
const hex = uuid.replace(/-/g, "").toLowerCase();
|
|
37
|
+
if (!/^[0-9a-f]{32}$/.test(hex)) {
|
|
38
|
+
throw new InvalidUuidError(uuid);
|
|
39
|
+
}
|
|
40
|
+
let num = BigInt("0x" + hex);
|
|
41
|
+
let encoded = "";
|
|
42
|
+
do {
|
|
43
|
+
const rem = Number(num % ALPHABET_LENGTH);
|
|
44
|
+
encoded = BASE58_ALPHABET[rem] + encoded;
|
|
45
|
+
num /= ALPHABET_LENGTH;
|
|
46
|
+
} while (num > 0n);
|
|
47
|
+
return encoded;
|
|
48
|
+
}
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
package/esm/index.js
ADDED
package/esm/package.json
ADDED
package/esm/uuid58.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a new Base58-encoded UUID.
|
|
3
|
+
*
|
|
4
|
+
* This function combines the standard UUID generation (using crypto.randomUUID())
|
|
5
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
6
|
+
*
|
|
7
|
+
* @returns A Base58-encoded string representing a newly generated UUID
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function uuid58(): string;
|
|
15
|
+
//# sourceMappingURL=uuid58.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAE/B"}
|
package/esm/uuid58.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { encodeUuid58 } from "./encode.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generates a new Base58-encoded UUID.
|
|
4
|
+
*
|
|
5
|
+
* This function combines the standard UUID generation (using crypto.randomUUID())
|
|
6
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
7
|
+
*
|
|
8
|
+
* @returns A Base58-encoded string representing a newly generated UUID
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function uuid58() {
|
|
16
|
+
return encodeUuid58(crypto.randomUUID());
|
|
17
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nakanoaas/uuid58",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A tiny, zero-dependency library for generating and converting UUIDs to Base58-encoded strings.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git://github.com/nakanoasaservice/uuid58.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/nakanoasaservice/uuid58/issues"
|
|
12
|
+
},
|
|
13
|
+
"main": "./script/index.js",
|
|
14
|
+
"module": "./esm/index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./esm/index.js",
|
|
18
|
+
"require": "./script/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^20.9.0"
|
|
23
|
+
},
|
|
24
|
+
"_generatedBy": "dnt@dev"
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alphabet.d.ts","sourceRoot":"","sources":["../src/alphabet.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,eAAe,+DACkC,CAAC;AAC/D,eAAO,MAAM,eAAe,QAAiC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ALPHABET_LENGTH = exports.BASE58_ALPHABET = void 0;
|
|
4
|
+
// Character set for Base58 (Bitcoin compatible)
|
|
5
|
+
exports.BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; // cspell:disable-line
|
|
6
|
+
exports.ALPHABET_LENGTH = BigInt(exports.BASE58_ALPHABET.length);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
3
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
4
|
+
*/
|
|
5
|
+
export declare class InvalidBase58Error extends Error {
|
|
6
|
+
constructor(base58: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converts a Base58-encoded string back to a standard UUID string format.
|
|
10
|
+
*
|
|
11
|
+
* @param base58 - The Base58-encoded string to decode
|
|
12
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
13
|
+
* @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const uuid = decodeUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function decodeUuid58(base58: string): string;
|
|
21
|
+
//# sourceMappingURL=decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;gBAK/B,MAAM,EAAE,MAAM;CAG3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAuBnD"}
|
package/script/decode.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.InvalidBase58Error = void 0;
|
|
5
|
+
exports.decodeUuid58 = decodeUuid58;
|
|
6
|
+
const alphabet_js_1 = require("./alphabet.js");
|
|
7
|
+
// Fast lookup map from alphabet characters to their indices
|
|
8
|
+
const BASE58_MAP = {};
|
|
9
|
+
for (let i = 0; i < alphabet_js_1.BASE58_ALPHABET.length; i++) {
|
|
10
|
+
BASE58_MAP[alphabet_js_1.BASE58_ALPHABET[i]] = BigInt(i);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Error thrown when an invalid Base58 string is provided for decoding.
|
|
14
|
+
* This includes strings containing characters not in the Base58 alphabet.
|
|
15
|
+
*/
|
|
16
|
+
class InvalidBase58Error extends Error {
|
|
17
|
+
constructor(base58) {
|
|
18
|
+
super(`Invalid Base58 string: ${base58}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.InvalidBase58Error = InvalidBase58Error;
|
|
22
|
+
_a = InvalidBase58Error;
|
|
23
|
+
(() => {
|
|
24
|
+
_a.prototype.name = "InvalidBase58Error";
|
|
25
|
+
})();
|
|
26
|
+
/**
|
|
27
|
+
* Converts a Base58-encoded string back to a standard UUID string format.
|
|
28
|
+
*
|
|
29
|
+
* @param base58 - The Base58-encoded string to decode
|
|
30
|
+
* @returns A standard UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (always in lowercase)
|
|
31
|
+
* @throws {InvalidBase58Error} If the input string contains characters not in the Base58 alphabet
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const uuid = decodeUuid58("XDY9dmBbcMBXqcRvYw8xJ2"); // returns "f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function decodeUuid58(base58) {
|
|
39
|
+
if (!/^[1-9A-HJ-NP-Za-km-z]+$/.test(base58)) {
|
|
40
|
+
throw new InvalidBase58Error(base58);
|
|
41
|
+
}
|
|
42
|
+
let num = 0n;
|
|
43
|
+
for (const char of base58) {
|
|
44
|
+
num = num * alphabet_js_1.ALPHABET_LENGTH + BASE58_MAP[char];
|
|
45
|
+
}
|
|
46
|
+
const hex = num.toString(16).padStart(32, "0");
|
|
47
|
+
return (hex.slice(0, 8) +
|
|
48
|
+
"-" +
|
|
49
|
+
hex.slice(8, 12) +
|
|
50
|
+
"-" +
|
|
51
|
+
hex.slice(12, 16) +
|
|
52
|
+
"-" +
|
|
53
|
+
hex.slice(16, 20) +
|
|
54
|
+
"-" +
|
|
55
|
+
hex.slice(20));
|
|
56
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
3
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
4
|
+
*/
|
|
5
|
+
export declare class InvalidUuidError extends Error {
|
|
6
|
+
constructor(uuid: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converts a standard UUID string to a shortened Base58-encoded format.
|
|
10
|
+
*
|
|
11
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
12
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
13
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
14
|
+
* @returns A Base58-encoded string representation of the UUID
|
|
15
|
+
* @throws {InvalidUuidError} If the input string is not a valid UUID format
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // With hyphens
|
|
20
|
+
* const base58 = encodeUuid58("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
21
|
+
* // Without hyphens
|
|
22
|
+
* const base58 = encodeUuid58("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
23
|
+
* // Uppercase UUID is also accepted
|
|
24
|
+
* const base58 = encodeUuid58("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function encodeUuid58(uuid: string): string;
|
|
28
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../src/encode.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAK7B,IAAI,EAAE,MAAM;CAGzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAejD"}
|
package/script/encode.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.InvalidUuidError = void 0;
|
|
5
|
+
exports.encodeUuid58 = encodeUuid58;
|
|
6
|
+
const alphabet_js_1 = require("./alphabet.js");
|
|
7
|
+
/**
|
|
8
|
+
* Error thrown when an invalid UUID string is provided for encoding.
|
|
9
|
+
* This includes strings that don't match the standard UUID format (32 hexadecimal characters with optional hyphens).
|
|
10
|
+
*/
|
|
11
|
+
class InvalidUuidError extends Error {
|
|
12
|
+
constructor(uuid) {
|
|
13
|
+
super(`Invalid UUID format: ${uuid}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.InvalidUuidError = InvalidUuidError;
|
|
17
|
+
_a = InvalidUuidError;
|
|
18
|
+
(() => {
|
|
19
|
+
_a.prototype.name = "InvalidUuidError";
|
|
20
|
+
})();
|
|
21
|
+
/**
|
|
22
|
+
* Converts a standard UUID string to a shortened Base58-encoded format.
|
|
23
|
+
*
|
|
24
|
+
* @param uuid - The UUID string to encode. Can be provided with or without hyphens
|
|
25
|
+
* (format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").
|
|
26
|
+
* The input is case-insensitive, so both uppercase and lowercase hexadecimal characters are accepted.
|
|
27
|
+
* @returns A Base58-encoded string representation of the UUID
|
|
28
|
+
* @throws {InvalidUuidError} If the input string is not a valid UUID format
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // With hyphens
|
|
33
|
+
* const base58 = encodeUuid58("f4b247fd-1f87-45d4-aa06-1c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
34
|
+
* // Without hyphens
|
|
35
|
+
* const base58 = encodeUuid58("f4b247fd1f8745d4aa061c6fc0a8dfaf"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
36
|
+
* // Uppercase UUID is also accepted
|
|
37
|
+
* const base58 = encodeUuid58("F4B247FD-1F87-45D4-AA06-1C6FC0A8DFAF"); // returns "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function encodeUuid58(uuid) {
|
|
41
|
+
const hex = uuid.replace(/-/g, "").toLowerCase();
|
|
42
|
+
if (!/^[0-9a-f]{32}$/.test(hex)) {
|
|
43
|
+
throw new InvalidUuidError(uuid);
|
|
44
|
+
}
|
|
45
|
+
let num = BigInt("0x" + hex);
|
|
46
|
+
let encoded = "";
|
|
47
|
+
do {
|
|
48
|
+
const rem = Number(num % alphabet_js_1.ALPHABET_LENGTH);
|
|
49
|
+
encoded = alphabet_js_1.BASE58_ALPHABET[rem] + encoded;
|
|
50
|
+
num /= alphabet_js_1.ALPHABET_LENGTH;
|
|
51
|
+
} while (num > 0n);
|
|
52
|
+
return encoded;
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
package/script/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uuid58 = exports.decodeUuid58 = exports.encodeUuid58 = void 0;
|
|
4
|
+
var encode_js_1 = require("./encode.js");
|
|
5
|
+
Object.defineProperty(exports, "encodeUuid58", { enumerable: true, get: function () { return encode_js_1.encodeUuid58; } });
|
|
6
|
+
var decode_js_1 = require("./decode.js");
|
|
7
|
+
Object.defineProperty(exports, "decodeUuid58", { enumerable: true, get: function () { return decode_js_1.decodeUuid58; } });
|
|
8
|
+
var uuid58_js_1 = require("./uuid58.js");
|
|
9
|
+
Object.defineProperty(exports, "uuid58", { enumerable: true, get: function () { return uuid58_js_1.uuid58; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a new Base58-encoded UUID.
|
|
3
|
+
*
|
|
4
|
+
* This function combines the standard UUID generation (using crypto.randomUUID())
|
|
5
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
6
|
+
*
|
|
7
|
+
* @returns A Base58-encoded string representing a newly generated UUID
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function uuid58(): string;
|
|
15
|
+
//# sourceMappingURL=uuid58.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid58.d.ts","sourceRoot":"","sources":["../src/uuid58.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAE/B"}
|
package/script/uuid58.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uuid58 = uuid58;
|
|
4
|
+
const encode_js_1 = require("./encode.js");
|
|
5
|
+
/**
|
|
6
|
+
* Generates a new Base58-encoded UUID.
|
|
7
|
+
*
|
|
8
|
+
* This function combines the standard UUID generation (using crypto.randomUUID())
|
|
9
|
+
* with Base58 encoding to create a shorter, URL-safe identifier.
|
|
10
|
+
*
|
|
11
|
+
* @returns A Base58-encoded string representing a newly generated UUID
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const id = uuid58(); // returns something like "XDY9dmBbcMBXqcRvYw8xJ2"
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function uuid58() {
|
|
19
|
+
return (0, encode_js_1.encodeUuid58)(crypto.randomUUID());
|
|
20
|
+
}
|