1.0.3 • Published 9 years ago
node-code-generator v1.0.3
CodeGenerator
Installation
$ npm install node-code-generator
Usage
var CodeGenerator = require('node-code-generator');
var generator = new CodeGenerator();
var pattern = 'ABC#+';
var howMany = 100;
var options = {};
// Generate an array of random unique codes according to the provided pattern:
var codes = generator.generateCodes(pattern, howMany, options);
Pattern
In the pattern, the following characters are replaced:
#
with a single numeric character*
with a single alphanumeric character (excluding ambiguous letters and numbers)#+
with a variable number of numeric characters (the number of characters depends on how many codes we need to generate and on the sparsity option)*+
with a variable number of alphanumeric characters (the number of characters depends on how many codes we need to generate and on the sparsity option)
Options
Name | Default | Description |
---|---|---|
sparsity | 1 | How sparse should the generated set be? The default setting generates a dense set, but probably if you are generating vouchers you want to provide an higer number to avoid consecutive (guessable) codes |
existingCodesLoader | function(pattern) { return []; } | Provide a function that returns an array of previously generated codes for this pattern to avoid duplicates |
alphanumericChars | 123456789ABCDEFGHJKLMNPQRSTUVWXYZ | Characters that will be substituted to * |
numericChars | 0123456789 | Characters that will be substituted to # |
alphanumericRegex | /\*(?!\+)/g | Regular expression to match * |
numericRegex | /#(?!\+)/g | Regular expression to match # |
alphanumericMoreRegex | /\*\+/g | Regular expression to match *+ |
numericMoreRegex | /#\+/g | Regular expression to match #+ |
Extra
The library also exposes the simple randomChars(allowedChars, howMany)
function to simply generate random strings (based on Math.random(), so don't use it where security matters!
Contribute
Fork -> Branch -> Pull request!
License
MIT (see LICENSE)