fast-key-generator
Version:
Simple utils library for key generation.
91 lines (71 loc) • 2.78 kB
Markdown
# fast-key-generator
##### Simple utils library for API calls.
[](https://www.npmjs.com/package/fast-key-generator)
[](https://www.npmjs.com/package/fast-key-generator)
[](https://www.npmjs.com/package/fast-key-generator)
# Instalation
##### To install `fast-key-generator` - you can use `npm` or `yarn` package manager.
```bash
npm install fast-key-generator --save
```
```bash
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
```js
import { generateKey } from 'fast-key-generator';
const key = generateKey();
// The key will be a string with a length of 16
```
```js
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"
```
```js
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"
```