Package Exports
- random-codes
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (random-codes) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
random-codes
Customizable random codes generator to use for example generating giftcards codes
Instalation
Install using npm:
$ npm install random-codes
Usage
You can crete a RandomCodes object with your own config array
var RandomCodes = require('random-codes');
var rc = new RandomCodes(config);
Code creation
var code = rc.generate();
// >> 'S5RU-1K1U-F77Z'
Code validation
The validation function gets a user input and returns it formatted as defined by the config array
var validated_code = rc.validate('S5RU-1K1U-F77Z');
// >> 'S5RU-1K1U-F77Z'
var validated_code = rc.validate('S5RU1K1UF77Z');
// >> 'S5RU-1K1U-F77Z'
var validated_code = rc.validate('s5ru-1k1u-f77z');
// >> 'S5RU-1K1U-F77Z'
var validated_code = rc.validate('s5ru1k1uf77z');
// >> 'S5RU-1K1U-F77Z'
Code masking
var masked_code = rc.mask('S5RU-1K1U-F77Z');
// >> '****-****-F77Z'
Config
You can modify any parameter in the config array, the default one is:
var config = {
// A string containing available chars
chars: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
// Separator char used to divide code parts
separator: '-',
// Char used to mask code
mask: '*',
// Number of parts the code contains
parts: 3,
// Size of each part
part_size: 4,
// Function used to get a random char from the chars pool
// (Please use a better one)
getChar: function (pool) {
var random = Math.floor(Math.random() * pool.length);
return pool.charAt(random);
}
};
Keep in mind that the validate function assumes that you only use upper case letters to generate codes and only checks for '-' as separator.