@bboss/uuid32 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 bigboss.dev (https://github.com/bigbossdev/uuid32)
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,125 @@
1
+ # @bboss/uuid32
2
+
3
+ A lightweight TypeScript library for encoding and decoding UUIDs to/from Crockford Base32 strings, designed exclusively for Node.js environments.
4
+
5
+ ## Features
6
+
7
+ - ๐Ÿš€ **Fast & Lightweight**: Zero dependencies, optimized for performance
8
+ - ๐Ÿ”’ **Node.js Only**: Built specifically for server-side applications
9
+ - ๐Ÿ“ฆ **TypeScript Native**: Full TypeScript support with type definitions
10
+ - ๐Ÿงช **Well Tested**: Comprehensive test suite with high coverage
11
+ - ๐Ÿ”ง **Simple API**: Easy-to-use functions for encoding, decoding, and validation
12
+ - ๐Ÿ›ก๏ธ **User-Friendly**: Excludes confusing characters (I, L, O, U) to prevent input errors
13
+
14
+ ## Use Cases
15
+
16
+ - **Product Key Generation**: Software license keys, serial numbers
17
+ - **API Key Generation**: Service authentication tokens, access keys
18
+ - **Short IDs**: URL shortening, reference codes, tracking IDs
19
+ - **Manual Input**: Reduced errors when users need to type keys manually
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ # npm
25
+ npm install @bboss/uuid32
26
+
27
+ # yarn
28
+ yarn add @bboss/uuid32
29
+ ```
30
+
31
+ ## Requirements
32
+
33
+ - Node.js 16.0.0 or higher (requires `crypto.randomUUID()`)
34
+ - **Node.js only** - not compatible with browser environments
35
+
36
+ ## Usage
37
+
38
+ ```javascript
39
+ import uuid32 from '@bboss/uuid32';
40
+ // const uuid32 = require('@bboss/uuid32'); // legacy way
41
+
42
+ // Generate a new Base32 UUID
43
+ const shortId = uuid32.generateBase32();
44
+ console.log(shortId); // โ†’ "29STNWYQG28H4VWA59PD0XYJR8" (random)
45
+
46
+ // Encode existing UUID to Base32
47
+ const encoded = uuid32.encode('49ceabcf-5e02-4449-be28-a9b341df4b08');
48
+ console.log(encoded); // โ†’ "29STNWYQG28H4VWA59PD0XYJR8"
49
+
50
+ // Decode Base32 back to standard UUID
51
+ const decoded = uuid32.decode('29STNWYQG28H4VWA59PD0XYJR8');
52
+ console.log(decoded); // โ†’ "49ceabcf-5e02-4449-be28-a9b341df4b08"
53
+
54
+ // Validation
55
+ console.log(uuid32.isValidBase32('29STNWYQG28H4VWA59PD0XYJR8')); // โ†’ true
56
+ ```
57
+
58
+ ## API Reference
59
+
60
+ ### `generateBase32(): string`
61
+ Generates a new UUID v4 using `crypto.randomUUID()` and encodes it to Crockford Base32.
62
+
63
+ **Returns:** Base32 encoded UUID string
64
+
65
+ ### `encode(uuid: string): string`
66
+ Encodes a standard UUID to Crockford Base32 format.
67
+
68
+ **Parameters:**
69
+ - `uuid` - Standard UUID string (with or without hyphens)
70
+
71
+ **Returns:** Base32 encoded string
72
+
73
+ **Throws:** Error if UUID format is invalid
74
+
75
+ ### `decode(base32: string): string`
76
+ Decodes a Crockford Base32 string back to standard UUID format.
77
+
78
+ **Parameters:**
79
+ - `base32` - Base32 encoded string
80
+
81
+ **Returns:** Standard UUID string with hyphens
82
+
83
+ **Throws:** Error if Base32 format is invalid
84
+
85
+ ### `isValidBase32(str: string): boolean`
86
+ Validates if a string is a valid Crockford Base32 format.
87
+
88
+ **Parameters:**
89
+ - `str` - String to validate
90
+
91
+ **Returns:** `true` if valid Base32, `false` otherwise
92
+
93
+ ## Error Handling
94
+
95
+ The library throws descriptive errors for invalid inputs:
96
+
97
+ ```javascript
98
+ try {
99
+ uuid32.encode('invalid-uuid');
100
+ } catch (error) {
101
+ console.log(error.message); // โ†’ "Invalid UUID format"
102
+ }
103
+
104
+ try {
105
+ uuid32.decode('invalid@base32!');
106
+ } catch (error) {
107
+ console.log(error.message); // โ†’ "Invalid Base32 format"
108
+ }
109
+ ```
110
+
111
+ ## Crockford Base32 Character Set
112
+
113
+ Uses the following 32 characters: `0123456789ABCDEFGHJKMNPQRSTVWXYZ`
114
+
115
+ **Excluded characters for clarity:**
116
+ - `I` (can be confused with `1`)
117
+ - `L` (can be confused with `1`)
118
+ - `O` (can be confused with `0`)
119
+ - `U` (can be confused with `V`)
120
+
121
+ This makes the encoded strings more reliable for manual input and reduces transcription errors.
122
+
123
+ ## License
124
+
125
+ MIT License - see [LICENSE](LICENSE) file for details.
@@ -0,0 +1,22 @@
1
+ import { isValidBase32 } from './utils';
2
+ /**
3
+ * Encodes a UUID to Crockford Base32 string
4
+ */
5
+ export declare function encode(uuid: string): string;
6
+ /**
7
+ * Decodes a Crockford Base32 string to UUID
8
+ */
9
+ export declare function decode(base32: string): string;
10
+ /**
11
+ * Generates a new UUID v4 and encodes it to Crockford Base32
12
+ */
13
+ export declare function generateBase32(): string;
14
+ export { isValidBase32 };
15
+ declare const _default: {
16
+ generateBase32: typeof generateBase32;
17
+ encode: typeof encode;
18
+ decode: typeof decode;
19
+ isValidBase32: typeof isValidBase32;
20
+ };
21
+ export default _default;
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAGH,aAAa,EAIhB,MAAM,SAAS,CAAC;AAKjB;;GAEG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAmB3C;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAuB7C;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAGvC;AAGD,OAAO,EAAE,aAAa,EAAE,CAAC;;;;;;;AAGzB,wBAKE"}
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidBase32 = void 0;
4
+ exports.encode = encode;
5
+ exports.decode = decode;
6
+ exports.generateBase32 = generateBase32;
7
+ const crypto_1 = require("crypto");
8
+ const utils_1 = require("./utils");
9
+ Object.defineProperty(exports, "isValidBase32", { enumerable: true, get: function () { return utils_1.isValidBase32; } });
10
+ // Check environment on module load
11
+ (0, utils_1.checkNodeEnvironment)();
12
+ /**
13
+ * Encodes a UUID to Crockford Base32 string
14
+ */
15
+ function encode(uuid) {
16
+ if (!(0, utils_1.isValidUuid)(uuid)) {
17
+ throw new Error('Invalid UUID format');
18
+ }
19
+ const normalized = (0, utils_1.normalizeUuid)(uuid);
20
+ const hex = BigInt('0x' + normalized);
21
+ if (hex === 0n)
22
+ return '0'.padStart(26, '0');
23
+ let result = '';
24
+ let num = hex;
25
+ while (num > 0n) {
26
+ result = utils_1.BASE32_CHARS[Number(num % 32n)] + result;
27
+ num = num / 32n;
28
+ }
29
+ return result.padStart(26, '0');
30
+ }
31
+ /**
32
+ * Decodes a Crockford Base32 string to UUID
33
+ */
34
+ function decode(base32) {
35
+ if (!(0, utils_1.isValidBase32)(base32)) {
36
+ throw new Error('Invalid Base32 format');
37
+ }
38
+ let num = 0n;
39
+ for (let i = 0; i < base32.length; i++) {
40
+ const char = base32[i];
41
+ const index = utils_1.BASE32_CHARS.indexOf(char);
42
+ if (index === -1) {
43
+ throw new Error('Invalid Base32 character');
44
+ }
45
+ num = num * 32n + BigInt(index);
46
+ }
47
+ let hex = num.toString(16).padStart(32, '0');
48
+ if (hex.length > 32) {
49
+ throw new Error('Invalid Base32 value: too large for UUID');
50
+ }
51
+ return (0, utils_1.formatUuid)(hex);
52
+ }
53
+ /**
54
+ * Generates a new UUID v4 and encodes it to Crockford Base32
55
+ */
56
+ function generateBase32() {
57
+ const uuid = (0, crypto_1.randomUUID)();
58
+ return encode(uuid);
59
+ }
60
+ // Default export for CommonJS compatibility
61
+ exports.default = {
62
+ generateBase32,
63
+ encode,
64
+ decode,
65
+ isValidBase32: utils_1.isValidBase32
66
+ };
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAgBA,wBAmBC;AAKD,wBAuBC;AAKD,wCAGC;AAvED,mCAAoC;AACpC,mCAOiB;AAkER,8FAtEL,qBAAa,OAsEK;AAhEtB,mCAAmC;AACnC,IAAA,4BAAoB,GAAE,CAAC;AAEvB;;GAEG;AACH,SAAgB,MAAM,CAAC,IAAY;IAC/B,IAAI,CAAC,IAAA,mBAAW,EAAC,IAAI,CAAC,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,UAAU,GAAG,IAAA,qBAAa,EAAC,IAAI,CAAC,CAAC;IACvC,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,GAAG,UAAU,CAAC,CAAC;IAEtC,IAAI,GAAG,KAAK,EAAE;QAAE,OAAO,G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CAAC;IAED,OAAO,IAAA,kBAAU,EAAC,GAAG,CAAC,CAAC;AAC3B,CAAC;AAED;;GAEG;AACH,SAAgB,cAAc;IAC1B,MAAM,IAAI,GAAG,IAAA,mBAAU,GAAE,CAAC;IAC1B,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC;AAKD,4CAA4C;AAC5C,kBAAe;IACX,cAAc;IACd,MAAM;IACN,MAAM;IACN,aAAa,EAAb,qBAAa;CAChB,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Crockford Base32 character set (excludes I, L, O, U to avoid confusion)
3
+ */
4
+ export declare const BASE32_CHARS = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
5
+ /**
6
+ * Validates if a string is a valid UUID format
7
+ */
8
+ export declare function isValidUuid(str: string): boolean;
9
+ /**
10
+ * Validates if a string is a valid Crockford Base32 format
11
+ */
12
+ export declare function isValidBase32(str: string): boolean;
13
+ /**
14
+ * Normalizes UUID by removing hyphens
15
+ */
16
+ export declare function normalizeUuid(uuid: string): string;
17
+ /**
18
+ * Formats UUID by adding hyphens
19
+ */
20
+ export declare function formatUuid(uuid: string): string;
21
+ /**
22
+ * Checks if running in browser environment
23
+ */
24
+ export declare function checkNodeEnvironment(): void;
25
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,YAAY,qCAAqC,CAAC;AAY/D;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAGhD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAGlD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAI3C"}
package/dist/utils.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BASE32_CHARS = void 0;
4
+ exports.isValidUuid = isValidUuid;
5
+ exports.isValidBase32 = isValidBase32;
6
+ exports.normalizeUuid = normalizeUuid;
7
+ exports.formatUuid = formatUuid;
8
+ exports.checkNodeEnvironment = checkNodeEnvironment;
9
+ /**
10
+ * Crockford Base32 character set (excludes I, L, O, U to avoid confusion)
11
+ */
12
+ exports.BASE32_CHARS = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
13
+ /**
14
+ * UUID validation regex (with or without hyphens)
15
+ */
16
+ const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i;
17
+ /**
18
+ * Crockford Base32 validation regex
19
+ */
20
+ const BASE32_REGEX = /^[0-9ABCDEFGHJKMNPQRSTVWXYZ]+$/;
21
+ /**
22
+ * Validates if a string is a valid UUID format
23
+ */
24
+ function isValidUuid(str) {
25
+ if (typeof str !== 'string')
26
+ return false;
27
+ return UUID_REGEX.test(str);
28
+ }
29
+ /**
30
+ * Validates if a string is a valid Crockford Base32 format
31
+ */
32
+ function isValidBase32(str) {
33
+ if (typeof str !== 'string')
34
+ return false;
35
+ return BASE32_REGEX.test(str) && str.length > 0;
36
+ }
37
+ /**
38
+ * Normalizes UUID by removing hyphens
39
+ */
40
+ function normalizeUuid(uuid) {
41
+ return uuid.replace(/-/g, '');
42
+ }
43
+ /**
44
+ * Formats UUID by adding hyphens
45
+ */
46
+ function formatUuid(uuid) {
47
+ return `${uuid.slice(0, 8)}-${uuid.slice(8, 12)}-${uuid.slice(12, 16)}-${uuid.slice(16, 20)}-${uuid.slice(20, 32)}`;
48
+ }
49
+ /**
50
+ * Checks if running in browser environment
51
+ */
52
+ function checkNodeEnvironment() {
53
+ if (typeof globalThis.window !== 'undefined' || typeof globalThis.document !== 'undefined') {
54
+ throw new Error('@bboss/uuid32 is Node.js only and cannot run in browser environments');
55
+ }
56
+ }
57
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAkBA,kCAGC;AAKD,sCAGC;AAKD,sCAEC;AAKD,gCAEC;AAKD,oDAIC;AApDD;;GAEG;AACU,QAAA,YAAY,GAAG,kCAAkC,CAAC;AAE/D;;GAEG;AACH,MAAM,UAAU,GAAG,qEAAqE,CAAC;AAEzF;;GAEG;AACH,MAAM,YAAY,GAAG,gCAAgC,CAAC;AAEtD;;GAEG;AACH,SAAgB,WAAW,CAAC,GAAW;IACnC,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC1C,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,SAAgB,aAAa,CAAC,GAAW;IACrC,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC1C,OAAO,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;AACpD,CAAC;AAED;;GAEG;AACH,SAAgB,aAAa,CAAC,IAAY;IACtC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAgB,UAAU,CAAC,IAAY;IACnC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;AACxH,CAAC;AAED;;GAEG;AACH,SAAgB,oBAAoB;IAChC,IAAI,OAAQ,UAAkB,CAAC,MAAM,KAAK,WAAW,IAAI,OAAQ,UAAkB,CAAC,QAAQ,KAAK,WAAW,EAAE,CAAC;QAC3G,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;IAC5F,CAAC;AACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@bboss/uuid32",
3
+ "version": "0.1.0",
4
+ "description": "UUID to Crockford Base32 encoding/decoding library for Node.js",
5
+ "keywords": [
6
+ "uuid",
7
+ "base32",
8
+ "crockford",
9
+ "encoding",
10
+ "decoding",
11
+ "product-key",
12
+ "api-key",
13
+ "typescript",
14
+ "nodejs"
15
+ ],
16
+ "author": "bigboss.dev",
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/bigbossdev/uuid32.git"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/bigbossdev/uuid32/issues"
24
+ },
25
+ "homepage": "https://github.com/bigbossdev/uuid32#readme",
26
+ "main": "dist/index.js",
27
+ "types": "dist/index.d.ts",
28
+ "files": [
29
+ "dist/**/*",
30
+ "README.md",
31
+ "LICENSE"
32
+ ],
33
+ "engines": {
34
+ "node": ">=16.0.0"
35
+ },
36
+ "scripts": {
37
+ "build": "rimraf dist && tsc",
38
+ "test": "jest",
39
+ "test:watch": "jest --watch",
40
+ "test:coverage": "jest --coverage",
41
+ "prepublishOnly": "npm run build"
42
+ },
43
+ "devDependencies": {
44
+ "@types/jest": "^29.5.14",
45
+ "@types/node": "^22.0.0",
46
+ "jest": "^29.7.0",
47
+ "rimraf": "^6.0.1",
48
+ "ts-jest": "^29.4.5",
49
+ "typescript": "^5.9.3"
50
+ },
51
+ "packageManager": "yarn@4.10.3"
52
+ }