generate-string is a string generator that build random strings, strings with mask and passwords with password-strength tester. It is lightweight, extensible, has no dependencies, and can be used on the server with NodeJS or in-browser with JavaScript.
From the command line:
npm install generate-strings --save Within your document:
<script src='generate-strings.js'></script> - Generate random strings (default) like below:
,9nlg4^] - Generate strings with mask like below:
'@#$%-@#$%-@#$%-@#$%' = 'Aa!0-Aa!0-Aa!0-Aa!0' - Generate passwords with password-strength tester like below:
{ password: '2dt4hKIPO*=He', strength: 'high' } After you've included it into your project, using the module is straightforward:
// require the module const str = require('generate-strings'); // invoke generate() to generate a random string const string = str.generate(/* settings into object */) // shows the result console.log(string) // something like ,9nlg4^] // in the browser, including the script will make a generate() function available. const str = generate() // will return a string The module may be configured as follows:
const str = require('generate-strings'); // Pass a hash of settings into an object. The settings shown here are the defaults. const settings = { /* ************************************************* Settings for all modes ************************************************* */ amount: 1, // set the amount of strings to generate mode: 'random', // set the mode. Allows "random", "mask" and "password" upperCases: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', // upperCases characters lowerCases: 'abcdefghijklmnopqrstuvwxyz', // lowerCase characters specials: '!@#$%&*()=[]{}', // special characters numbers: '0123456789', // numbers /* ************************************************* Settings for random string and password modes ************************************************** */ length: 8, // length of the strings upperCase: true, // set a boolean value to generate strings with upperCase characters lowerCase: true, // set a boolean value to generate strings with lowerCase characters special: false, // set a boolean value to generate strings with special characters number: true, // set a boolean value to generate strings with numbers /* ************************************************* Settings for password mode ************************************************* */ showStrength: false, // Shows the password strength // like: strength: high. Possible results: unacceptable, terrible, medium, good and high. firstCharType: 'random', // set the type of first character when generate a password // 'random' - random type // 'upperCase' - to upperCase character // 'lowerCase' - to lowerCase character // 'special' - to special character // 'number' - to number /* ************************************************* Settings for mask mode ************************************************* */ mask: '@#$%-@#$%-@#$%-@#$%' // mask of string // @ - to upperCase characters // # - to lowerCase characters // $ - to special characters // % - to numbers // others: no will be changed } // and then: const string = str.generate(settings) To run the test, simply type cd into directory and run npm test. You may first need to run npm install to install the required development dependencies. (These dependencies are not required in a production environment, and facilitate only unit testing.)
If you'd like to contribute, please fork this repository, make your changes, and then submit a pull-request.