idchunk 2.1.0 → 2.4.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 +20 -20
- package/README.md +131 -99
- package/package.json +7 -2
- package/src/index.d.ts +25 -0
- package/src/index.js +61 -26
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Garv Thakral
|
|
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
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Garv Thakral
|
|
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
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,100 +1,132 @@
|
|
|
1
|
-
# ⚡ idchunk — Tiny, Fast & Customizable ID Generator
|
|
2
|
-
|
|
3
|
-
<table width="100%">
|
|
4
|
-
<tr>
|
|
5
|
-
<td>
|
|
6
|
-
<a href="https://www.npmjs.com/package/idchunk">
|
|
7
|
-
<img src="https://img.shields.io/npm/v/idchunk.svg" alt="npm version"/>
|
|
8
|
-
</a>
|
|
9
|
-
<a href="https://www.npmjs.com/package/idchunk">
|
|
10
|
-
<img src="https://img.shields.io/npm/dt/idchunk.svg" alt="downloads"/>
|
|
11
|
-
</a>
|
|
12
|
-
<a href="https://github.com/garvthakral/idchunk/blob/main/LICENSE">
|
|
13
|
-
<img src="https://img.shields.io/npm/l/idchunk.svg" alt="license"/>
|
|
14
|
-
</a>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
</
|
|
23
|
-
|
|
24
|
-
>
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
1
|
+
# ⚡ idchunk — Tiny, Blazingly Fast & Customizable ID Generator
|
|
2
|
+
|
|
3
|
+
<table width="100%">
|
|
4
|
+
<tr>
|
|
5
|
+
<td>
|
|
6
|
+
<a href="https://www.npmjs.com/package/idchunk">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/idchunk.svg" alt="npm version"/>
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/idchunk">
|
|
10
|
+
<img src="https://img.shields.io/npm/dt/idchunk.svg" alt="downloads"/>
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/garvthakral/idchunk/blob/main/LICENSE">
|
|
13
|
+
<img src="https://img.shields.io/npm/l/idchunk.svg" alt="license"/>
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/idchunk">
|
|
16
|
+
<img src="https://img.shields.io/npm/types/idchunk.svg" alt="types included"/>
|
|
17
|
+
</a>
|
|
18
|
+
</td>
|
|
19
|
+
<td>
|
|
20
|
+
<a href="https://idchunk.netlify.app/">
|
|
21
|
+
<img src="https://i.pinimg.com/736x/d0/68/64/d06864b8b20b18921e465d401fa605e4.jpg" alt="idchunk barcode" width="60"/>
|
|
22
|
+
</a>
|
|
23
|
+
</td>
|
|
24
|
+
</tr>
|
|
25
|
+
</table>
|
|
26
|
+
|
|
27
|
+
> Generate short, secure, and customizable IDs for your applications in **milliseconds**.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## ✨ Features
|
|
32
|
+
- **🚀 Blazingly Fast:** Uses optimized bulk random-byte generation for maximum performance.
|
|
33
|
+
- **🛡️ TypeScript Ready:** Native `.d.ts` definitions included out of the box.
|
|
34
|
+
- **🧩 Built-in Presets:** Instantly generate OTP numbers, hex tokens, and more.
|
|
35
|
+
- **📦 Tiny:** Zero dependencies, incredibly minimal footprint.
|
|
36
|
+
- **🔒 Secure:** Uses Node.js [`crypto`](https://nodejs.org/api/crypto.html) for cryptographically secure randomness.
|
|
37
|
+
- **🛠️ Customizable:** Choose the exact length *and* your own custom character set.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 🏎️ Benchmark
|
|
42
|
+
*Recorded on June 21, 2026 (1,000,000 iterations generating 10-character IDs).*
|
|
43
|
+
|
|
44
|
+
**`idchunk` is ~40% faster than NanoID** when using custom alphabets!
|
|
45
|
+
|
|
46
|
+
| Library | Standard (10 chars) | Custom Alphabet (10 chars) |
|
|
47
|
+
| :--- | :--- | :--- |
|
|
48
|
+
| `crypto.randomUUID()` | 101.69 ms | N/A |
|
|
49
|
+
| `uuid v4` | 184.39 ms | N/A |
|
|
50
|
+
| **`nanoid`** | 211.22 ms | 472.50 ms |
|
|
51
|
+
| **`idchunk`** | 235.55 ms | **288.59 ms** 🏆 |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 📦 Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install idchunk
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
By default, `idchunk()` generates a random ID of length **10**:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
const idchunk = require("idchunk");
|
|
69
|
+
|
|
70
|
+
console.log(idchunk());
|
|
71
|
+
// Example: "aZ8_-kL2pQ"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
You can specify a custom length:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
console.log(idchunk(16));
|
|
78
|
+
// Example: "bQ9pL2_-aZ8kL2pQ"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Custom character set:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
const customValues = "ABC123";
|
|
85
|
+
console.log(idchunk(8, customValues));
|
|
86
|
+
// Example: "1A23BCAB"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 🎯 Built-in Presets
|
|
90
|
+
Don't want to define custom alphabets? Use the built-in presets:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
const idchunk = require("idchunk");
|
|
94
|
+
|
|
95
|
+
console.log(idchunk.numbers(6)); // "829103" - Great for OTPs and 2FA
|
|
96
|
+
console.log(idchunk.hex(12)); // "e4368746a829" - Great for tokens
|
|
97
|
+
console.log(idchunk.lowercase(8)); // "ymddmahe"
|
|
98
|
+
console.log(idchunk.uppercase(8)); // "WFKVFGSW"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## API
|
|
104
|
+
|
|
105
|
+
### `idchunk(length?: number, customValues?: string): string`
|
|
106
|
+
- `length` (optional) → Length of the ID (default: 10).
|
|
107
|
+
- `customValues` (optional) → String of allowed characters (default: a-zA-Z0-9_-).
|
|
108
|
+
- Returns: Random string ID.
|
|
109
|
+
|
|
110
|
+
### `idchunk.numbers(length?: number): string`
|
|
111
|
+
### `idchunk.hex(length?: number): string`
|
|
112
|
+
### `idchunk.lowercase(length?: number): string`
|
|
113
|
+
### `idchunk.uppercase(length?: number): string`
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## How It Works
|
|
118
|
+
|
|
119
|
+
- Pre-allocates an array of secure random bytes using Node.js [`crypto.randomBytes`](https://nodejs.org/api/crypto.html) or `crypto.getRandomValues()` for a massive performance boost over single-character generation.
|
|
120
|
+
- Default character set: `a-z`, `A-Z`, `0-9`, `_`, `-`.
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## 📄 License
|
|
125
|
+
|
|
126
|
+
MIT © [Garv Thakral](https://github.com/codebygarv)
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Documentation
|
|
131
|
+
|
|
100
132
|
Read full documentation here : [**Read Docs**](https://idchunk.netlify.app/)
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "idchunk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Tiny, Fast & Customizable ID Generator",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
6
7
|
"files": [
|
|
7
8
|
"src",
|
|
8
9
|
"README.md",
|
|
@@ -25,5 +26,9 @@
|
|
|
25
26
|
"repository": {
|
|
26
27
|
"type": "git",
|
|
27
28
|
"url": "git+https://github.com/codebygarv/idchunk.git"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"nanoid": "^3.3.14",
|
|
32
|
+
"uuid": "^14.0.1"
|
|
28
33
|
}
|
|
29
|
-
}
|
|
34
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare module "idchunk" {
|
|
2
|
+
/**
|
|
3
|
+
* Generates a random ID of the specified length using the provided custom values.
|
|
4
|
+
* @param length The length of the ID to generate (default: 10).
|
|
5
|
+
* @param customValues A string of characters to use for the ID (default: a-zA-Z0-9_-).
|
|
6
|
+
* @returns A string representing the generated ID.
|
|
7
|
+
*/
|
|
8
|
+
function idchunk(length?: number, customValues?: string): string;
|
|
9
|
+
|
|
10
|
+
namespace idchunk {
|
|
11
|
+
/** Generates a random ID consisting only of numbers (0-9). */
|
|
12
|
+
export function numbers(length?: number): string;
|
|
13
|
+
|
|
14
|
+
/** Generates a random ID consisting of lowercase hex characters (0-9, a-f). */
|
|
15
|
+
export function hex(length?: number): string;
|
|
16
|
+
|
|
17
|
+
/** Generates a random ID consisting only of lowercase letters (a-z). */
|
|
18
|
+
export function lowercase(length?: number): string;
|
|
19
|
+
|
|
20
|
+
/** Generates a random ID consisting only of uppercase letters (A-Z). */
|
|
21
|
+
export function uppercase(length?: number): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export = idchunk;
|
|
25
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,26 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
let nodeCrypto;
|
|
2
|
+
try {
|
|
3
|
+
nodeCrypto = require("crypto");
|
|
4
|
+
} catch {
|
|
5
|
+
nodeCrypto = null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const DEFAULT_VALUES =
|
|
9
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
|
|
10
|
+
|
|
11
|
+
const POOL_SIZE = 4096;
|
|
12
|
+
let pool;
|
|
13
|
+
let poolOffset = POOL_SIZE;
|
|
14
|
+
|
|
15
|
+
function fillPool() {
|
|
16
|
+
if (nodeCrypto && typeof nodeCrypto.randomFillSync === "function") {
|
|
17
|
+
if (!pool || pool.length !== POOL_SIZE) {
|
|
18
|
+
pool = typeof Buffer !== "undefined" ? Buffer.allocUnsafe(POOL_SIZE) : new Uint8Array(POOL_SIZE);
|
|
19
|
+
}
|
|
20
|
+
nodeCrypto.randomFillSync(pool);
|
|
21
|
+
} else if (typeof globalThis.crypto !== "undefined" && globalThis.crypto.getRandomValues) {
|
|
22
|
+
if (!pool || pool.length !== POOL_SIZE) {
|
|
23
|
+
pool = new Uint8Array(POOL_SIZE);
|
|
24
|
+
}
|
|
25
|
+
globalThis.crypto.getRandomValues(pool);
|
|
26
|
+
} else {
|
|
27
|
+
if (!pool || pool.length !== POOL_SIZE) {
|
|
28
|
+
pool = new Uint8Array(POOL_SIZE);
|
|
29
|
+
}
|
|
30
|
+
for (let i = 0; i < POOL_SIZE; i++) {
|
|
31
|
+
pool[i] = Math.floor(Math.random() * 256);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
poolOffset = 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function idchunk(length = 10, customValues = DEFAULT_VALUES) {
|
|
38
|
+
if (typeof customValues !== "string" || customValues.length === 0) {
|
|
39
|
+
throw new Error("customValues must be a non-empty string");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const limit = customValues.length;
|
|
43
|
+
let result = "";
|
|
44
|
+
|
|
45
|
+
for (let i = 0; i < length; i++) {
|
|
46
|
+
if (poolOffset >= POOL_SIZE) {
|
|
47
|
+
fillPool();
|
|
48
|
+
}
|
|
49
|
+
result += customValues[pool[poolOffset++] % limit];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Presets
|
|
56
|
+
idchunk.numbers = (length = 10) => idchunk(length, "0123456789");
|
|
57
|
+
idchunk.hex = (length = 10) => idchunk(length, "0123456789abcdef");
|
|
58
|
+
idchunk.lowercase = (length = 10) => idchunk(length, "abcdefghijklmnopqrstuvwxyz");
|
|
59
|
+
idchunk.uppercase = (length = 10) => idchunk(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
|
60
|
+
|
|
61
|
+
module.exports = idchunk;
|