rand-gen
Random character generator that builds a random string. The string produced can include upper and lowercase letters numbers and special characters.
Version: 1.0.3
Installation
npm install rand-gen
Getting Started
To userand-gen
require it into your file and call .gen()
.
``` js
var randGen = require('rand-gen');
var myFunction = function () {
var randStr = randGen().gen();
console.log(randStr); // random string of 15 characters.
};
```
The above function will produce a random string that can include all letters a-z lower and uppercase, numbers 0-9, and special characters.
Options
length
- Default length is15
can be changed.
uppercase
- Default is set totrue
, your random string cannot include uppercase letters set tofalse
.
lowercase
- Default is set totrue
, your random string cannot include lowercase letters set tofalse
.
numbers
- Default is set totrue
, your random string cannot include numbers set tofalse
.
spChars
- Default is set totrue
, your random string cannot include special characters set tofalse
.
include
- If there is a character or a set of characters, you need you include them as a single string or array of individual characters.
exclude
- If you need to exclude a character or a set of characters pass them as a single string or array of individual characters.
Options Usages
``` js var randGen = require('rand-gen'); var myFunction1 = function () {var randStr = randGen({
length: 10
}.gen();
console.log(randStr); // random string of 10 characters.
};
var myFunction2 = function () {
var randStr = randGen({
length: 20,
spChars: false,
numbers: false,
}.gen();
console.log(randStr); // random string of 20 characters that will not have special characters or numbers.
};
var myFunction3 = function () {
var randStr = randGen({
length: 20,
include: "ȦÖ",
numbers: false,
spChars: false
}.gen();
console.log(randStr); // random string of 20 characters that will not have special characters or numbers but includes Ȧ and Ö into the pool.
};
var myFunction4 = function () {
var randStr = randGen({
include: "ȦÖ",
exclude: ["A", "0"]
}.gen();
console.log(randStr); // random string of 15 that includes Ȧ and Ö into the pool but removes A and 0 as options.
};
```
Functions
To go with the optionals that can be passed in you can call individual functions that is not.gen()
to get random characters.
Alphabet Functions
The alphabet function are: ``` js randGen().alphabet(); randGen().alpha.lowercase(); randGen().alpha.uppercase(); ```randGen().alphabet();
- Returns a string that can include upper and lower case letters.
randGen().alpha.lowercase();
- Returns a string that only includes lowercase letters.
randGen().alpha.uppercase();
- Returns a string that only includes uppercase letters.
Number Function
The.number()
will generate a string of numbers it can also return a base 10 integer if you pass true into the function.
Acceptable parameter
Boolean
- Passing a boolean value of true will return a base 10 integer instead of a string.
Number
orString
- You may pass a number or a string representation of a number between 2 and 36 will return a string number in that base.
Special Characters Function
The.spChars()
will generate a string of special characters. The pool of special characters is this: "!@#$%^&()-+=;:{}\"'<>,.\/|~`?".
``` js
randGen().spChars();
```
noDups Function
If the.noDups()
function is called, no two of the same characters will be in the resulting string. Be sure your length does not exceed the amount of total characters in the pool. By default there are 96 characters so don't make your length 97.
``` js
randGen().noDups(); // Returns something like "asdfg1bD6m%0Vq3"
```
Single Character Function
.char()
will return a single random character that could be any lower or uppercase letter, any number or any special character.
``` js
randGen().char();
```