From 73c89c3755e4b7943a9e869b64498bed09b8d240 Mon Sep 17 00:00:00 2001 From: Dagmawi zewdu Date: Sat, 28 Oct 2023 11:04:34 +0300 Subject: [PATCH 01/12] Update issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 38 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/custom.md | 10 ++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 ++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/custom.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..dd84ea7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md new file mode 100644 index 0000000..48d5f81 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/custom.md @@ -0,0 +1,10 @@ +--- +name: Custom issue template +about: Describe this issue template's purpose here. +title: '' +labels: '' +assignees: '' + +--- + + diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. From b44e82b3c6aca64b752b22f3499cbb143a573c75 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 12:47:41 +0300 Subject: [PATCH 02/12] Fix all code formatting issues --- README.md | 221 ++++++++++++++++++++++++-------------------- bin/randomString | 113 ++++++++++------------ src/check.option.ts | 4 +- src/emoji.ts | 2 +- src/index.ts | 10 +- src/types.ts | 10 +- test.js | 163 ++++++++++++++++++-------------- try.js | 17 ++-- 8 files changed, 286 insertions(+), 254 deletions(-) diff --git a/README.md b/README.md index aa21e29..3d5bd14 100644 --- a/README.md +++ b/README.md @@ -2,28 +2,29 @@ There are lots of scenarios which one can use our Random string generator the most known or the best scenario is for - 1.Password generation - - 2.Token generation. - - 3.Testing - - 4.id generation - +1.Password generation + +2.Token generation. + +3.Testing + +4.id generation + This package generate random string based on the given parameters. ## Getting Started -To start to use this on local machine build then first Test the program using the test.js +To start to use this on local machine build then first Test the program using the test.js Install node modules -* npm - +- npm + ``` npm install randomized-string ``` -* yarn + +- yarn ``` yarn add randomized-string @@ -33,121 +34,134 @@ yarn add randomized-string ## Usage For generating a random string you call `generate()` function.By default it generate 16 character string. -``` js -const randomString = require("randomized-string"); -randomString.generate(); // xabgtl3yb1Ac0MrQ +```js +const randomString = require('randomized-string'); +randomString.generate(); // xabgtl3yb1Ac0MrQ ``` -or you can use `import` statment + +or you can use `import` statment + ```js -import randomString from 'randomized-string' +import randomString from 'randomized-string'; ``` + If you want some length to random string pass a number to the function. ```js - randomString.generate(6); // lAo3Bi +randomString.generate(6); // lAo3Bi ``` To be more specific you can pass an object for more flexibility.This are the listed options you can provide -``` ts -type options={ - charset?:"alphanumeric"|"number"|"alphabet"|"hex"|"binary"|"octal", - lowerCaseOnly?:boolean, //only lowercase letter - range?:string, // user provided charset - length: number, //length of the character if not provided generates 16 characters by default - upperCaseOnly?:boolean, // only uppercase letter - insertSymbol?:boolean, // insert symbols to given charset - symbolsOnly:boolean, //only symbols - prefix: string, // add before string - suffix: string // add after the string -} +```ts +type options = { + charset?: 'alphanumeric' | 'number' | 'alphabet' | 'hex' | 'binary' | 'octal'; + lowerCaseOnly?: boolean; //only lowercase letter + range?: string; // user provided charset + length: number; //length of the character if not provided generates 16 characters by default + upperCaseOnly?: boolean; // only uppercase letter + insertSymbol?: boolean; // insert symbols to given charset + symbolsOnly: boolean; //only symbols + prefix: string; // add before string + suffix: string; // add after the string +}; ``` + ### passing down the options - Generate string only in the given string range +Generate string only in the given string range ```js randomeString.generate({ - range:'abc123', - length:6 - }); + range: 'abc123', + length: 6, +}); // a3cb21 ``` + You can set also set type of string + ```js randomString.generate({ -charset:'number', -length:10 -}) //2342612198 + charset: 'number', + length: 10, +}); //2342612198 randomString.generate({ -charset:'alphabet', -length:10 -}) //SoWhopDFTb + charset: 'alphabet', + length: 10, +}); //SoWhopDFTb randomString.generate({ -charset:'hex', -length:6 -})// C0faDB + charset: 'hex', + length: 6, +}); // C0faDB ``` + You can insert prefix `prefix` or `suffix` characters. Note that the length of the prefix or suffix is not counted with the length of the random string. ```js randomString.generate({ -length:10, -prefix: "pre-" -}) //pre-GaKdvH8Bro + length: 10, + prefix: 'pre-', +}); //pre-GaKdvH8Bro randomString.generate({ -length:10, -suffix: "-suff" -}) //0YcCeMISpE-suff + length: 10, + suffix: '-suff', +}); //0YcCeMISpE-suff +``` + +You can also insert symbols if you pass set the `insertSymbol` option to true. But it doesn't work for binary or octal or hex charset option. -``` -You can also insert symbols if you pass set the `insertSymbol` option to true. But it doesn't work for binary or octal or hex charset option. -``` js +```js randomString.generate({ -insertSymbol:true -}) + insertSymbol: true, +}); //bd@MK8Λ†IvpGVoorO{FJkf]iMz,{1+-8g ``` + You can pass the option `upperCaseOnly` to make the string only capital and `lowerCaseOnly` to make the string lowercase ```js randomString.generate({ - charset:'alphabet', - upperCaseOnly:true - }) - //ODEISDGQXUGGOHHG + charset: 'alphabet', + upperCaseOnly: true, +}); +//ODEISDGQXUGGOHHG randomString.generate({ - lowerCaseOnly:true - }) - //fnzkamf0svos4yso + lowerCaseOnly: true, +}); +//fnzkamf0svos4yso ``` + You can get symbols only if you set option ` symbolsOnly:true,` + ```js randomString.generate({ - symbolsOnly:true, - length:10 -}) - // ?[]}$'&,{] + symbolsOnly: true, + length: 10, +}); +// ?[]}$'&,{] ``` + For fun you can also generate random emojis using `generateUnicodeEmoji(length)` ```js -randomString.generateUnicodeEmoji(1) //🍍 +randomString.generateUnicodeEmoji(1); //🍍 ``` + ## Command Line Usage -First the package should be installed globally +First the package should be installed globally ```js $ npm install randomized-string -g $ yarn add randomized-string -$ randomized-string +$ randomized-string // Urp0YDaIHWn7YCCF @@ -161,75 +175,81 @@ $ randomized-string charset=alphanumeric insertSymbol=true length=8 $ randomized-string prefix=pre- //pre-KOyWstwcpA6sLaH3 -$ randomized-string generateUnicodeEmoji length=5 +$ randomized-string generateUnicodeEmoji length=5 // βΊοΈπŸ’€πŸ‘‡πŸ˜°πŸ—³οΈ ``` + ## API randomString `generate(option|length)` - * option: +- option: - * length: define the length of the output default 16 [Optional] +- length: define the length of the output default 16 [Optional] - * insert Symbol: to use Symbols when generatig random string output [Optional] +- insert Symbol: to use Symbols when generatig random string output [Optional] - * charset: +- charset: - - Alphabetic [a-zA-Z] + - Alphabetic [a-zA-Z] - - alphanumeric[0-9a-zAZ] + - alphanumeric[0-9a-zAZ] - - numeric [0-9] + - numeric [0-9] - - Binary [01] + - Binary [01] - - Octal [0-7] + - Octal [0-7] - - Hexa [0-9a-fA-F] + - Hexa [0-9a-fA-F] - * capitalization: +- capitalization: - - upperCaseOnly : Only Capital Leters are used to generate the output [Optional] + - upperCaseOnly : Only Capital Leters are used to generate the output [Optional] - - lowerCaseOnly : Only Small Leters are used to generate the output [Optional] + - lowerCaseOnly : Only Small Leters are used to generate the output [Optional] - * symbols only: +- symbols only: - - used to generate symbols only + - used to generate symbols only - * prefix: - - Adds a character before the random string - - * suffix: - - Adds character after the random string +- prefix: + + - Adds a character before the random string + +- suffix: + - Adds character after the random string `generateUnicodeEmoji(length)` - - generates random emojis + +- generates random emojis + ## Tests -- npm install + +- npm install - npm run test See the [open issues](https://github.com/Dagic-zewdu/random-string/issues) for a full list of proposed features (and known issues).

