qinu
Highly customizable generator of [random] strings.
Installation
Via NPM
Install the package
$ npm install qinu
require it
const qinu =
Via Bower
Install the package
$ bower install qinu
add script on page
as well you can do it with RequireJS or any similar tool.
Usage
Basic usage
// Generates 32-character random string, e.g. '4plugjpebgyqduvwcy7lo74wj4idxu5w'const randomString =
Using with options
// Generates 64-character random string// that contains only hexadecimal numbers,// e.g. 'df6x4ou3p5tar625301dfuftqd8rh9kxwcjl6t0mtmxke3a9q8wuu3nhvrtrn3na'const randomString =
Using with template
// Generates string corresponding to template,// e.g. 'LABEL: hjwjd4hk4cpx7b1ekh9vdmvsnob228bf SUFIX'const randomString =
%qinu%
will be replaced with generated random string. Each passed argument will replace the relative code %arg[<index>]%
.
Instead of array you can pass all template arguments as function arguments:
// It works the same as an example aboveconst randomString =
There is another option to pass arguments to the template, via args
option:
// Still, the same as examples aboveconst randomString =
In case when both described options will be used arguments will be merged: args
from the options object will be in the beginning and arguments from the function argument will be in the end:
// Still, no differenceconst randomString =
Using predefined options
You can create qinu-generator with predefined options:
const qinuCustom = qinu // Generates string corresponding to template,// e.g. 'group-label-wh0qothao58nk0zno2g86ct4gl3j9wa7'const randomString = // or use it with arrayconst anotherRandomString =
When using
qinu.create()
generated results are ordered in a sequence. This behavior can be changed usingrandom
option
Shortcut for length property
In case you need to specify only the length
:
// Generates 64-character random stringconst randomString = // The shortcut can be applied to create method as well:const generateRandom10Characters = qinu// Generate 10-character random stringconst randomString =
Generating unique sequence (random option)
In case if absolute uniqueness is required during the session, qinu.create()
creates a function that first generate a random value and then iterate it one-by-one. This makes it impossible to generate the same string unless all possible combinations wasn't generated.
const next = qinu // 'abb' // 'abc' // 'aca'
If randomness is preferred pass random: true
when create generator:
const generate = qinu // 'ccb' // 'aaa' // 'baa'
License
MIT © Stanislav Termosa