idchunk 2.0.0 → 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/package.json +1 -1
- package/src/index.js +12 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -2,15 +2,25 @@ const crypto = require("crypto");
|
|
|
2
2
|
|
|
3
3
|
const DEFAULT_VALUES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
|
|
4
4
|
|
|
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
|
+
|
|
5
13
|
function idchunk(length = 10, customValues = DEFAULT_VALUES) {
|
|
6
14
|
if (typeof customValues !== "string" || customValues.length === 0) {
|
|
7
15
|
throw new Error("customValues must be a non-empty string");
|
|
8
16
|
}
|
|
17
|
+
|
|
9
18
|
let result = "";
|
|
10
19
|
for (let i = 0; i < length; i++) {
|
|
11
|
-
|
|
20
|
+
const randomIndex = getRandomInt(customValues.length);
|
|
21
|
+
result += customValues[randomIndex];
|
|
12
22
|
}
|
|
13
23
|
return result;
|
|
14
24
|
}
|
|
15
25
|
|
|
16
|
-
module.exports = idchunk;
|
|
26
|
+
module.exports = idchunk;
|