(back to top)

- ## support us -[β˜• Buy me a coffee](https://www.buymeacoffee.com/dagizewdudc) + +[β˜• Buy me a coffee](https://www.buymeacoffee.com/dagizewdudc) - You can also give as star to our repo. + ## Authors ### Author 1 πŸ‘€ **Dagmawi Zewdu** - - GitHub: [@Dagic-zewdu](https://github.com/Dagic-zewdu) +- GitHub: [@Dagic-zewdu](https://github.com/Dagic-zewdu) - LinkedIn: [Dagi-Zewdu](https://www.linkedin.com/in/dagic-zewdu/) ### Author 2 @@ -240,6 +260,7 @@ See the [open issues](https://github.com/Dagic-zewdu/random-string/issues) for a - LinkedIn: [sentayhu-berhanu](https://www.linkedin.com/in/sentayhu-berhanu-6376579a/) + ## Contribution If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". @@ -253,19 +274,17 @@ Don't forget to give the project a star! Thanks again!

(back to top)

- - ### Built With -* [TypeScript](https://www.typescriptlang.org/) -* [JavaScript](https://www.javascript.com/) - +- [TypeScript](https://www.typescriptlang.org/) +- [JavaScript](https://www.javascript.com/) + ## Contact -Dagmawi Zewdu - [@Dagi](https://twitter.com/dagmawi-zewdu) - dagi.zewdu.dz@gmail.com - +Dagmawi Zewdu - [@Dagi](https://twitter.com/dagmawi-zewdu) - dagi.zewdu.dz@gmail.com + Sentayhu Berhanu- [@Sentayhu](https://twitter.com/VoltageBerhanu) - sentayhuberhanu19@gmail.com Project Link: [https://github.com/Dagic-zewdu/random-string](https://github.com/Dagic-zewdu/random-string) diff --git a/bin/randomString b/bin/randomString index cfea916..6227009 100755 --- a/bin/randomString +++ b/bin/randomString @@ -1,75 +1,58 @@ #!/usr/bin/env node -const randomString = require(".."); -let Display=true; +const randomString = require('..'); +let Display = true; const options = {}; const emojilen = {}; for (let i = 2, j = process.argv.length; i < j; i++) { - let arg = process.argv[i]; - - if (arg.search('length') >= 0) { - let length = arg.split('=')[1]; - - if (length) - options.length = parseInt(length); - } - else if (arg.search('charset') >= 0) { - let charset = arg.split('=')[1]; - - if (charset) - options.charset = charset; - } - else if (arg.search('lowerCaseOnly') >= 0) { - let lowerCaseOnly = arg.split('=')[1]; - - if (lowerCaseOnly) - options.lowerCaseOnly = lowerCaseOnly; - } - else if (arg.search('insertSymbol') >= 0) { - let insertSymbol = arg.split('=')[1]; - - if ( insertSymbol) - options.insertSymbol = insertSymbol; - } - else if (arg.search('upperCaseOnly') >= 0) { - let upperCaseOnly = arg.split('=')[1]; - - if ( upperCaseOnly) - options.upperCaseOnly = upperCaseOnly; - } - else if (arg.search('symbolsOnly') >= 0) { - let symbolsOnly = arg.split('=')[1]; - - if (symbolsOnly) - options.symbolsOnly = symbolsOnly; - } - else if (arg.search('prefix') >= 0) { - let prefix = arg.split('=')[1]; - - if (prefix) - options.prefix = prefix; - } - else if (arg.search('suffix') >= 0) { - let suffix = arg.split('=')[1]; - - if (suffix) - options.suffix = suffix; - } - else if (arg.search('generateUnicodeEmoji') >= 0) { - Display =false; - let generateUnicodeEmoji = arg.split('=')[1]; + let arg = process.argv[i]; - if (generateUnicodeEmoji) - emojilen.generateUnicodeEmoji = generateUnicodeEmoji; - } + if (arg.search('length') >= 0) { + let length = arg.split('=')[1]; + + if (length) options.length = parseInt(length); + } else if (arg.search('charset') >= 0) { + let charset = arg.split('=')[1]; + + if (charset) options.charset = charset; + } else if (arg.search('lowerCaseOnly') >= 0) { + let lowerCaseOnly = arg.split('=')[1]; + + if (lowerCaseOnly) options.lowerCaseOnly = lowerCaseOnly; + } else if (arg.search('insertSymbol') >= 0) { + let insertSymbol = arg.split('=')[1]; + + if (insertSymbol) options.insertSymbol = insertSymbol; + } else if (arg.search('upperCaseOnly') >= 0) { + let upperCaseOnly = arg.split('=')[1]; + + if (upperCaseOnly) options.upperCaseOnly = upperCaseOnly; + } else if (arg.search('symbolsOnly') >= 0) { + let symbolsOnly = arg.split('=')[1]; + + if (symbolsOnly) options.symbolsOnly = symbolsOnly; + } else if (arg.search('prefix') >= 0) { + let prefix = arg.split('=')[1]; + + if (prefix) options.prefix = prefix; + } else if (arg.search('suffix') >= 0) { + let suffix = arg.split('=')[1]; + + if (suffix) options.suffix = suffix; + } else if (arg.search('generateUnicodeEmoji') >= 0) { + Display = false; + let generateUnicodeEmoji = arg.split('=')[1]; + + if (generateUnicodeEmoji) + emojilen.generateUnicodeEmoji = generateUnicodeEmoji; + } } let result = randomString.generate(options); -let emoji=randomString.generateUnicodeEmoji(options.length); -if(Display===true){ -console.log('\x1b[32m',result); -} -else{ -console.log('\x1b[34m',emoji); +let emoji = randomString.generateUnicodeEmoji(options.length); +if (Display === true) { + console.log('\x1b[32m', result); +} else { + console.log('\x1b[34m', emoji); } - console.log('\x1b[0m'); //reset +console.log('\x1b[0m'); //reset diff --git a/src/check.option.ts b/src/check.option.ts index 9a2bc0c..a20a936 100644 --- a/src/check.option.ts +++ b/src/check.option.ts @@ -23,7 +23,9 @@ export const checkOptions = (result: string, option: options) => { : alphabetNumericSymbol; const charactersLength = characters.length; for (let i = 0; i < option.length; i++) { - newResult += characters.charAt(Math.floor(Math.random() * charactersLength)); + newResult += characters.charAt( + Math.floor(Math.random() * charactersLength) + ); } } if (option.symbolsOnly) { diff --git a/src/emoji.ts b/src/emoji.ts index 0a12983..363a334 100644 --- a/src/emoji.ts +++ b/src/emoji.ts @@ -1768,7 +1768,7 @@ export const emojis = [ '🧜 ', '🧜 ', '🧝 ', - '🧝 ', + '🧝 ', '🧝 ', '🧝 ', '🧝 ', diff --git a/src/index.ts b/src/index.ts index a8efa7c..f51fdfd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,16 +8,16 @@ import crypto from 'crypto'; * @returns - random string generated */ export const generate = (option: number | options) => { - const compute = (type: string ,optlen: number, length: number) => { + const compute = (type: string, optlen: number, length: number) => { let result = ''; const charactersLength = type.length; for (let i = 0; i < length; i += 1) { const randomBytes = crypto.randomBytes(1); - const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); + const randomIndex = Math.floor((randomBytes[0] / 256) * charactersLength); result += type.charAt(randomIndex); } return result; - } + }; let result: string | number = ''; if (typeof option === 'number' || !option) { let opt = option ? option : 16; @@ -34,7 +34,7 @@ export const generate = (option: number | options) => { result = compute(characters, charactersLength, option.length); result = checkOptions(result, option); } else if (option.charset === 'number') { - const max = Math.pow(10, option.length) - 1; + const max = Math.pow(10, option.length) - 1; const randomBytes = crypto.randomBytes(Math.ceil(option.length / 2)); const randomValue = parseInt(randomBytes.toString('hex'), 16) % max; // Converting the random bytes to a number within the desired range result = randomValue.toString(); @@ -67,7 +67,7 @@ export const generateUnicodeEmoji = (length: number) => { const charactersLength = emojis.length; for (let i = 0; i < len; i += 1) { const randomBytes = crypto.randomBytes(1); - const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); + const randomIndex = Math.floor((randomBytes[0] / 256) * charactersLength); result += emojis[randomIndex].trim(); } return result; diff --git a/src/types.ts b/src/types.ts index d60b98c..426dc79 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,13 +16,15 @@ export type options = { export const hex = '0123456789ABCDEFabcdef'; export const symbols = '[-!$%^&*()_+|~=`{}[]:";<>?,./]' + "'"; export const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; -export const alphanumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; +export const alphanumeric = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; export const number = '0123456789'; export const alphabetSymbol = - 'IJ[A-B!C$Dh%E^FGH&KfgL*f(zMNy)Q_iOfPe+xdRc|zb~S=aTw`{Y}X[]Z\vrW:j";kt?mVn,o.up/]Uq' + "'"; + 'IJ[A-B!C$Dh%E^FGH&KfgL*f(zMNy)Q_iOfPe+xdRc|zb~S=aTw`{Y}X[]Z\vrW:j";kt?mVn,o.up/]Uq' + + "'"; export const numberSymbol = '[-!$%9^&*()_8+|~6=7`5{4}[]3:"2;<>?0,.1/]' + "'"; export const alphabetNumericSymbol = 'IJ[0A-B!1C$3Dh%E^2FGH&4Kf5gL*6f(zMN7y)Q_iOfPe+xdRc|zb~S=aTw`{Y}X[]Z\vrW:j";kt?9mVn,o.u7p/]Uq' + "'"; - export const binary = '01'; - export const octal = '01234567'; +export const binary = '01'; +export const octal = '01234567'; diff --git a/test.js b/test.js index fb5f108..06066bf 100644 --- a/test.js +++ b/test.js @@ -1,74 +1,101 @@ -const randomString =require('./lib/index') +const randomString = require('./lib/index'); describe('testing using provide length', () => { - test('the string should return alphanumeric values',()=>{ - let random=randomString.generate(9) - expect(random.match(/^[a-zA-Z0-9]+$/)).toBeTruthy() - }) - test('length should be 16',()=>{ - let random=randomString.generate() - expect(random.length===16).toBeTruthy() - }) + test('the string should return alphanumeric values', () => { + let random = randomString.generate(9); + expect(random.match(/^[a-zA-Z0-9]+$/)).toBeTruthy(); + }); + test('length should be 16', () => { + let random = randomString.generate(); + expect(random.length === 16).toBeTruthy(); + }); }); -describe('testing with given options',()=>{ - test('should generate with the given charset',()=>{ - let random=randomString.generate({range:'2846',length:6}) - - expect(random.match(random,"^\d*[02468]$")).toBeTruthy() - }) - test('should generate with the given charset',()=>{ - let random=randomString.generate({range:'abcdef',length:10}) - expect(random.match(random,/Λ†[a-f]/)).toBeTruthy() - - }) - test('should be a number only',()=>{ - let random=randomString.generate({charset:'number',length:6}) - expect(random.match(/^[0-9]+$/)).toBeTruthy() +describe('testing with given options', () => { + test('should generate with the given charset', () => { + let random = randomString.generate({ range: '2846', length: 6 }); - }) - test('should be an alphabet only',()=>{ - let random=randomString.generate({charset:'alphabet',length:6}) - expect(random.match(/^[a-zA-Z]+$/)).toBeTruthy() - }) - test('should be Binary only',()=>{ - let random=randomString.generate({charset:'binary',length:6}) - expect(random.match(/[0-1]+$/)).toBeTruthy() - }) - test('should be octal only',()=>{ - let random=randomString.generate({charset:'octal',length:6}) - expect(random.match(/[0-7]+$/)).toBeTruthy() - }) - test('should be hexa only',()=>{ - let random=randomString.generate({charset:'hex',length:6}) - expect(random.match(/[0-9a-fA-F]+$/)).toBeTruthy() - }) - test('should be alphabet with small Letters only',()=>{ - let random=randomString.generate({charset:'alphabet',length:6,lowerCaseOnly:true}) - expect(random.match(/[0-9a-z]+$/)).toBeTruthy() - }) - test('should be alphabet with capital Letters only',()=>{ - let random=randomString.generate({charset:'alphabet',length:6,upperCaseOnly:true}) - expect(random.match(/[0-9A-Z]+$/)).toBeTruthy() - }) - - test('should be symbols with number only',()=>{ - let random=randomString.generate({insertSymbol:true,charset:"number"}) - console.log("Symbol and number : "+random); - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/+0-9]+$/)).toBeTruthy() - }) - test('should be symbols with alphanumeric small letters only',()=>{ + expect(random.match(random, '^d*[02468]$')).toBeTruthy(); + }); + test('should generate with the given charset', () => { + let random = randomString.generate({ range: 'abcdef', length: 10 }); + expect(random.match(random, /Λ†[a-f]/)).toBeTruthy(); + }); + test('should be a number only', () => { + let random = randomString.generate({ charset: 'number', length: 6 }); + expect(random.match(/^[0-9]+$/)).toBeTruthy(); + }); + test('should be an alphabet only', () => { + let random = randomString.generate({ charset: 'alphabet', length: 6 }); + expect(random.match(/^[a-zA-Z]+$/)).toBeTruthy(); + }); + test('should be Binary only', () => { + let random = randomString.generate({ charset: 'binary', length: 6 }); + expect(random.match(/[0-1]+$/)).toBeTruthy(); + }); + test('should be octal only', () => { + let random = randomString.generate({ charset: 'octal', length: 6 }); + expect(random.match(/[0-7]+$/)).toBeTruthy(); + }); + test('should be hexa only', () => { + let random = randomString.generate({ charset: 'hex', length: 6 }); + expect(random.match(/[0-9a-fA-F]+$/)).toBeTruthy(); + }); + test('should be alphabet with small Letters only', () => { + let random = randomString.generate({ + charset: 'alphabet', + length: 6, + lowerCaseOnly: true, + }); + expect(random.match(/[0-9a-z]+$/)).toBeTruthy(); + }); + test('should be alphabet with capital Letters only', () => { + let random = randomString.generate({ + charset: 'alphabet', + length: 6, + upperCaseOnly: true, + }); + expect(random.match(/[0-9A-Z]+$/)).toBeTruthy(); + }); - let random=randomString.generate({insertSymbol:true,lowerCaseOnly:true}) - console.log(random) - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9a-z]+$/)).toBeTruthy() - }) - test('should be symbols with alphanumeric capital letters only',()=>{ - let random=randomString.generate({insertSymbol:true,upperCaseOnly:true,length:5}) - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9A-Z]/)).toBeTruthy() - }) - test('should be symbols with given provided number',()=>{ - let random=randomString.generate({insertSymbol:true,range:"123abc",length:6}) - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/1-3a-c]/)).toBeTruthy() - }) -}) \ No newline at end of file + test('should be symbols with number only', () => { + let random = randomString.generate({ + insertSymbol: true, + charset: 'number', + }); + console.log('Symbol and number : ' + random); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/+0-9]+$/) + ).toBeTruthy(); + }); + test('should be symbols with alphanumeric small letters only', () => { + let random = randomString.generate({ + insertSymbol: true, + lowerCaseOnly: true, + }); + console.log(random); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9a-z]+$/) + ).toBeTruthy(); + }); + test('should be symbols with alphanumeric capital letters only', () => { + let random = randomString.generate({ + insertSymbol: true, + upperCaseOnly: true, + length: 5, + }); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9A-Z]/) + ).toBeTruthy(); + }); + test('should be symbols with given provided number', () => { + let random = randomString.generate({ + insertSymbol: true, + range: '123abc', + length: 6, + }); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/1-3a-c]/) + ).toBeTruthy(); + }); +}); diff --git a/try.js b/try.js index aed6912..947d6a9 100644 --- a/try.js +++ b/try.js @@ -1,11 +1,10 @@ -const randomString =require('./lib/index') +const randomString = require('./lib/index'); +let str = randomString.generate({ + length: 10, + range: 'Feven', + insertSymbol: true, + insertSymbol: true, +}); -let str=randomString.generate({ -length:10, -range: "Feven", -insertSymbol:true, -insertSymbol:true, -}) - -console.log(str) +console.log(str); From da30906711fd4d42c6cc0d517498bdf852b76a9c Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 12:55:36 +0300 Subject: [PATCH 03/12] Create Prettier config file --- .prettierrc | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1dc7164 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true +} \ No newline at end of file From b64f7fc53dcbd9ed48f46c80896f803fdff69c0b Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 19:24:54 +0300 Subject: [PATCH 04/12] Get length from option with a default value --- src/index.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index a8efa7c..935a13d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import crypto from 'crypto'; * @param option - could be a number which is the length of the string to be generated || could be an option { * @returns - random string generated */ -export const generate = (option: number | options) => { +export const generate = (option: number | options): string => { const compute = (type: string ,optlen: number, length: number) => { let result = ''; const charactersLength = type.length; @@ -25,35 +25,33 @@ export const generate = (option: number | options) => { const charactersLength = characters.length; result = compute(characters, charactersLength, opt); } else if (typeof option === 'object') { - if (!option.length) { - option.length = 16; - } + const { length = 16 } = option; if (option.range) { const characters = option.range; const charactersLength = characters.toString().length; - result = compute(characters, charactersLength, option.length); + result = compute(characters, charactersLength, length); result = checkOptions(result, option); } else if (option.charset === 'number') { - const max = Math.pow(10, option.length) - 1; - const randomBytes = crypto.randomBytes(Math.ceil(option.length / 2)); + const max = Math.pow(10, length) - 1; + const randomBytes = crypto.randomBytes(Math.ceil(length / 2)); const randomValue = parseInt(randomBytes.toString('hex'), 16) % max; // Converting the random bytes to a number within the desired range result = randomValue.toString(); result = checkOptions(result, option); } else if (option.charset === 'binary') { - result = compute(binary, 2, option.length); + result = compute(binary, 2, length); } else if (option.charset === 'octal') { - result = compute(octal, 8, option.length); + result = compute(octal, 8, length); } else if (option.charset === 'hex') { const charactersLength = hex.length; - result = compute(hex, charactersLength, option.length); + result = compute(hex, charactersLength, length); result = checkOptions(result, option); } else if (option.charset === 'alphabet') { const charactersLength = alphabet.length; - result = compute(alphabet, charactersLength, option.length); + result = compute(alphabet, charactersLength, length); result = checkOptions(result, option); } else { const charactersLength = alphanumeric.length; - result = compute(alphanumeric, charactersLength, option.length); + result = compute(alphanumeric, charactersLength, length); result = checkOptions(result, option); } } From f19ccd9a112d806b2c99de52b7afd827874a608e Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 20:17:57 +0300 Subject: [PATCH 05/12] Destructure option object to get range as characters --- src/index.ts | 5 ++--- test.js | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 935a13d..a1db5a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,9 +25,8 @@ export const generate = (option: number | options): string => { const charactersLength = characters.length; result = compute(characters, charactersLength, opt); } else if (typeof option === 'object') { - const { length = 16 } = option; - if (option.range) { - const characters = option.range; + const { length = 16, range: characters } = option; + if (characters) { const charactersLength = characters.toString().length; result = compute(characters, charactersLength, length); result = checkOptions(result, option); diff --git a/test.js b/test.js index fb5f108..3a0c71a 100644 --- a/test.js +++ b/test.js @@ -15,11 +15,11 @@ describe('testing with given options',()=>{ test('should generate with the given charset',()=>{ let random=randomString.generate({range:'2846',length:6}) - expect(random.match(random,"^\d*[02468]$")).toBeTruthy() + expect(random.match("^\d*[02468]$")).toBeTruthy() }) test('should generate with the given charset',()=>{ let random=randomString.generate({range:'abcdef',length:10}) - expect(random.match(random,/Λ†[a-f]/)).toBeTruthy() + expect(random.match(/[a-f]/)).toBeTruthy() }) test('should be a number only',()=>{ From de149e2bffe20e7182b4a7a29d45e812408d24bb Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 20:28:40 +0300 Subject: [PATCH 06/12] Convert the long if else into switch case statements --- src/index.ts | 54 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index a1db5a6..f1862ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,33 +25,41 @@ export const generate = (option: number | options): string => { const charactersLength = characters.length; result = compute(characters, charactersLength, opt); } else if (typeof option === 'object') { - const { length = 16, range: characters } = option; + + const { length = 16, range: characters, charset } = option; + if (characters) { const charactersLength = characters.toString().length; result = compute(characters, charactersLength, length); result = checkOptions(result, option); - } else if (option.charset === 'number') { - const max = Math.pow(10, length) - 1; - const randomBytes = crypto.randomBytes(Math.ceil(length / 2)); - const randomValue = parseInt(randomBytes.toString('hex'), 16) % max; // Converting the random bytes to a number within the desired range - result = randomValue.toString(); - result = checkOptions(result, option); - } else if (option.charset === 'binary') { - result = compute(binary, 2, length); - } else if (option.charset === 'octal') { - result = compute(octal, 8, length); - } else if (option.charset === 'hex') { - const charactersLength = hex.length; - result = compute(hex, charactersLength, length); - result = checkOptions(result, option); - } else if (option.charset === 'alphabet') { - const charactersLength = alphabet.length; - result = compute(alphabet, charactersLength, length); - result = checkOptions(result, option); - } else { - const charactersLength = alphanumeric.length; - result = compute(alphanumeric, charactersLength, length); - result = checkOptions(result, option); + } + + switch(charset) { + case 'number': + const max = Math.pow(10, length) - 1; + const randomBytes = crypto.randomBytes(Math.ceil(length / 2)); + const randomValue = parseInt(randomBytes.toString('hex'), 16) % max; // Converting the random bytes to a number within the desired range + result = randomValue.toString(); + result = checkOptions(result, option); + break; + case 'binary': + result = compute(binary, 2, length); + break; + case 'octal': + result = compute(octal, 8, length); + break; + case 'hex': + result = compute(hex, hex.length, length); + result = checkOptions(result, option); + break; + case 'alphabet': + result = compute(alphabet, alphabet.length, length); + result = checkOptions(result, option); + break; + default: + const charactersLength = alphanumeric.length; + result = compute(alphanumeric, alphanumeric.length, length); + result = checkOptions(result, option); } } From 96fdd7d7838b555d6e45716e1014ed9fec20bcab Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 20:49:53 +0300 Subject: [PATCH 07/12] Fix flaky unit test about the range option --- src/index.ts | 4 +++- test.js | 9 +++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1862ff..816d56b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,8 @@ import crypto from 'crypto'; * @returns - random string generated */ export const generate = (option: number | options): string => { - const compute = (type: string ,optlen: number, length: number) => { + + const compute = (type: string, optlen: number, length: number) => { let result = ''; const charactersLength = type.length; for (let i = 0; i < length; i += 1) { @@ -18,6 +19,7 @@ export const generate = (option: number | options): string => { } return result; } + let result: string | number = ''; if (typeof option === 'number' || !option) { let opt = option ? option : 16; diff --git a/test.js b/test.js index 3a0c71a..dffe60e 100644 --- a/test.js +++ b/test.js @@ -13,19 +13,16 @@ describe('testing using provide length', () => { describe('testing with given options',()=>{ test('should generate with the given charset',()=>{ - let random=randomString.generate({range:'2846',length:6}) - - expect(random.match("^\d*[02468]$")).toBeTruthy() + let random=randomString.generate({range:'2846',length:6}) + expect(random.match(/[^2846]/)).toBeFalsy() }) test('should generate with the given charset',()=>{ let random=randomString.generate({range:'abcdef',length:10}) - expect(random.match(/[a-f]/)).toBeTruthy() - + expect(random.match(/[a-f]/)).toBeTruthy() }) test('should be a number only',()=>{ let random=randomString.generate({charset:'number',length:6}) expect(random.match(/^[0-9]+$/)).toBeTruthy() - }) test('should be an alphabet only',()=>{ let random=randomString.generate({charset:'alphabet',length:6}) From 7159dc8ba8e6af1cff8c60895aaac1919d7031f5 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 20:54:37 +0300 Subject: [PATCH 08/12] Remove unnecessary variable declaration --- src/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 816d56b..b0b9ab5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,7 +59,6 @@ export const generate = (option: number | options): string => { result = checkOptions(result, option); break; default: - const charactersLength = alphanumeric.length; result = compute(alphanumeric, alphanumeric.length, length); result = checkOptions(result, option); } @@ -68,11 +67,10 @@ export const generate = (option: number | options): string => { return result; }; -export const generateUnicodeEmoji = (length: number) => { - let len = length ? length : 16; +export const generateUnicodeEmoji = (length: number = 16) => { let result = ''; const charactersLength = emojis.length; - for (let i = 0; i < len; i += 1) { + for (let i = 0; i < length; i += 1) { const randomBytes = crypto.randomBytes(1); const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); result += emojis[randomIndex].trim(); From 7b746a8b3a394d01baa2f911da40e0015c2535e5 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 21:00:14 +0300 Subject: [PATCH 09/12] Rename variable name to avoid conflict --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index b0b9ab5..38b5904 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,17 +10,17 @@ import crypto from 'crypto'; export const generate = (option: number | options): string => { const compute = (type: string, optlen: number, length: number) => { - let result = ''; + let computedResult = ''; const charactersLength = type.length; for (let i = 0; i < length; i += 1) { const randomBytes = crypto.randomBytes(1); const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); - result += type.charAt(randomIndex); + computedResult += type.charAt(randomIndex); } - return result; + return computedResult; } - let result: string | number = ''; + let result = ''; if (typeof option === 'number' || !option) { let opt = option ? option : 16; const characters = alphanumeric; From b7b4fac2921e2500f4c051d7e29ac50453dc890c Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 22 Dec 2023 21:17:12 +0300 Subject: [PATCH 10/12] Remove the middle argument which is never used from the compute method --- src/index.ts | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/index.ts b/src/index.ts index 38b5904..a970f36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,32 +7,19 @@ import crypto from 'crypto'; * @param option - could be a number which is the length of the string to be generated || could be an option { * @returns - random string generated */ -export const generate = (option: number | options): string => { - - const compute = (type: string, optlen: number, length: number) => { - let computedResult = ''; - const charactersLength = type.length; - for (let i = 0; i < length; i += 1) { - const randomBytes = crypto.randomBytes(1); - const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); - computedResult += type.charAt(randomIndex); - } - return computedResult; - } - +export const generate = (option: number | options): string => { let result = ''; + if (typeof option === 'number' || !option) { let opt = option ? option : 16; const characters = alphanumeric; - const charactersLength = characters.length; - result = compute(characters, charactersLength, opt); + result = compute(characters, opt); } else if (typeof option === 'object') { const { length = 16, range: characters, charset } = option; if (characters) { - const charactersLength = characters.toString().length; - result = compute(characters, charactersLength, length); + result = compute(characters, length); result = checkOptions(result, option); } @@ -45,21 +32,21 @@ export const generate = (option: number | options): string => { result = checkOptions(result, option); break; case 'binary': - result = compute(binary, 2, length); + result = compute(binary, length); break; case 'octal': - result = compute(octal, 8, length); + result = compute(octal, length); break; case 'hex': - result = compute(hex, hex.length, length); + result = compute(hex, length); result = checkOptions(result, option); break; case 'alphabet': - result = compute(alphabet, alphabet.length, length); + result = compute(alphabet, length); result = checkOptions(result, option); break; default: - result = compute(alphanumeric, alphanumeric.length, length); + result = compute(alphanumeric, length); result = checkOptions(result, option); } } @@ -67,6 +54,17 @@ export const generate = (option: number | options): string => { return result; }; +const compute = (type: string, length: number) => { + let computedResult = ''; + const charactersLength = type.length; + for (let i = 0; i < length; i += 1) { + const randomBytes = crypto.randomBytes(1); + const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); + computedResult += type.charAt(randomIndex); + } + return computedResult; +} + export const generateUnicodeEmoji = (length: number = 16) => { let result = ''; const charactersLength = emojis.length; From 69a4743c480d641f40038adfabede9b93050f581 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Wed, 27 Dec 2023 11:43:21 +0300 Subject: [PATCH 11/12] fix: reformat code using prettier --- src/check.option.ts | 4 +- src/emoji.ts | 2 +- src/index.ts | 15 ++--- src/types.ts | 10 +-- test.js | 161 ++++++++++++++++++++++++++------------------ try.js | 17 +++-- 6 files changed, 120 insertions(+), 89 deletions(-) diff --git a/src/check.option.ts b/src/check.option.ts index 9a2bc0c..a20a936 100644 --- a/src/check.option.ts +++ b/src/check.option.ts @@ -23,7 +23,9 @@ export const checkOptions = (result: string, option: options) => { : alphabetNumericSymbol; const charactersLength = characters.length; for (let i = 0; i < option.length; i++) { - newResult += characters.charAt(Math.floor(Math.random() * charactersLength)); + newResult += characters.charAt( + Math.floor(Math.random() * charactersLength) + ); } } if (option.symbolsOnly) { diff --git a/src/emoji.ts b/src/emoji.ts index 0a12983..363a334 100644 --- a/src/emoji.ts +++ b/src/emoji.ts @@ -1768,7 +1768,7 @@ export const emojis = [ '🧜 ', '🧜 ', '🧝 ', - '🧝 ', + '🧝 ', '🧝 ', '🧝 ', '🧝 ', diff --git a/src/index.ts b/src/index.ts index a970f36..cb441b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import crypto from 'crypto'; * @param option - could be a number which is the length of the string to be generated || could be an option { * @returns - random string generated */ -export const generate = (option: number | options): string => { +export const generate = (option: number | options): string => { let result = ''; if (typeof option === 'number' || !option) { @@ -15,17 +15,16 @@ export const generate = (option: number | options): string => { const characters = alphanumeric; result = compute(characters, opt); } else if (typeof option === 'object') { - const { length = 16, range: characters, charset } = option; - + if (characters) { result = compute(characters, length); result = checkOptions(result, option); } - switch(charset) { + switch (charset) { case 'number': - const max = Math.pow(10, length) - 1; + const max = Math.pow(10, length) - 1; const randomBytes = crypto.randomBytes(Math.ceil(length / 2)); const randomValue = parseInt(randomBytes.toString('hex'), 16) % max; // Converting the random bytes to a number within the desired range result = randomValue.toString(); @@ -59,18 +58,18 @@ const compute = (type: string, length: number) => { const charactersLength = type.length; for (let i = 0; i < length; i += 1) { const randomBytes = crypto.randomBytes(1); - const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); + const randomIndex = Math.floor((randomBytes[0] / 256) * charactersLength); computedResult += type.charAt(randomIndex); } return computedResult; -} +}; export const generateUnicodeEmoji = (length: number = 16) => { let result = ''; const charactersLength = emojis.length; for (let i = 0; i < length; i += 1) { const randomBytes = crypto.randomBytes(1); - const randomIndex = Math.floor(randomBytes[0] / 256 * charactersLength); + const randomIndex = Math.floor((randomBytes[0] / 256) * charactersLength); result += emojis[randomIndex].trim(); } return result; diff --git a/src/types.ts b/src/types.ts index d60b98c..426dc79 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,13 +16,15 @@ export type options = { export const hex = '0123456789ABCDEFabcdef'; export const symbols = '[-!$%^&*()_+|~=`{}[]:";<>?,./]' + "'"; export const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; -export const alphanumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; +export const alphanumeric = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; export const number = '0123456789'; export const alphabetSymbol = - 'IJ[A-B!C$Dh%E^FGH&KfgL*f(zMNy)Q_iOfPe+xdRc|zb~S=aTw`{Y}X[]Z\vrW:j";kt?mVn,o.up/]Uq' + "'"; + 'IJ[A-B!C$Dh%E^FGH&KfgL*f(zMNy)Q_iOfPe+xdRc|zb~S=aTw`{Y}X[]Z\vrW:j";kt?mVn,o.up/]Uq' + + "'"; export const numberSymbol = '[-!$%9^&*()_8+|~6=7`5{4}[]3:"2;<>?0,.1/]' + "'"; export const alphabetNumericSymbol = 'IJ[0A-B!1C$3Dh%E^2FGH&4Kf5gL*6f(zMN7y)Q_iOfPe+xdRc|zb~S=aTw`{Y}X[]Z\vrW:j";kt?9mVn,o.u7p/]Uq' + "'"; - export const binary = '01'; - export const octal = '01234567'; +export const binary = '01'; +export const octal = '01234567'; diff --git a/test.js b/test.js index dffe60e..68a6d0c 100644 --- a/test.js +++ b/test.js @@ -1,71 +1,100 @@ -const randomString =require('./lib/index') +const randomString = require('./lib/index'); describe('testing using provide length', () => { - test('the string should return alphanumeric values',()=>{ - let random=randomString.generate(9) - expect(random.match(/^[a-zA-Z0-9]+$/)).toBeTruthy() - }) - test('length should be 16',()=>{ - let random=randomString.generate() - expect(random.length===16).toBeTruthy() - }) + test('the string should return alphanumeric values', () => { + let random = randomString.generate(9); + expect(random.match(/^[a-zA-Z0-9]+$/)).toBeTruthy(); + }); + test('length should be 16', () => { + let random = randomString.generate(); + expect(random.length === 16).toBeTruthy(); + }); }); -describe('testing with given options',()=>{ - test('should generate with the given charset',()=>{ - let random=randomString.generate({range:'2846',length:6}) - expect(random.match(/[^2846]/)).toBeFalsy() - }) - test('should generate with the given charset',()=>{ - let random=randomString.generate({range:'abcdef',length:10}) - expect(random.match(/[a-f]/)).toBeTruthy() - }) - test('should be a number only',()=>{ - let random=randomString.generate({charset:'number',length:6}) - expect(random.match(/^[0-9]+$/)).toBeTruthy() - }) - test('should be an alphabet only',()=>{ - let random=randomString.generate({charset:'alphabet',length:6}) - expect(random.match(/^[a-zA-Z]+$/)).toBeTruthy() - }) - test('should be Binary only',()=>{ - let random=randomString.generate({charset:'binary',length:6}) - expect(random.match(/[0-1]+$/)).toBeTruthy() - }) - test('should be octal only',()=>{ - let random=randomString.generate({charset:'octal',length:6}) - expect(random.match(/[0-7]+$/)).toBeTruthy() - }) - test('should be hexa only',()=>{ - let random=randomString.generate({charset:'hex',length:6}) - expect(random.match(/[0-9a-fA-F]+$/)).toBeTruthy() - }) - test('should be alphabet with small Letters only',()=>{ - let random=randomString.generate({charset:'alphabet',length:6,lowerCaseOnly:true}) - expect(random.match(/[0-9a-z]+$/)).toBeTruthy() - }) - test('should be alphabet with capital Letters only',()=>{ - let random=randomString.generate({charset:'alphabet',length:6,upperCaseOnly:true}) - expect(random.match(/[0-9A-Z]+$/)).toBeTruthy() - }) - - test('should be symbols with number only',()=>{ - let random=randomString.generate({insertSymbol:true,charset:"number"}) - console.log("Symbol and number : "+random); - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/+0-9]+$/)).toBeTruthy() - }) - test('should be symbols with alphanumeric small letters only',()=>{ +describe('testing with given options', () => { + test('should generate with the given charset', () => { + let random = randomString.generate({ range: '2846', length: 6 }); + expect(random.match(/[^2846]/)).toBeFalsy(); + }); + test('should generate with the given charset', () => { + let random = randomString.generate({ range: 'abcdef', length: 10 }); + expect(random.match(/[a-f]/)).toBeTruthy(); + }); + test('should be a number only', () => { + let random = randomString.generate({ charset: 'number', length: 6 }); + expect(random.match(/^[0-9]+$/)).toBeTruthy(); + }); + test('should be an alphabet only', () => { + let random = randomString.generate({ charset: 'alphabet', length: 6 }); + expect(random.match(/^[a-zA-Z]+$/)).toBeTruthy(); + }); + test('should be Binary only', () => { + let random = randomString.generate({ charset: 'binary', length: 6 }); + expect(random.match(/[0-1]+$/)).toBeTruthy(); + }); + test('should be octal only', () => { + let random = randomString.generate({ charset: 'octal', length: 6 }); + expect(random.match(/[0-7]+$/)).toBeTruthy(); + }); + test('should be hexa only', () => { + let random = randomString.generate({ charset: 'hex', length: 6 }); + expect(random.match(/[0-9a-fA-F]+$/)).toBeTruthy(); + }); + test('should be alphabet with small Letters only', () => { + let random = randomString.generate({ + charset: 'alphabet', + length: 6, + lowerCaseOnly: true, + }); + expect(random.match(/[0-9a-z]+$/)).toBeTruthy(); + }); + test('should be alphabet with capital Letters only', () => { + let random = randomString.generate({ + charset: 'alphabet', + length: 6, + upperCaseOnly: true, + }); + expect(random.match(/[0-9A-Z]+$/)).toBeTruthy(); + }); - let random=randomString.generate({insertSymbol:true,lowerCaseOnly:true}) - console.log(random) - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9a-z]+$/)).toBeTruthy() - }) - test('should be symbols with alphanumeric capital letters only',()=>{ - let random=randomString.generate({insertSymbol:true,upperCaseOnly:true,length:5}) - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9A-Z]/)).toBeTruthy() - }) - test('should be symbols with given provided number',()=>{ - let random=randomString.generate({insertSymbol:true,range:"123abc",length:6}) - expect(random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/1-3a-c]/)).toBeTruthy() - }) -}) \ No newline at end of file + test('should be symbols with number only', () => { + let random = randomString.generate({ + insertSymbol: true, + charset: 'number', + }); + console.log('Symbol and number : ' + random); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/+0-9]+$/) + ).toBeTruthy(); + }); + test('should be symbols with alphanumeric small letters only', () => { + let random = randomString.generate({ + insertSymbol: true, + lowerCaseOnly: true, + }); + console.log(random); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9a-z]+$/) + ).toBeTruthy(); + }); + test('should be symbols with alphanumeric capital letters only', () => { + let random = randomString.generate({ + insertSymbol: true, + upperCaseOnly: true, + length: 5, + }); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/0-9A-Z]/) + ).toBeTruthy(); + }); + test('should be symbols with given provided number', () => { + let random = randomString.generate({ + insertSymbol: true, + range: '123abc', + length: 6, + }); + expect( + random.match(/[-!$%^&*()_+|~=`{}\[\]:";<>?,.\/1-3a-c]/) + ).toBeTruthy(); + }); +}); diff --git a/try.js b/try.js index aed6912..947d6a9 100644 --- a/try.js +++ b/try.js @@ -1,11 +1,10 @@ -const randomString =require('./lib/index') +const randomString = require('./lib/index'); +let str = randomString.generate({ + length: 10, + range: 'Feven', + insertSymbol: true, + insertSymbol: true, +}); -let str=randomString.generate({ -length:10, -range: "Feven", -insertSymbol:true, -insertSymbol:true, -}) - -console.log(str) +console.log(str); From 137db72a3c67a3b4089596890eecec5bad09ec62 Mon Sep 17 00:00:00 2001 From: Dagmawi zewdu Date: Wed, 27 Dec 2023 11:58:50 +0300 Subject: [PATCH 12/12] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d24303c..53ebd37 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "randomized-string", - "version": "2.0.0", + "version": "2.0.1", "description": "", "main": "lib/index", "bin": { @@ -40,4 +40,4 @@ "nodemon": "^2.0.15", "typescript": "^4.6.2" } -} \ No newline at end of file +}