genkode 1.0.1 → 1.2.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.
@@ -0,0 +1,60 @@
1
+ name: CI/CD Pipeline
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - 'feature/**'
7
+ - master
8
+ pull_request:
9
+ branches:
10
+ - master
11
+
12
+ jobs:
13
+ build-and-test:
14
+ name: Build & Test
15
+ runs-on: ubuntu-latest
16
+
17
+ steps:
18
+ - name: Checkout code
19
+ uses: actions/checkout@v4
20
+
21
+ - name: Setup Node
22
+ uses: actions/setup-node@v4
23
+ with:
24
+ node-version: 22
25
+
26
+ - name: Install dependencies
27
+ run: npm install
28
+
29
+ - name: Build project
30
+ run: npm run build
31
+
32
+ - name: Run tests
33
+ run: npm test
34
+
35
+ publish:
36
+ name: Publish to npm
37
+ runs-on: ubuntu-latest
38
+ needs: build-and-test
39
+ if: github.ref == 'refs/heads/master'
40
+
41
+ steps:
42
+ - name: Checkout code
43
+ uses: actions/checkout@v4
44
+
45
+ - name: Setup Node
46
+ uses: actions/setup-node@v4
47
+ with:
48
+ node-version: 22
49
+ registry-url: https://registry.npmjs.org/
50
+
51
+ - name: Install dependencies
52
+ run: npm install
53
+
54
+ - name: Build project
55
+ run: npm run build
56
+
57
+ - name: Publish to npm
58
+ run: npm publish
59
+ env:
60
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/README.md CHANGED
@@ -1,11 +1,10 @@
1
- # genkode
1
+ # genkode - Random String & ID Generator for Node.js
2
2
 
