@rahul_dadhich15/id-toolkit 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 +21 -0
- package/README.md +36 -0
- package/dist/index.cjs +250 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +215 -0
- package/dist/secure-id/index.d.ts +3 -0
- package/dist/secure-id/index.d.ts.map +1 -0
- package/dist/secure-id/secure-id.d.ts +16 -0
- package/dist/secure-id/secure-id.d.ts.map +1 -0
- package/dist/short-id/index.d.ts +3 -0
- package/dist/short-id/index.d.ts.map +1 -0
- package/dist/short-id/short-id.d.ts +16 -0
- package/dist/short-id/short-id.d.ts.map +1 -0
- package/dist/uuid-generator/index.d.ts +3 -0
- package/dist/uuid-generator/index.d.ts.map +1 -0
- package/dist/uuid-generator/uuid-generator.d.ts +17 -0
- package/dist/uuid-generator/uuid-generator.d.ts.map +1 -0
- package/dist/uuid-validator/index.d.ts +3 -0
- package/dist/uuid-validator/index.d.ts.map +1 -0
- package/dist/uuid-validator/uuid-validator.d.ts +14 -0
- package/dist/uuid-validator/uuid-validator.d.ts.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rahuldadhich1517-sys
|
|
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,36 @@
|
|
|
1
|
+
# ID Toolkit
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@rahul_dadhich15/id-toolkit)
|
|
4
|
+
[](https://www.npmjs.com/package/@rahul_dadhich15/id-toolkit)
|
|
5
|
+
[](https://github.com/rahuldadhich15/id-toolkit/actions/workflows/ci.yml)
|
|
6
|
+
[](https://github.com/rahuldadhich15/id-toolkit/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
A lightweight, dependency-free TypeScript toolkit for generating, validating, and working with UUIDs, short IDs, and cryptographically secure identifiers.
|
|
9
|
+
|
|
10
|
+
Built for modern JavaScript and TypeScript applications.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Cryptographically secure random identifiers
|
|
17
|
+
- UUID v4 generation
|
|
18
|
+
- UUID v7 generation
|
|
19
|
+
- Bulk UUID generation
|
|
20
|
+
- UUID structure and version validation
|
|
21
|
+
- Compact URL-friendly short IDs
|
|
22
|
+
- Secure IDs using Node.js `crypto`
|
|
23
|
+
- Hex and Base64URL encoding
|
|
24
|
+
- Full TypeScript support
|
|
25
|
+
- ESM and CommonJS support
|
|
26
|
+
- Zero runtime dependencies
|
|
27
|
+
- Vitest test coverage
|
|
28
|
+
- Tree-shake friendly
|
|
29
|
+
- Lightweight
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install @rahul_dadhich15/id-toolkit
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
generateSecureId: () => generateSecureId,
|
|
24
|
+
generateSecureIds: () => generateSecureIds,
|
|
25
|
+
generateShortId: () => generateShortId,
|
|
26
|
+
generateShortIds: () => generateShortIds,
|
|
27
|
+
generateUUIDs: () => generateUUIDs,
|
|
28
|
+
getUUIDVersion: () => getUUIDVersion,
|
|
29
|
+
isUUID: () => isUUID,
|
|
30
|
+
uuidV4: () => uuidV4,
|
|
31
|
+
uuidV7: () => uuidV7
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/uuid-generator/uuid-generator.ts
|
|
36
|
+
var import_node_crypto = require("crypto");
|
|
37
|
+
function uuidV4() {
|
|
38
|
+
const bytes = (0, import_node_crypto.randomBytes)(16);
|
|
39
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
40
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
41
|
+
return bytesToUUID(bytes);
|
|
42
|
+
}
|
|
43
|
+
function uuidV7() {
|
|
44
|
+
const bytes = (0, import_node_crypto.randomBytes)(16);
|
|
45
|
+
const timestamp = BigInt(Date.now());
|
|
46
|
+
bytes[0] = Number(timestamp >> 40n & 0xffn);
|
|
47
|
+
bytes[1] = Number(timestamp >> 32n & 0xffn);
|
|
48
|
+
bytes[2] = Number(timestamp >> 24n & 0xffn);
|
|
49
|
+
bytes[3] = Number(timestamp >> 16n & 0xffn);
|
|
50
|
+
bytes[4] = Number(timestamp >> 8n & 0xffn);
|
|
51
|
+
bytes[5] = Number(timestamp & 0xffn);
|
|
52
|
+
bytes[6] = bytes[6] & 15 | 112;
|
|
53
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
54
|
+
return bytesToUUID(bytes);
|
|
55
|
+
}
|
|
56
|
+
function generateUUIDs(count, version = "v4") {
|
|
57
|
+
validateCount(count);
|
|
58
|
+
const generator = version === "v7" ? uuidV7 : uuidV4;
|
|
59
|
+
return Array.from({ length: count }, generator);
|
|
60
|
+
}
|
|
61
|
+
function bytesToUUID(bytes) {
|
|
62
|
+
if (bytes.length !== 16) {
|
|
63
|
+
throw new RangeError("UUID requires exactly 16 bytes");
|
|
64
|
+
}
|
|
65
|
+
const hex = Array.from(
|
|
66
|
+
bytes,
|
|
67
|
+
(byte) => byte.toString(16).padStart(2, "0")
|
|
68
|
+
).join("");
|
|
69
|
+
return [
|
|
70
|
+
hex.slice(0, 8),
|
|
71
|
+
hex.slice(8, 12),
|
|
72
|
+
hex.slice(12, 16),
|
|
73
|
+
hex.slice(16, 20),
|
|
74
|
+
hex.slice(20, 32)
|
|
75
|
+
].join("-");
|
|
76
|
+
}
|
|
77
|
+
function validateCount(count) {
|
|
78
|
+
if (!Number.isInteger(count)) {
|
|
79
|
+
throw new TypeError("count must be an integer");
|
|
80
|
+
}
|
|
81
|
+
if (count < 1) {
|
|
82
|
+
throw new RangeError("count must be greater than 0");
|
|
83
|
+
}
|
|
84
|
+
if (count > 1e6) {
|
|
85
|
+
throw new RangeError("count cannot exceed 1,000,000");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/uuid-validator/uuid-validator.ts
|
|
90
|
+
var UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
91
|
+
function isUUID(value, version) {
|
|
92
|
+
if (typeof value !== "string") {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (!UUID_REGEX.test(value)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (version !== void 0) {
|
|
99
|
+
return getUUIDVersion(value) === version;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
function getUUIDVersion(value) {
|
|
104
|
+
if (!isUUID(value)) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return Number(value[14]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/short-id/short-id.ts
|
|
111
|
+
var import_node_crypto2 = require("crypto");
|
|
112
|
+
var DEFAULT_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
113
|
+
var DEFAULT_LENGTH = 12;
|
|
114
|
+
function generateShortId(options = {}) {
|
|
115
|
+
const length = options.length ?? DEFAULT_LENGTH;
|
|
116
|
+
const alphabet = options.alphabet ?? DEFAULT_ALPHABET;
|
|
117
|
+
validateLength(length);
|
|
118
|
+
validateAlphabet(alphabet);
|
|
119
|
+
const result = [];
|
|
120
|
+
const limit = Math.floor(256 / alphabet.length) * alphabet.length;
|
|
121
|
+
while (result.length < length) {
|
|
122
|
+
const bytes = (0, import_node_crypto2.randomBytes)(length - result.length);
|
|
123
|
+
for (const byte of bytes) {
|
|
124
|
+
if (byte >= limit) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
result.push(alphabet[byte % alphabet.length]);
|
|
128
|
+
if (result.length === length) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return result.join("");
|
|
134
|
+
}
|
|
135
|
+
function generateShortIds(count, options = {}) {
|
|
136
|
+
validateCount2(count);
|
|
137
|
+
return Array.from(
|
|
138
|
+
{ length: count },
|
|
139
|
+
() => generateShortId(options)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
function validateLength(length) {
|
|
143
|
+
if (!Number.isInteger(length)) {
|
|
144
|
+
throw new TypeError("length must be an integer");
|
|
145
|
+
}
|
|
146
|
+
if (length < 1) {
|
|
147
|
+
throw new RangeError("length must be greater than 0");
|
|
148
|
+
}
|
|
149
|
+
if (length > 256) {
|
|
150
|
+
throw new RangeError("length cannot exceed 256");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function validateCount2(count) {
|
|
154
|
+
if (!Number.isInteger(count)) {
|
|
155
|
+
throw new TypeError("count must be an integer");
|
|
156
|
+
}
|
|
157
|
+
if (count < 1) {
|
|
158
|
+
throw new RangeError("count must be greater than 0");
|
|
159
|
+
}
|
|
160
|
+
if (count > 1e6) {
|
|
161
|
+
throw new RangeError("count cannot exceed 1,000,000");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function validateAlphabet(alphabet) {
|
|
165
|
+
if (typeof alphabet !== "string") {
|
|
166
|
+
throw new TypeError("alphabet must be a string");
|
|
167
|
+
}
|
|
168
|
+
if (alphabet.length < 2) {
|
|
169
|
+
throw new RangeError(
|
|
170
|
+
"alphabet must contain at least 2 characters"
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (alphabet.length > 256) {
|
|
174
|
+
throw new RangeError(
|
|
175
|
+
"alphabet cannot contain more than 256 characters"
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const uniqueCharacters = new Set(alphabet);
|
|
179
|
+
if (uniqueCharacters.size !== alphabet.length) {
|
|
180
|
+
throw new RangeError(
|
|
181
|
+
"alphabet must contain unique characters"
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// src/secure-id/secure-id.ts
|
|
187
|
+
var import_node_crypto3 = require("crypto");
|
|
188
|
+
function generateSecureId(options = {}) {
|
|
189
|
+
const bytes = options.bytes ?? 32;
|
|
190
|
+
const encoding = options.encoding ?? "hex";
|
|
191
|
+
validateBytes(bytes);
|
|
192
|
+
validateEncoding(encoding);
|
|
193
|
+
return (0, import_node_crypto3.randomBytes)(bytes).toString(encoding);
|
|
194
|
+
}
|
|
195
|
+
function generateSecureIds(count, options = {}) {
|
|
196
|
+
validateCount3(count);
|
|
197
|
+
return Array.from(
|
|
198
|
+
{ length: count },
|
|
199
|
+
() => generateSecureId(options)
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
function validateBytes(bytes) {
|
|
203
|
+
if (!Number.isInteger(bytes)) {
|
|
204
|
+
throw new TypeError("bytes must be an integer");
|
|
205
|
+
}
|
|
206
|
+
if (bytes < 1) {
|
|
207
|
+
throw new RangeError(
|
|
208
|
+
"bytes must be greater than 0"
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
if (bytes > 1024) {
|
|
212
|
+
throw new RangeError(
|
|
213
|
+
"bytes cannot exceed 1024"
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function validateCount3(count) {
|
|
218
|
+
if (!Number.isInteger(count)) {
|
|
219
|
+
throw new TypeError("count must be an integer");
|
|
220
|
+
}
|
|
221
|
+
if (count < 1) {
|
|
222
|
+
throw new RangeError(
|
|
223
|
+
"count must be greater than 0"
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
if (count > 1e6) {
|
|
227
|
+
throw new RangeError(
|
|
228
|
+
"count cannot exceed 1,000,000"
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function validateEncoding(encoding) {
|
|
233
|
+
if (encoding !== "hex" && encoding !== "base64url") {
|
|
234
|
+
throw new RangeError(
|
|
235
|
+
'encoding must be either "hex" or "base64url"'
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
240
|
+
0 && (module.exports = {
|
|
241
|
+
generateSecureId,
|
|
242
|
+
generateSecureIds,
|
|
243
|
+
generateShortId,
|
|
244
|
+
generateShortIds,
|
|
245
|
+
generateUUIDs,
|
|
246
|
+
getUUIDVersion,
|
|
247
|
+
isUUID,
|
|
248
|
+
uuidV4,
|
|
249
|
+
uuidV7
|
|
250
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { uuidV4, uuidV7, generateUUIDs, } from "./uuid-generator/index.js";
|
|
2
|
+
export type { UUIDVersion as UUIDGeneratorVersion, } from "./uuid-generator/index.js";
|
|
3
|
+
export { isUUID, getUUIDVersion, } from "./uuid-validator/index.js";
|
|
4
|
+
export type { UUIDVersion as UUIDValidatorVersion, } from "./uuid-validator/index.js";
|
|
5
|
+
export { generateShortId, generateShortIds, } from "./short-id/index.js";
|
|
6
|
+
export type { ShortIdOptions, } from "./short-id/index.js";
|
|
7
|
+
export { generateSecureId, generateSecureIds, } from "./secure-id/index.js";
|
|
8
|
+
export type { SecureIdEncoding, SecureIdOptions, } from "./secure-id/index.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,MAAM,EACN,aAAa,GACd,MAAM,2BAA2B,CAAC;AAEnC,YAAY,EACV,WAAW,IAAI,oBAAoB,GACpC,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EACL,MAAM,EACN,cAAc,GACf,MAAM,2BAA2B,CAAC;AAEnC,YAAY,EACV,WAAW,IAAI,oBAAoB,GACpC,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EACL,eAAe,EACf,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,cAAc,GACf,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAE9B,YAAY,EACV,gBAAgB,EAChB,eAAe,GAChB,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// src/uuid-generator/uuid-generator.ts
|
|
2
|
+
import { randomBytes } from "crypto";
|
|
3
|
+
function uuidV4() {
|
|
4
|
+
const bytes = randomBytes(16);
|
|
5
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
6
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
7
|
+
return bytesToUUID(bytes);
|
|
8
|
+
}
|
|
9
|
+
function uuidV7() {
|
|
10
|
+
const bytes = randomBytes(16);
|
|
11
|
+
const timestamp = BigInt(Date.now());
|
|
12
|
+
bytes[0] = Number(timestamp >> 40n & 0xffn);
|
|
13
|
+
bytes[1] = Number(timestamp >> 32n & 0xffn);
|
|
14
|
+
bytes[2] = Number(timestamp >> 24n & 0xffn);
|
|
15
|
+
bytes[3] = Number(timestamp >> 16n & 0xffn);
|
|
16
|
+
bytes[4] = Number(timestamp >> 8n & 0xffn);
|
|
17
|
+
bytes[5] = Number(timestamp & 0xffn);
|
|
18
|
+
bytes[6] = bytes[6] & 15 | 112;
|
|
19
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
20
|
+
return bytesToUUID(bytes);
|
|
21
|
+
}
|
|
22
|
+
function generateUUIDs(count, version = "v4") {
|
|
23
|
+
validateCount(count);
|
|
24
|
+
const generator = version === "v7" ? uuidV7 : uuidV4;
|
|
25
|
+
return Array.from({ length: count }, generator);
|
|
26
|
+
}
|
|
27
|
+
function bytesToUUID(bytes) {
|
|
28
|
+
if (bytes.length !== 16) {
|
|
29
|
+
throw new RangeError("UUID requires exactly 16 bytes");
|
|
30
|
+
}
|
|
31
|
+
const hex = Array.from(
|
|
32
|
+
bytes,
|
|
33
|
+
(byte) => byte.toString(16).padStart(2, "0")
|
|
34
|
+
).join("");
|
|
35
|
+
return [
|
|
36
|
+
hex.slice(0, 8),
|
|
37
|
+
hex.slice(8, 12),
|
|
38
|
+
hex.slice(12, 16),
|
|
39
|
+
hex.slice(16, 20),
|
|
40
|
+
hex.slice(20, 32)
|
|
41
|
+
].join("-");
|
|
42
|
+
}
|
|
43
|
+
function validateCount(count) {
|
|
44
|
+
if (!Number.isInteger(count)) {
|
|
45
|
+
throw new TypeError("count must be an integer");
|
|
46
|
+
}
|
|
47
|
+
if (count < 1) {
|
|
48
|
+
throw new RangeError("count must be greater than 0");
|
|
49
|
+
}
|
|
50
|
+
if (count > 1e6) {
|
|
51
|
+
throw new RangeError("count cannot exceed 1,000,000");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/uuid-validator/uuid-validator.ts
|
|
56
|
+
var UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
57
|
+
function isUUID(value, version) {
|
|
58
|
+
if (typeof value !== "string") {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (!UUID_REGEX.test(value)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (version !== void 0) {
|
|
65
|
+
return getUUIDVersion(value) === version;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
function getUUIDVersion(value) {
|
|
70
|
+
if (!isUUID(value)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return Number(value[14]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/short-id/short-id.ts
|
|
77
|
+
import { randomBytes as randomBytes2 } from "crypto";
|
|
78
|
+
var DEFAULT_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
79
|
+
var DEFAULT_LENGTH = 12;
|
|
80
|
+
function generateShortId(options = {}) {
|
|
81
|
+
const length = options.length ?? DEFAULT_LENGTH;
|
|
82
|
+
const alphabet = options.alphabet ?? DEFAULT_ALPHABET;
|
|
83
|
+
validateLength(length);
|
|
84
|
+
validateAlphabet(alphabet);
|
|
85
|
+
const result = [];
|
|
86
|
+
const limit = Math.floor(256 / alphabet.length) * alphabet.length;
|
|
87
|
+
while (result.length < length) {
|
|
88
|
+
const bytes = randomBytes2(length - result.length);
|
|
89
|
+
for (const byte of bytes) {
|
|
90
|
+
if (byte >= limit) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
result.push(alphabet[byte % alphabet.length]);
|
|
94
|
+
if (result.length === length) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return result.join("");
|
|
100
|
+
}
|
|
101
|
+
function generateShortIds(count, options = {}) {
|
|
102
|
+
validateCount2(count);
|
|
103
|
+
return Array.from(
|
|
104
|
+
{ length: count },
|
|
105
|
+
() => generateShortId(options)
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
function validateLength(length) {
|
|
109
|
+
if (!Number.isInteger(length)) {
|
|
110
|
+
throw new TypeError("length must be an integer");
|
|
111
|
+
}
|
|
112
|
+
if (length < 1) {
|
|
113
|
+
throw new RangeError("length must be greater than 0");
|
|
114
|
+
}
|
|
115
|
+
if (length > 256) {
|
|
116
|
+
throw new RangeError("length cannot exceed 256");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function validateCount2(count) {
|
|
120
|
+
if (!Number.isInteger(count)) {
|
|
121
|
+
throw new TypeError("count must be an integer");
|
|
122
|
+
}
|
|
123
|
+
if (count < 1) {
|
|
124
|
+
throw new RangeError("count must be greater than 0");
|
|
125
|
+
}
|
|
126
|
+
if (count > 1e6) {
|
|
127
|
+
throw new RangeError("count cannot exceed 1,000,000");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function validateAlphabet(alphabet) {
|
|
131
|
+
if (typeof alphabet !== "string") {
|
|
132
|
+
throw new TypeError("alphabet must be a string");
|
|
133
|
+
}
|
|
134
|
+
if (alphabet.length < 2) {
|
|
135
|
+
throw new RangeError(
|
|
136
|
+
"alphabet must contain at least 2 characters"
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
if (alphabet.length > 256) {
|
|
140
|
+
throw new RangeError(
|
|
141
|
+
"alphabet cannot contain more than 256 characters"
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const uniqueCharacters = new Set(alphabet);
|
|
145
|
+
if (uniqueCharacters.size !== alphabet.length) {
|
|
146
|
+
throw new RangeError(
|
|
147
|
+
"alphabet must contain unique characters"
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/secure-id/secure-id.ts
|
|
153
|
+
import { randomBytes as randomBytes3 } from "crypto";
|
|
154
|
+
function generateSecureId(options = {}) {
|
|
155
|
+
const bytes = options.bytes ?? 32;
|
|
156
|
+
const encoding = options.encoding ?? "hex";
|
|
157
|
+
validateBytes(bytes);
|
|
158
|
+
validateEncoding(encoding);
|
|
159
|
+
return randomBytes3(bytes).toString(encoding);
|
|
160
|
+
}
|
|
161
|
+
function generateSecureIds(count, options = {}) {
|
|
162
|
+
validateCount3(count);
|
|
163
|
+
return Array.from(
|
|
164
|
+
{ length: count },
|
|
165
|
+
() => generateSecureId(options)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
function validateBytes(bytes) {
|
|
169
|
+
if (!Number.isInteger(bytes)) {
|
|
170
|
+
throw new TypeError("bytes must be an integer");
|
|
171
|
+
}
|
|
172
|
+
if (bytes < 1) {
|
|
173
|
+
throw new RangeError(
|
|
174
|
+
"bytes must be greater than 0"
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
if (bytes > 1024) {
|
|
178
|
+
throw new RangeError(
|
|
179
|
+
"bytes cannot exceed 1024"
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function validateCount3(count) {
|
|
184
|
+
if (!Number.isInteger(count)) {
|
|
185
|
+
throw new TypeError("count must be an integer");
|
|
186
|
+
}
|
|
187
|
+
if (count < 1) {
|
|
188
|
+
throw new RangeError(
|
|
189
|
+
"count must be greater than 0"
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
if (count > 1e6) {
|
|
193
|
+
throw new RangeError(
|
|
194
|
+
"count cannot exceed 1,000,000"
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function validateEncoding(encoding) {
|
|
199
|
+
if (encoding !== "hex" && encoding !== "base64url") {
|
|
200
|
+
throw new RangeError(
|
|
201
|
+
'encoding must be either "hex" or "base64url"'
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
export {
|
|
206
|
+
generateSecureId,
|
|
207
|
+
generateSecureIds,
|
|
208
|
+
generateShortId,
|
|
209
|
+
generateShortIds,
|
|
210
|
+
generateUUIDs,
|
|
211
|
+
getUUIDVersion,
|
|
212
|
+
isUUID,
|
|
213
|
+
uuidV4,
|
|
214
|
+
uuidV7
|
|
215
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/secure-id/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AAExB,YAAY,EACV,gBAAgB,EAChB,eAAe,GAChB,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type SecureIdEncoding = "hex" | "base64url";
|
|
2
|
+
export interface SecureIdOptions {
|
|
3
|
+
bytes?: number;
|
|
4
|
+
encoding?: SecureIdEncoding;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Generate a cryptographically secure identifier.
|
|
8
|
+
*
|
|
9
|
+
* @param options Configuration for byte length and encoding.
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateSecureId(options?: SecureIdOptions): string;
|
|
12
|
+
/**
|
|
13
|
+
* Generate multiple cryptographically secure identifiers.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateSecureIds(count: number, options?: SecureIdOptions): string[];
|
|
16
|
+
//# sourceMappingURL=secure-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secure-id.d.ts","sourceRoot":"","sources":["../../src/secure-id/secure-id.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,WAAW,CAAC;AAEnD,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC7B;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAE,eAAoB,GAC5B,MAAM,CAQR;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,MAAM,EAAE,CAOV"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/short-id/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,gBAAgB,GACjB,MAAM,eAAe,CAAC;AAEvB,YAAY,EACV,cAAc,GACf,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ShortIdOptions {
|
|
2
|
+
length?: number;
|
|
3
|
+
alphabet?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Generate a compact URL-friendly identifier.
|
|
7
|
+
*
|
|
8
|
+
* Uses cryptographically secure random bytes and rejection
|
|
9
|
+
* sampling to avoid modulo bias.
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateShortId(options?: ShortIdOptions): string;
|
|
12
|
+
/**
|
|
13
|
+
* Generate multiple compact identifiers.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateShortIds(count: number, options?: ShortIdOptions): string[];
|
|
16
|
+
//# sourceMappingURL=short-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"short-id.d.ts","sourceRoot":"","sources":["../../src/short-id/short-id.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAOD;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,OAAO,GAAE,cAAmB,GAC3B,MAAM,CA+BR;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,cAAmB,GAC3B,MAAM,EAAE,CAOV"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/uuid-generator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,MAAM,EACN,aAAa,GACd,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,WAAW,GACZ,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type UUIDVersion = "v4" | "v7";
|
|
2
|
+
/**
|
|
3
|
+
* Generate a UUID v4 using cryptographically secure random bytes.
|
|
4
|
+
*/
|
|
5
|
+
export declare function uuidV4(): string;
|
|
6
|
+
/**
|
|
7
|
+
* Generate a UUID v7 using the current Unix timestamp.
|
|
8
|
+
*
|
|
9
|
+
* UUID v7 is time-ordered and useful for sortable identifiers,
|
|
10
|
+
* database keys, event IDs, and distributed systems.
|
|
11
|
+
*/
|
|
12
|
+
export declare function uuidV7(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Generate multiple UUIDs.
|
|
15
|
+
*/
|
|
16
|
+
export declare function generateUUIDs(count: number, version?: UUIDVersion): string[];
|
|
17
|
+
//# sourceMappingURL=uuid-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid-generator.d.ts","sourceRoot":"","sources":["../../src/uuid-generator/uuid-generator.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,WAAW,GAAG,IAAI,GAAG,IAAI,CAAC;AAEtC;;GAEG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAQ/B;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAoB/B;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,WAAkB,GAC1B,MAAM,EAAE,CAMV"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/uuid-validator/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,cAAc,GACf,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,WAAW,GACZ,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type UUIDVersion = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
|
|
2
|
+
/**
|
|
3
|
+
* Check whether a value is a valid UUID.
|
|
4
|
+
*
|
|
5
|
+
* Optionally validates a specific UUID version.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isUUID(value: unknown, version?: UUIDVersion): value is string;
|
|
8
|
+
/**
|
|
9
|
+
* Get the UUID version.
|
|
10
|
+
*
|
|
11
|
+
* Returns null when the value is not a valid UUID.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getUUIDVersion(value: unknown): UUIDVersion | null;
|
|
14
|
+
//# sourceMappingURL=uuid-validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid-validator.d.ts","sourceRoot":"","sources":["../../src/uuid-validator/uuid-validator.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAKxD;;;;GAIG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,WAAW,GACpB,KAAK,IAAI,MAAM,CAcjB;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,OAAO,GACb,WAAW,GAAG,IAAI,CAMpB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rahul_dadhich15/id-toolkit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lightweight, dependency-free TypeScript toolkit for UUIDs, short IDs, and secure identifiers.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/rahuldadhich15/id-toolkit.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/rahuldadhich15/id-toolkit/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/rahuldadhich15/id-toolkit#readme",
|
|
31
|
+
"keywords": [
|
|
32
|
+
"uuid",
|
|
33
|
+
"uuid-v4",
|
|
34
|
+
"uuid-v7",
|
|
35
|
+
"uuid-generator",
|
|
36
|
+
"uuid-validator",
|
|
37
|
+
"short-id",
|
|
38
|
+
"secure-id",
|
|
39
|
+
"id-generator",
|
|
40
|
+
"identifier",
|
|
41
|
+
"random-id",
|
|
42
|
+
"crypto",
|
|
43
|
+
"typescript",
|
|
44
|
+
"javascript",
|
|
45
|
+
"utilities"
|
|
46
|
+
],
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "npm run clean && tsup src/index.ts --format esm,cjs && tsc",
|
|
50
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
51
|
+
"test": "vitest",
|
|
52
|
+
"test:run": "vitest run",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "^22.20.2",
|
|
57
|
+
"tsup": "^8.0.0",
|
|
58
|
+
"typescript": "^5.0.0",
|
|
59
|
+
"vitest": "^3.0.0"
|
|
60
|
+
}
|
|
61
|
+
}
|