@jrc03c/make-key 0.0.42 → 0.0.44
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/.prettierrc.json +10 -0
- package/build.mjs +24 -0
- package/dist/make-key.js +37 -0
- package/dist/make-key.min.js +1 -0
- package/eslint.config.js +19 -0
- package/moon.yml +29 -0
- package/package.json +10 -24
- package/pnpm-workspace.yaml +4 -0
- package/src/bin.mjs +19 -0
- package/src/iife.mjs +5 -0
- package/src/index.mjs +38 -0
- package/src/index.test.mjs +21 -0
- package/dist/index.d.mts +0 -2
- package/dist/index.mjs +0 -10
package/.prettierrc.json
ADDED
package/build.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { execSync } from "node:child_process"
|
|
2
|
+
|
|
3
|
+
function rebuild() {
|
|
4
|
+
console.log("-----")
|
|
5
|
+
console.log(`\nRebuilding... (${new Date().toLocaleString()})`)
|
|
6
|
+
|
|
7
|
+
try {
|
|
8
|
+
execSync(
|
|
9
|
+
`npx esbuild src/iife.mjs --bundle --outfile=dist/make-key.js`,
|
|
10
|
+
{ encoding: "utf8" },
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
execSync(
|
|
14
|
+
`npx esbuild src/iife.mjs --bundle --minify --outfile=dist/make-key.min.js`,
|
|
15
|
+
{ encoding: "utf8" },
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
console.log("\nDone! 🎉\n")
|
|
19
|
+
} catch (e) {
|
|
20
|
+
console.error(e)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
rebuild()
|
package/dist/make-key.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
// src/index.mjs
|
|
3
|
+
function makeKey(keyLength, charset, randomFn) {
|
|
4
|
+
if (typeof keyLength !== "number" || isNaN(keyLength)) {
|
|
5
|
+
throw new Error(
|
|
6
|
+
"The first value passed into the `makeKey` function must be a number!"
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
if (charset) {
|
|
10
|
+
if (charset instanceof Array) {
|
|
11
|
+
charset = charset.join("");
|
|
12
|
+
}
|
|
13
|
+
if (typeof charset !== "string") {
|
|
14
|
+
throw new Error(
|
|
15
|
+
"The second argument passed into the `makeKey` function must be a string or array of strings!"
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
randomFn = randomFn || Math.random;
|
|
20
|
+
if (typeof randomFn !== "function") {
|
|
21
|
+
throw new Error(
|
|
22
|
+
"The third argument passed into the `makeKey` function must be a function!"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
let out = "";
|
|
26
|
+
charset = charset || "abcdef1234567890";
|
|
27
|
+
for (let i = 0; i < keyLength; i++) {
|
|
28
|
+
out += charset[Math.floor(randomFn() * charset.length)];
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/iife.mjs
|
|
34
|
+
if (typeof globalThis !== "undefined") {
|
|
35
|
+
globalThis.makeKey = makeKey;
|
|
36
|
+
}
|
|
37
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{function f(t,e,o){if(typeof t!="number"||isNaN(t))throw new Error("The first value passed into the `makeKey` function must be a number!");if(e&&(e instanceof Array&&(e=e.join("")),typeof e!="string"))throw new Error("The second argument passed into the `makeKey` function must be a string or array of strings!");if(o=o||Math.random,typeof o!="function")throw new Error("The third argument passed into the `makeKey` function must be a function!");let i="";e=e||"abcdef1234567890";for(let n=0;n<t;n++)i+=e[Math.floor(o()*e.length)];return i}typeof globalThis<"u"&&(globalThis.makeKey=f);})();
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import globals from "globals"
|
|
2
|
+
import pluginJs from "@eslint/js"
|
|
3
|
+
|
|
4
|
+
export default [
|
|
5
|
+
{
|
|
6
|
+
languageOptions: {
|
|
7
|
+
globals: {
|
|
8
|
+
...globals.browser,
|
|
9
|
+
...globals.node,
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
rules: {
|
|
14
|
+
"no-empty": ["error", { allowEmptyCatch: true }],
|
|
15
|
+
"no-unused-vars": ["error", { caughtErrors: "none" }],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
pluginJs.configs.recommended,
|
|
19
|
+
]
|
package/moon.yml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
language: javascript
|
|
2
|
+
tasks:
|
|
3
|
+
build:
|
|
4
|
+
command:
|
|
5
|
+
- node
|
|
6
|
+
- build.mjs
|
|
7
|
+
platform: node
|
|
8
|
+
pub:
|
|
9
|
+
command:
|
|
10
|
+
- npm
|
|
11
|
+
- publish
|
|
12
|
+
- --access=public
|
|
13
|
+
deps:
|
|
14
|
+
- ~:pub-dep1
|
|
15
|
+
local: true
|
|
16
|
+
platform: node
|
|
17
|
+
pub-dep1:
|
|
18
|
+
command:
|
|
19
|
+
- npm
|
|
20
|
+
- version
|
|
21
|
+
- patch
|
|
22
|
+
- --force
|
|
23
|
+
local: true
|
|
24
|
+
platform: node
|
|
25
|
+
test:
|
|
26
|
+
command:
|
|
27
|
+
- npx
|
|
28
|
+
- fake-jest
|
|
29
|
+
platform: node
|
package/package.json
CHANGED
|
@@ -1,36 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"bin": {
|
|
3
|
-
"key": "
|
|
3
|
+
"key": "./src/bin.mjs"
|
|
4
4
|
},
|
|
5
5
|
"devDependencies": {
|
|
6
|
-
"esbuild": "^0.25.
|
|
7
|
-
"eslint": "^9.
|
|
8
|
-
"prettier": "^3.
|
|
9
|
-
"
|
|
10
|
-
"tsx": "^4.23.15",
|
|
11
|
-
"typescript": "^6.0.3",
|
|
12
|
-
"typescript-eslint": "^8.72.1",
|
|
13
|
-
"vitest": "^4.1.12"
|
|
6
|
+
"esbuild": "^0.25.4",
|
|
7
|
+
"eslint": "^9.38.0",
|
|
8
|
+
"prettier": "^3.6.2",
|
|
9
|
+
"vitest": "^4.1.10"
|
|
14
10
|
},
|
|
15
|
-
"exports": {
|
|
16
|
-
".": {
|
|
17
|
-
"import": "./dist/index.mjs",
|
|
18
|
-
"types": "./dist/index.d.mts"
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
"files": [
|
|
22
|
-
"dist"
|
|
23
|
-
],
|
|
24
11
|
"license": "ISC",
|
|
25
|
-
"main": "
|
|
26
|
-
"module": "
|
|
12
|
+
"main": "./src/index.mjs",
|
|
13
|
+
"module": "./src/index.mjs",
|
|
27
14
|
"name": "@jrc03c/make-key",
|
|
28
15
|
"scripts": {
|
|
29
|
-
"build": "
|
|
16
|
+
"build": "node build.mjs",
|
|
30
17
|
"pub": "npm version patch --force && npm publish --access=public",
|
|
31
|
-
"test": "vitest run"
|
|
32
|
-
"watch": "tsdown --watch"
|
|
18
|
+
"test": "npx vitest run"
|
|
33
19
|
},
|
|
34
20
|
"type": "module",
|
|
35
|
-
"version": "0.0.
|
|
21
|
+
"version": "0.0.44"
|
|
36
22
|
}
|
package/src/bin.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { exec } from "node:child_process"
|
|
3
|
+
import { makeKey } from "./index.mjs"
|
|
4
|
+
|
|
5
|
+
const args = process.argv.slice(2)
|
|
6
|
+
|
|
7
|
+
if (args.length === 0) {
|
|
8
|
+
console.log("SYNTAX: key <length> <charset>")
|
|
9
|
+
process.exit(0)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const keyLength = parseInt(args[0])
|
|
13
|
+
const charset = args[1]
|
|
14
|
+
const key = makeKey(keyLength, charset)
|
|
15
|
+
console.log(key)
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
exec(`echo -n "${key}" | xsel -b`, () => {})
|
|
19
|
+
} catch (e) {}
|
package/src/iife.mjs
ADDED
package/src/index.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
function makeKey(keyLength, charset, randomFn) {
|
|
2
|
+
if (typeof keyLength !== "number" || isNaN(keyLength)) {
|
|
3
|
+
throw new Error(
|
|
4
|
+
"The first value passed into the `makeKey` function must be a number!",
|
|
5
|
+
)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (charset) {
|
|
9
|
+
if (charset instanceof Array) {
|
|
10
|
+
charset = charset.join("")
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (typeof charset !== "string") {
|
|
14
|
+
throw new Error(
|
|
15
|
+
"The second argument passed into the `makeKey` function must be a string or array of strings!",
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
randomFn = randomFn || Math.random
|
|
21
|
+
|
|
22
|
+
if (typeof randomFn !== "function") {
|
|
23
|
+
throw new Error(
|
|
24
|
+
"The third argument passed into the `makeKey` function must be a function!",
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let out = ""
|
|
29
|
+
charset = charset || "abcdef1234567890"
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < keyLength; i++) {
|
|
32
|
+
out += charset[Math.floor(randomFn() * charset.length)]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return out
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { makeKey }
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
import { makeKey } from "./index.mjs"
|
|
3
|
+
|
|
4
|
+
test("tests that random strings can be generated correctly", () => {
|
|
5
|
+
const a = makeKey(123)
|
|
6
|
+
expect(a.length).toBe(123)
|
|
7
|
+
|
|
8
|
+
const b = makeKey(123)
|
|
9
|
+
const c = makeKey(123)
|
|
10
|
+
expect(b).not.toBe(c)
|
|
11
|
+
|
|
12
|
+
const charset = "Hello, world!"
|
|
13
|
+
const d = makeKey(123, charset)
|
|
14
|
+
|
|
15
|
+
d.split("").forEach(char => {
|
|
16
|
+
expect(charset.includes(char)).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const e = makeKey(123, charset)
|
|
20
|
+
expect(e).not.toBe(d)
|
|
21
|
+
})
|
package/dist/index.d.mts
DELETED
package/dist/index.mjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
function makeKey(length, charset, randomFn) {
|
|
3
|
-
if (charset instanceof Array) charset = charset.join("");
|
|
4
|
-
randomFn = randomFn || Math.random;
|
|
5
|
-
let out = "";
|
|
6
|
-
charset = charset || "abcdef1234567890";
|
|
7
|
-
for (let i = 0; i < length; i++) out += charset[Math.floor(randomFn() * charset.length)];
|
|
8
|
-
return out;
|
|
9
|
-
}
|
|
10
|
-
export { makeKey };
|