1.0.3 • Published 9 years ago

node-code-generator v1.0.3

Weekly downloads
304
License
MIT
Repository
github
Last release
9 years ago

CodeGenerator

npm version build coverage

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

NameDefaultDescription
sparsity1How 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
existingCodesLoaderfunction(pattern) { return []; }Provide a function that returns an array of previously generated codes for this pattern to avoid duplicates
alphanumericChars123456789ABCDEFGHJKLMNPQRSTUVWXYZCharacters that will be substituted to *
numericChars0123456789Characters that will be substituted to #
alphanumericRegex/\*(?!\+)/gRegular expression to match *
numericRegex/#(?!\+)/gRegular expression to match #
alphanumericMoreRegex/\*\+/gRegular expression to match *+
numericMoreRegex/#\+/gRegular 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)