fast-key-generator
v1.0.1
Published
Simple utils library for key generation.
Downloads
2,894
Maintainers
Readme
fast-key-generator
Simple utils library for API calls.
Instalation
To install fast-key-generator
- you can use npm
or yarn
package manager.
npm install fast-key-generator --save
yarn add fast-key-generator
Documentation
The fast-key-generator
library includes only one function generateKey
.
Below you can find the documentation for this function.
The generateKey
function
The simple method for key generation.
The generateKey
function takes only one parameter, which is an object.
The options
parameter may includes the following fields:
| Name | Type | Default value | Description |
| --------- | ------------------ | -------------- | ----------- |
| size | number | 16 | Key size (without prefix). |
| prefix | string | "" | Key prefix. |
| chartype | string | "alpha" | Can be one of "alpha", "numeric", "alphanum", "symbols". |
| chartset | string | "" | If you provide charset
, then the generator will use the characters from this charset & ignore the chartype. |
| transform | string or function | "none" | Can be one of "none", "lower", "upper" or custom function that takes a key as the first argument & should return formatted key. |
| validate | function | empty function | If the function returns true, the generation process will be completed. If false, the generation process will start again until the function returns true. |
| exclude | array of strings | [] | An array of strings that should not be keys. |
Examples
import { generateKey } from 'fast-key-generator';
const key = generateKey();
// The key will be a string with a length of 16
import { generateKey } from 'fast-key-generator';
const key = generateKey({
size: 1,
prefix: 'key_',
chartype: 'numeric',
exclude: [
'key_0',
'key_1',
'key_2',
'key_3',
'key_4',
'key_5',
'key_6',
'key_7',
'key_8',
]
});
// The key will be "key_9"
import { generateKey } from 'fast-key-generator';
const key = generateKey({
size: 1,
prefix: 'key_',
chartype: 'numeric',
validate: (key) => key.includes('2')
});
// The key will be "key_2"