@codedev168/string-pattern-gen 0.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 +19 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +79 -0
- package/package.json +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 codedev168
|
|
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
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# string-pattern-gen
|
|
2
|
+
|
|
3
|
+
Generates strings based on a pattern with placeholders for random characters (e.g., 'USER-####' → 'USER-AB12')
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @codedev168/string-pattern-gen
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { } from '@codedev168/string-pattern-gen';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a string based on a pattern with placeholders.
|
|
3
|
+
* Placeholders are in the format {TYPE:LENGTH}, where TYPE is one of 'DIGIT', 'LETTER', or 'ALPHANUM'.
|
|
4
|
+
* @param pattern - The pattern string containing placeholders.
|
|
5
|
+
* @returns The generated string with placeholders replaced.
|
|
6
|
+
* @throws {Error} If the pattern contains invalid placeholders.
|
|
7
|
+
* @note A placeholder with length 0 will result in an empty string.
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateString(pattern: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a string of random digits.
|
|
12
|
+
* @param length - The number of digits to generate (must be ≥ 0).
|
|
13
|
+
* @returns A string containing the specified number of random digits.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateDigits(length: number): string;
|
|
16
|
+
/**
|
|
17
|
+
* Generates a string of random uppercase letters.
|
|
18
|
+
* @param length - The number of letters to generate (must be ≥ 0).
|
|
19
|
+
* @returns A string containing the specified number of random uppercase letters.
|
|
20
|
+
*/
|
|
21
|
+
export declare function generateLetters(length: number): string;
|
|
22
|
+
/**
|
|
23
|
+
* Generates a string of random alphanumeric characters.
|
|
24
|
+
* @param length - The number of characters to generate (must be ≥ 0).
|
|
25
|
+
* @returns A string containing the specified number of random alphanumeric characters.
|
|
26
|
+
*/
|
|
27
|
+
export declare function generateAlphanum(length: number): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a string based on a pattern with placeholders.
|
|
3
|
+
* Placeholders are in the format {TYPE:LENGTH}, where TYPE is one of 'DIGIT', 'LETTER', or 'ALPHANUM'.
|
|
4
|
+
* @param pattern - The pattern string containing placeholders.
|
|
5
|
+
* @returns The generated string with placeholders replaced.
|
|
6
|
+
* @throws {Error} If the pattern contains invalid placeholders.
|
|
7
|
+
* @note A placeholder with length 0 will result in an empty string.
|
|
8
|
+
*/
|
|
9
|
+
export function generateString(pattern) {
|
|
10
|
+
const placeholderRegex = /{(\w+):(\d+)}/g;
|
|
11
|
+
/**
|
|
12
|
+
* Generates a cryptographically secure random integer between 0 and max (exclusive).
|
|
13
|
+
* @param max - The upper bound (exclusive) for the random number.
|
|
14
|
+
* @returns A cryptographically secure random integer.
|
|
15
|
+
*/
|
|
16
|
+
function generateSecureRandomInt(max) {
|
|
17
|
+
const array = new Uint32Array(1);
|
|
18
|
+
globalThis.crypto.getRandomValues(array);
|
|
19
|
+
return array[0] % max;
|
|
20
|
+
}
|
|
21
|
+
let result = pattern.replace(placeholderRegex, (match, type, lengthStr) => {
|
|
22
|
+
const length = parseInt(lengthStr, 10);
|
|
23
|
+
if (isNaN(length) || length < 0) {
|
|
24
|
+
throw new Error(`Invalid length in placeholder: ${match}`);
|
|
25
|
+
}
|
|
26
|
+
const validTypes = ['DIGIT', 'LETTER', 'ALPHANUM'];
|
|
27
|
+
if (!validTypes.includes(type)) {
|
|
28
|
+
throw new Error(`Invalid placeholder type: ${type}. Valid types are: ${validTypes.join(', ')}`);
|
|
29
|
+
}
|
|
30
|
+
let generated = '';
|
|
31
|
+
for (let i = 0; i < length; i++) {
|
|
32
|
+
switch (type) {
|
|
33
|
+
case 'DIGIT':
|
|
34
|
+
generated += generateSecureRandomInt(10);
|
|
35
|
+
break;
|
|
36
|
+
case 'LETTER':
|
|
37
|
+
generated += String.fromCharCode(65 + generateSecureRandomInt(26));
|
|
38
|
+
break;
|
|
39
|
+
case 'ALPHANUM':
|
|
40
|
+
const alphaNumSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
41
|
+
generated += alphaNumSet[generateSecureRandomInt(alphaNumSet.length)];
|
|
42
|
+
break;
|
|
43
|
+
default:
|
|
44
|
+
throw new Error(`Unknown placeholder type: ${type}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return generated;
|
|
48
|
+
});
|
|
49
|
+
// Validate that no invalid placeholders remain in the output
|
|
50
|
+
const invalidPlaceholderRegex = /{.*?}/;
|
|
51
|
+
if (invalidPlaceholderRegex.test(result)) {
|
|
52
|
+
throw new Error('Invalid placeholder format detected');
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generates a string of random digits.
|
|
58
|
+
* @param length - The number of digits to generate (must be ≥ 0).
|
|
59
|
+
* @returns A string containing the specified number of random digits.
|
|
60
|
+
*/
|
|
61
|
+
export function generateDigits(length) {
|
|
62
|
+
return generateString(`{DIGIT:${length}}`);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Generates a string of random uppercase letters.
|
|
66
|
+
* @param length - The number of letters to generate (must be ≥ 0).
|
|
67
|
+
* @returns A string containing the specified number of random uppercase letters.
|
|
68
|
+
*/
|
|
69
|
+
export function generateLetters(length) {
|
|
70
|
+
return generateString(`{LETTER:${length}}`);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Generates a string of random alphanumeric characters.
|
|
74
|
+
* @param length - The number of characters to generate (must be ≥ 0).
|
|
75
|
+
* @returns A string containing the specified number of random alphanumeric characters.
|
|
76
|
+
*/
|
|
77
|
+
export function generateAlphanum(length) {
|
|
78
|
+
return generateString(`{ALPHANUM:${length}}`);
|
|
79
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codedev168/string-pattern-gen",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generates strings based on a pattern with placeholders for random characters (e.g., 'USER-####' → 'USER-AB12')",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"string-pattern-gen",
|
|
15
|
+
"typescript",
|
|
16
|
+
"utility"
|
|
17
|
+
],
|
|
18
|
+
"author": "codedev168",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "^5.0.0",
|
|
22
|
+
"vitest": "^1.0.0"
|
|
23
|
+
}
|
|
24
|
+
}
|