3
3
  ![npm version](https://img.shields.io/npm/v/genkode)
4
4
  ![npm downloads](https://img.shields.io/npm/dw/genkode)
5
5
  ![license](https://img.shields.io/npm/l/genkode)
6
6
  ![types](https://img.shields.io/npm/types/genkode)
7
7
 
8
-
9
8
  A lightweight Node.js utility to generate random alphanumeric, alphabetic, or numeric strings.
10
9
  Fully compatible with TypeScript. No external dependencies.
11
10
 
@@ -21,61 +20,52 @@ npm install genkode
21
20
 
22
21
  ## 🚀 Usage
23
22
 
24
- ### 1. Generate Alphanumeric String
23
+ ### Generate using main API
25
24
 
26
25
  ```ts
27
- import { mumble } from 'genkode';
26
+ import { generateCode } from 'genkode';
28
27
 
29
- console.log(mumble(12));
28
+ generateCode({ length: 12 });
29
+ // Example: rqfvYxJRWfoP (alpha by default)
30
30
 
31
- // Example output:
32
- // vl6QLrr3xIBe
33
- ```
31
+ generateCode({ length: 12, type: "alphanumeric" });
32
+ // Example: aZ8kL2pQ9xW1
34
33
 
35
- ---
34
+ generateCode({ length: 12, type: "numeric" });
35
+ // Example: 362128126198
36
36
 
37
- ### 2. Generate Alphabet-only String
38
-
39
- ```ts
40
- import { mumblealpha } from 'genkode';
41
-
42
- console.log(mumblealpha(12));
43
-
44
- // Example output:
45
- // rqfvYxJRWfoP
37
+ // Cryptographically secure generation
38
+ generateCode({ length: 12, type: "alphanumeric", secure: true });
39
+ // Example: Tz3mW8qA1nXp
46
40
  ```
47
41
 
48
42
  ---
49
43
 
50
- ### 3. Generate Numeric String
51
-
52
- ```ts
53
- import { mumblenum } from 'genkode';
44
+ ## ⚙️ API
54
45
 
55
- console.log(mumblenum(12));
46
+ ### `generateCode(options)`
56
47
 
57
- // Example output:
58
- // 362128126198
59
- ```
48
+ | Property | Type | Default | Description |
49
+ |----------|------|---------|-------------|
50
+ | `length` | `number` | — | Required. Length of the generated string |
51
+ | `type` | `"alpha"` \| `"numeric"` \| `"alphanumeric"` | `"alpha"` | Character set to use |
52
+ | `secure` | `boolean` | `false` | Use `crypto.getRandomValues` for cryptographically secure output |
60
53
 
61
54
  ---
62
55
 
63
- ## ⚙️ API
56
+ ## Why genkode?
64
57
 
65
- | Function | Description |
66
- |----------------|--------------------------------------|
67
- | `mumble(n)` | Returns alphanumeric string |
68
- | `mumblealpha(n)`| Returns alphabet-only string |
69
- | `mumblenum(n)` | Returns numeric string |
70
-
71
- - `n` → Length of the required string
58
+ - Zero dependencies
59
+ - Fast and lightweight
60
+ - TypeScript support
61
+ - Simple and flexible API
62
+ - Multiple formats (alpha, numeric, alphanumeric)
63
+ - Optional cryptographically secure mode via Web Crypto API
72
64
 
73
65
  ---
74
66
 
75
67
  ## 🧩 Use Cases
76
68
 
77
- These generated strings can be used for:
78
-
79
69
  - Database IDs
80
70
  - Tokens / API keys
81
71
  - Session identifiers
@@ -84,23 +74,27 @@ These generated strings can be used for:
84
74
 
85
75
  ---
86
76
 
87
- ## 🔒 Notes
77
+ ## 🔒 Security
88
78
 
89
- - This package uses standard random generation (`Math.random`)
90
- - Suitable for general-purpose use
91
- - Not recommended for cryptographic/security-critical use cases
79
+ By default, `generateCode` uses `Math.random`, which is fast but **not cryptographically secure**.
80
+
81
+ Set `secure: true` to use `crypto.getRandomValues` (Web Crypto API), which produces unbiased, cryptographically secure output. Use this when generating tokens, API keys, or any value where predictability is a security concern.
82
+
83
+ ```ts
84
+ generateCode({ length: 32, type: "alphanumeric", secure: true });
85
+ ```
92
86
 
93
87
  ---
94
88
 
95
89
  ## 🔗 Related Package
96
90
 
97
- This package is also available as a JavaScript-only version:
91
+ JavaScript-only version:
98
92
 
99
- **node-mumble** (by the same developer)
93
+ node-mumble (by same developer)
100
94
  https://www.npmjs.com/package/node-mumble
101
95
 
102
96
  ---
103
97
 
104
98
  ## 📄 License
105
99
 
106
- MIT
100
+ MIT
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export declare function mumble(input: number): string;
2
- export declare function mumbleaplha(input: number): string;
3
- export declare function mumblenum(input: number): string;
1
+ import type { GenerateOptions } from "./kode.interface.js";
2
+ export declare function generateCode({ length, type, secure }: GenerateOptions): string;
4
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAWA,wBAAgB,MAAM,CAAC,KAAK,EAAC,MAAM,GAAE,MAAM,CAS1C;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAC,MAAM,GAAG,MAAM,CAWhD;AACD,wBAAgB,SAAS,CAAC,KAAK,EAAC,MAAM,GAAG,MAAM,CAW9C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAmD3D,wBAAgB,YAAY,CAAC,EAAC,MAAM,EAAC,IAAc,EAAC,MAAc,EAAC,EAAE,eAAe,GAAG,MAAM,CAS5F"}
package/dist/index.js CHANGED
@@ -1,42 +1,44 @@
1
- const arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
2
- function isLetter(s) {
3
- return s.match("^[a-zA-Z\(\)]+$");
4
- }
5
- function isNumeric(s) {
6
- return s.match("^[0-9\(\)]+$");
7
- }
8
- export function mumble(input) {
9
- let uniqString = "";
10
- const length = arr.length;
11
- while (input != 0) {
12
- const randomIndex = Math.floor(Math.random() * length);
13
- uniqString = uniqString + arr[randomIndex];
14
- input = input - 1;
1
+ const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2
+ const NUMERIC = "0123456789";
3
+ const ALPHANUM = ALPHA + NUMERIC;
4
+ function generate(length, charset) {
5
+ let result = "";
6
+ const max = charset.length;
7
+ while (length > 0) {
8
+ const index = Math.floor(Math.random() * max);
9
+ result += charset[index];
10
+ length--;
15
11
  }
16
- return uniqString;
12
+ return result;
17
13
  }
18
- export function mumbleaplha(input) {
19
- let uniqString = "";
20
- const length = arr.length;
21
- while (input != 0) {
22
- const randomIndex = Math.floor(Math.random() * length);
23
- if (isLetter(arr[randomIndex])) {
24
- uniqString = uniqString + arr[randomIndex];
25
- input = input - 1;
14
+ function generateSecure(length, charset) {
15
+ const max = charset.length;
16
+ const result = [];
17
+ const randomBuffer = new Uint8Array(1);
18
+ while (result.length < length) {
19
+ crypto.getRandomValues(randomBuffer);
20
+ const byte = randomBuffer[0];
21
+ if (byte < Math.floor(256 / max) * max) {
22
+ result.push(charset[byte % max]);
26
23
  }
27
24
  }
28
- return uniqString;
25
+ return result.join("");
29
26
  }
30
- export function mumblenum(input) {
31
- let uniqString = "";
32
- const length = arr.length;
33
- while (input != 0) {
34
- const randomIndex = Math.floor(Math.random() * length);
35
- if (isNumeric(arr[randomIndex])) {
36
- uniqString = uniqString + arr[randomIndex];
37
- input = input - 1;
38
- }
39
- }
40
- return uniqString;
27
+ function randomString(length) {
28
+ return generate(length, ALPHANUM);
29
+ }
30
+ function randomAlpha(length) {
31
+ return generate(length, ALPHA);
32
+ }
33
+ function randomNumeric(length) {
34
+ return generate(length, NUMERIC);
35
+ }
36
+ export function generateCode({ length, type = "alpha", secure = false }) {
37
+ const charset = type === "alpha" ? ALPHA :
38
+ type === "numeric" ? NUMERIC :
39
+ ALPHANUM;
40
+ return secure
41
+ ? generateSecure(length, charset)
42
+ : generate(length, charset);
41
43
  }
42
44
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,MAAM,GAAG,GAAG,CAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GAAG,EAAC,GA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AC,OAAO,CAAC,CAAC;YAC9B,QAAQ,CAAC;IAEX,OAAO,MAAM;QACX,CAAC,CAAC,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC;QACjC,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type CodeType = "alpha" | "numeric" | "alphanumeric";
2
+ export interface GenerateOptions {
3
+ length: number;
4
+ type?: CodeType;
5
+ secure?: boolean;
6
+ }
7
+ //# sourceMappingURL=kode.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kode.interface.d.ts","sourceRoot":"","sources":["../kode.interface.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,SAAS,GAAG,cAAc,CAAC;AAE5D,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=kode.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kode.interface.js","sourceRoot":"","sources":["../kode.interface.ts"],"names":[],"mappings":""}
package/index.ts CHANGED
@@ -1,46 +1,61 @@
1
- const arr = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"];
1
+ import type { GenerateOptions } from "./kode.interface.js";
2
2
 
3
- function isLetter(s:string)
4
- {
5
- return s.match("^[a-zA-Z\(\)]+$");
6
- }
7
- function isNumeric(s:string)
8
- {
9
- return s.match("^[0-9\(\)]+$");
10
- }
3
+ const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
4
+ const NUMERIC = "0123456789";
5
+ const ALPHANUM = ALPHA + NUMERIC;
6
+
7
+ function generate(length: number, charset: string): string {
8
+ let result = "";
9
+ const max = charset.length;
11
10
 
12
- export function mumble(input:number):string {
13
- let uniqString = "";
14
- const length = arr.length
15
- while (input!=0) {
16
- const randomIndex = Math.floor(Math.random()*length)
17
- uniqString = uniqString+arr[randomIndex];
18
- input = input - 1;
11
+ while (length > 0) {
12
+ const index = Math.floor(Math.random() * max);
13
+ result += charset[index]!;
14
+ length--;
19
15
  }
20
- return uniqString
16
+
17
+ return result;
21
18
  }
22
19
 
23
- export function mumbleaplha(input:number): string {
24
- let uniqString = "";
25
- const length = arr.length
26
- while (input!=0) {
27
- const randomIndex = Math.floor(Math.random()*length)
28
- if(isLetter(arr[randomIndex]!)){
29
- uniqString = uniqString+arr[randomIndex];
30
- input = input - 1;
20
+
21
+ function generateSecure(length: number, charset: string): string {
22
+ const max = charset.length;
23
+ const result: string[] = [];
24
+ const randomBuffer = new Uint8Array(1);
25
+
26
+ while (result.length < length) {
27
+ crypto.getRandomValues(randomBuffer);
28
+ const byte = randomBuffer[0]!;
29
+
30
+ if (byte < Math.floor(256 / max) * max) {
31
+ result.push(charset[byte % max]!);
31
32
  }
32
33
  }
33
- return uniqString
34
+
35
+ return result.join("");
34
36
  }
35
- export function mumblenum(input:number): string {
36
- let uniqString = "";
37
- const length = arr.length
38
- while (input!=0) {
39
- const randomIndex = Math.floor(Math.random()*length)
40
- if(isNumeric(arr[randomIndex]!)){
41
- uniqString = uniqString+arr[randomIndex];
42
- input = input - 1;
43
- }
44
- }
45
- return uniqString
37
+
38
+
39
+ function randomString(length: number): string {
40
+ return generate(length, ALPHANUM);
46
41
  }
42
+
43
+ function randomAlpha(length: number): string {
44
+ return generate(length, ALPHA);
45
+ }
46
+
47
+ function randomNumeric(length: number): string {
48
+ return generate(length, NUMERIC);
49
+ }
50
+
51
+
52
+ export function generateCode({length,type = "alpha",secure = false}: GenerateOptions): string {
53
+ const charset =
54
+ type === "alpha" ? ALPHA :
55
+ type === "numeric" ? NUMERIC :
56
+ ALPHANUM;
57
+
58
+ return secure
59
+ ? generateSecure(length, charset)
60
+ : generate(length, charset);
61
+ }
@@ -0,0 +1,7 @@
1
+ export type CodeType = "alpha" | "numeric" | "alphanumeric";
2
+
3
+ export interface GenerateOptions {
4
+ length: number;
5
+ type?: CodeType;
6
+ secure?: boolean;
7
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "genkode",
3
- "version": "1.0.1",
4
- "description": "A node package that creates a random unique characters string (Typescript Supported)",
3
+ "version": "1.2.0",
4
+ "description": "A lightweight Node.js utility to generate random alphanumeric, alphabetic, or numeric strings.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {
@@ -10,24 +10,43 @@
10
10
  },
11
11
  "type": "module",
12
12
  "scripts": {
13
+ "test": "node --test",
13
14
  "build": "tsc",
14
15
  "prepublishOnly": "npm run build"
15
16
  },
16
17
  "keywords": [
17
18
  "random",
18
- "string",
19
- "generator",
20
- "id",
19
+ "random-string",
20
+ "string-generator",
21
+ "random-id",
22
+ "id-generator",
21
23
  "token",
22
- "alphanumeric"
24
+ "token-generator",
25
+ "alphanumeric",
26
+ "alphabet",
27
+ "numeric",
28
+ "random-code",
29
+ "code-generator",
30
+ "unique-id",
31
+ "lightweight",
32
+ "zero-dependency",
33
+ "typescript",
34
+ "nodejs",
35
+ "utility"
23
36
  ],
24
37
  "author": "",
25
38
  "license": "MIT",
26
39
  "devDependencies": {
40
+ "@types/node": "^25.5.0",
41
+ "tsx": "^4.21.0",
42
+ "txc": "^0.0.1",
27
43
  "typescript": "^6.0.2"
28
44
  },
29
45
  "bugs": {
30
46
  "url": "https://github.com/sammitpal/genkode/issues"
31
47
  },
32
- "homepage": "https://github.com/sammitpal/genkode#readme"
48
+ "homepage": "https://github.com/sammitpal/genkode#readme",
49
+ "dependencies": {
50
+ "crypto": "^1.0.1"
51
+ }
33
52
  }
@@ -0,0 +1,64 @@
1
+ /// <reference types="node" />
2
+ import test from "node:test";
3
+ import assert from "node:assert";
4
+ import { generateCode } from "../dist/index.js";
5
+
6
+ // Helper regex
7
+ const alphaRegex = /^[A-Za-z]+$/;
8
+ const numericRegex = /^[0-9]+$/;
9
+ const alphanumRegex = /^[A-Za-z0-9]+$/;
10
+
11
+ test("should generate string of correct length", () => {
12
+ const result = generateCode({ length: 20 });
13
+ assert.strictEqual(result.length, 20);
14
+ });
15
+
16
+ test("should generate alphabet-only string", () => {
17
+ const result = generateCode({ length: 30, type: "alpha" });
18
+ assert.ok(alphaRegex.test(result));
19
+ });
20
+
21
+ test("should generate numeric-only string", () => {
22
+ const result = generateCode({ length: 30, type: "numeric" });
23
+ assert.ok(numericRegex.test(result));
24
+ });
25
+
26
+ test("should generate alphanumeric string", () => {
27
+ const result = generateCode({ length: 30, type: "alphanumeric" });
28
+ assert.ok(alphanumRegex.test(result));
29
+ });
30
+
31
+ test("should generate secure string (alpha)", () => {
32
+ const result = generateCode({ length: 25, type: "alpha", secure: true });
33
+ assert.strictEqual(result.length, 25);
34
+ assert.ok(alphaRegex.test(result));
35
+ });
36
+
37
+ test("should generate secure numeric string", () => {
38
+ const result = generateCode({ length: 25, type: "numeric", secure: true });
39
+ assert.strictEqual(result.length, 25);
40
+ assert.ok(numericRegex.test(result));
41
+ });
42
+
43
+ test("should generate secure alphanumeric string", () => {
44
+ const result = generateCode({ length: 25, type: "alphanumeric", secure: true });
45
+ assert.strictEqual(result.length, 25);
46
+ assert.ok(alphanumRegex.test(result));
47
+ });
48
+
49
+ test("should generate different values each time (non-deterministic)", () => {
50
+ const a = generateCode({ length: 20 });
51
+ const b = generateCode({ length: 20 });
52
+
53
+ assert.notStrictEqual(a, b);
54
+ });
55
+
56
+ test("should handle small length", () => {
57
+ const result = generateCode({ length: 1 });
58
+ assert.strictEqual(result.length, 1);
59
+ });
60
+
61
+ test("should handle large length", () => {
62
+ const result = generateCode({ length: 1000 });
63
+ assert.strictEqual(result.length, 1000);
64
+ });
package/tsconfig.json CHANGED
@@ -3,23 +3,29 @@
3
3
  "rootDir": ".",
4
4
  "outDir": "./dist",
5
5
 
6
- "module": "nodenext",
7
- "target": "esnext",
8
- "types": [],
6
+ "module": "NodeNext",
7
+ "moduleResolution": "NodeNext",
8
+ "target": "ES2022",
9
+
10
+ "types": ["node"],
9
11
 
10
12
  "sourceMap": true,
11
13
  "declaration": true,
12
14
  "declarationMap": true,
13
15
 
16
+ "strict": true,
14
17
  "noUncheckedIndexedAccess": true,
15
18
  "exactOptionalPropertyTypes": true,
16
19
 
17
-
18
- "strict": true,
19
20
  "verbatimModuleSyntax": true,
20
21
  "isolatedModules": true,
21
22
  "noUncheckedSideEffectImports": true,
22
23
  "moduleDetection": "force",
23
- "skipLibCheck": true,
24
- }
25
- }
24
+
25
+ "skipLibCheck": true
26
+ },
27
+
28
+ "include": ["index.ts"],
29
+
30
+ "exclude": ["test", "dist", "node_modules"]
31
+ }