idchunk 1.1.1 → 2.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 +28 -28
- package/package.json +20 -7
- package/src/index.js +19 -11
- package/.gitattributes +0 -2
- package/id.png +0 -0
- package/test/index.test.js +0 -3
package/LICENSE
CHANGED
|
@@ -0,0 +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
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# ⚡ idchunk — Tiny, Fast & Customizable ID Generator
|
|
2
2
|
|
|
3
|
-
<table width="100%"
|
|
3
|
+
<table width="100%">
|
|
4
4
|
<tr>
|
|
5
5
|
<td>
|
|
6
6
|
<a href="https://www.npmjs.com/package/idchunk">
|
|
@@ -14,28 +14,26 @@
|
|
|
14
14
|
</a>
|
|
15
15
|
</td>
|
|
16
16
|
<td>
|
|
17
|
-
<a href="https://idchunk.netlify.app/"
|
|
18
|
-
<img
|
|
17
|
+
<a href="https://idchunk.netlify.app/">
|
|
18
|
+
<img src="https://i.pinimg.com/736x/d0/68/64/d06864b8b20b18921e465d401fa605e4.jpg" alt="idchunk barcode" width="60"/>
|
|
19
19
|
</a>
|
|
20
20
|
</td>
|
|
21
21
|
</tr>
|
|
22
22
|
</table>
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
> Generate short, unique, and customizable IDs for your applications in milliseconds.
|
|
24
|
+
> Generate short, secure, and customizable IDs for your applications in **milliseconds**.
|
|
26
25
|
|
|
27
26
|
---
|
|
28
27
|
|
|
29
|
-
## Features
|
|
30
|
-
|
|
31
|
-
- **
|
|
32
|
-
- **
|
|
33
|
-
- **
|
|
34
|
-
- **Reliable:** Generates collision-resistant IDs.
|
|
28
|
+
## ✨ Features
|
|
29
|
+
- **Tiny:** Zero dependencies, minimal footprint.
|
|
30
|
+
- **Secure & Fast:** Uses Node.js [`crypto`](https://nodejs.org/api/crypto.html).
|
|
31
|
+
- **Customizable:** Choose length *and* your own character set.
|
|
32
|
+
- **Reliable:** Collision-resistant random IDs.
|
|
35
33
|
|
|
36
34
|
---
|
|
37
35
|
|
|
38
|
-
## Installation
|
|
36
|
+
## 📦 Installation
|
|
39
37
|
|
|
40
38
|
```bash
|
|
41
39
|
npm install idchunk
|
|
@@ -50,26 +48,38 @@ By default, `idchunk()` generates a random ID of length **10**:
|
|
|
50
48
|
```js
|
|
51
49
|
const idchunk = require("idchunk");
|
|
52
50
|
|
|
53
|
-
console.log(idchunk());
|
|
51
|
+
console.log(idchunk());
|
|
52
|
+
// Example: "aZ8_-kL2pQ"
|
|
54
53
|
```
|
|
55
54
|
|
|
56
55
|
You can specify a custom length:
|
|
57
56
|
|
|
58
57
|
```js
|
|
59
|
-
|
|
58
|
+
console.log(idchunk(16));
|
|
59
|
+
// Example: "bQ9pL2_-aZ8kL2pQ"
|
|
60
|
+
```
|
|
60
61
|
|
|
61
|
-
|
|
62
|
+
Custom character set:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const customValues = "ABC123";
|
|
66
|
+
console.log(idchunk(8, customValues));
|
|
67
|
+
// Example: "1A23BCAB"
|
|
62
68
|
```
|
|
63
69
|
|
|
70
|
+
|
|
71
|
+
|
|
64
72
|
---
|
|
65
73
|
|
|
66
74
|
## API
|
|
67
75
|
|
|
68
|
-
### `idchunk(length?: number): string`
|
|
76
|
+
### `idchunk(length?: number, customValues?: string): string`
|
|
69
77
|
|
|
70
|
-
- `length` (optional)
|
|
71
|
-
- Returns: A random string ID.
|
|
78
|
+
- `length` (optional) → Length of the ID (default: 10).
|
|
72
79
|
|
|
80
|
+
- `customValues` (optional) → String of allowed characters (default: a-zA-Z0-9_-).
|
|
81
|
+
|
|
82
|
+
- Returns: Random string ID.
|
|
73
83
|
---
|
|
74
84
|
|
|
75
85
|
## How It Works
|
|
@@ -79,16 +89,6 @@ console.log(idchunk(16)); // Example output: "bQ9pL2_-aZ8kL2pQ"
|
|
|
79
89
|
|
|
80
90
|
---
|
|
81
91
|
|
|
82
|
-
## Testing
|
|
83
|
-
|
|
84
|
-
To test locally:
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
node test/index.test.js
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
---
|
|
91
|
-
|
|
92
92
|
## 📄 License
|
|
93
93
|
|
|
94
94
|
MIT © [Garv Thakral](https://github.com/codebygarv)
|
package/package.json
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "idchunk",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Tiny, Fast & Customizable ID Generator",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"README.md",
|
|
9
|
+
"LICENSE"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "node test/index.test.js",
|
|
13
|
+
"release": "npm version"
|
|
14
|
+
},
|
|
6
15
|
"keywords": [
|
|
7
16
|
"id",
|
|
8
|
-
"nanoid",
|
|
9
17
|
"generator",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
18
|
+
"crypto",
|
|
19
|
+
"uuid",
|
|
20
|
+
"shortid",
|
|
21
|
+
"random"
|
|
13
22
|
],
|
|
14
23
|
"author": "Garv Thakral",
|
|
15
|
-
"license": "MIT"
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/codebygarv/idchunk.git"
|
|
28
|
+
}
|
|
16
29
|
}
|
package/src/index.js
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
const
|
|
2
|
-
const crypto = require('crypto');
|
|
1
|
+
const crypto = require("crypto");
|
|
3
2
|
|
|
4
|
-
const
|
|
5
|
-
length = length || 10;
|
|
6
|
-
let result = '';
|
|
7
|
-
const valuesLength = values.length;
|
|
3
|
+
const DEFAULT_VALUES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
function getRandomInt(max) {
|
|
6
|
+
if (typeof crypto.randomInt === "function") {
|
|
7
|
+
return crypto.randomInt(0, max);
|
|
8
|
+
}
|
|
9
|
+
const byte = crypto.randomBytes(1)[0];
|
|
10
|
+
return byte % max;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function idchunk(length = 10, customValues = DEFAULT_VALUES) {
|
|
14
|
+
if (typeof customValues !== "string" || customValues.length === 0) {
|
|
15
|
+
throw new Error("customValues must be a non-empty string");
|
|
12
16
|
}
|
|
13
17
|
|
|
18
|
+
let result = "";
|
|
19
|
+
for (let i = 0; i < length; i++) {
|
|
20
|
+
const randomIndex = getRandomInt(customValues.length);
|
|
21
|
+
result += customValues[randomIndex];
|
|
22
|
+
}
|
|
14
23
|
return result;
|
|
15
24
|
}
|
|
16
25
|
|
|
17
|
-
|
|
18
|
-
module.exports = idchunk;
|
|
26
|
+
module.exports = idchunk;
|
package/.gitattributes
DELETED
package/id.png
DELETED
|
Binary file
|
package/test/index.test.js
DELETED