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 = ;var rc = config;
Code creation
var code = rc;// >> '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;// >> 'S5RU-1K1U-F77Z'var validated_code = rc;// >> 'S5RU-1K1U-F77Z'var validated_code = rc;// >> 'S5RU-1K1U-F77Z'var validated_code = rc;// >> 'S5RU-1K1U-F77Z'
Code masking
var masked_code = rc;// >> '****-****-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) { var random = Math; return pool; };
Keep in mind that the validate function assumes that you only use upper case letters to generate codes and only checks for '-' as separator.