Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit cbfdb29

Browse files
authored
feat(module): make id use nanoid
1 parent 2544457 commit cbfdb29

4 files changed

Lines changed: 35 additions & 19 deletions

File tree

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"bugs": {
2121
"url": "https://github.com/EasyScriptJS/EasyScript/issues"
2222
},
23-
"homepage": "https://easyscript.dev"
23+
"homepage": "https://easyscript.dev",
24+
"dependencies": {
25+
"nanoid": "3.3.4"
26+
}
2427
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const modules = require("./modules");
22

3-
module.exports = { modules };
3+
module.exports = modules;

src/modules/id.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
const { types } = require("../index").modules;
1+
const { customAlphabet } = require("nanoid");
22

3-
function generateId(length) {
4-
return parseInt(Math.ceil(Math.random() * 9999999999999999).toPrecision(length).toString().replace(".", ""));
5-
}
6-
7-
function id(input) {
8-
let length = input;
9-
10-
if(!input) throw new Error("No length specified");
11-
if(typeof input !== "number" && typeof input !== "string") throw new Error("Invalid type specified\nValid types: number, string");
12-
13-
if(typeof input === "string") {
14-
len = types.strToNum(input);
15-
}
3+
function id(length) {
4+
if(!length) throw new Error("No length specified");
5+
if(typeof length !== "number") throw new Error("Invalid type specified");
166

17-
if(length > 16) throw new Error("Length cannot be more than 16");
7+
if(length > 64) throw new Error("Length cannot be more than 64");
188

19-
const id = generateId(length);
9+
const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", length);
10+
const id = nanoid();
2011

2112
return id;
2213
}

0 commit comments

Comments
 (0)