Generate unique human readable codes (e.g. vouchers, referrals, passwords, keys)
npm install token-gen
npm install token-gen
`Meteor < 1.3 (legacy)
Just add the package from atmosphere:`
meteor add hirespace:token-gen
`Usage
The most common usage would be:`javascript
import TokenGen from 'token-gen'; // var TokenGen = require('token-gen');var code = TokenGen();
console.log(code.toString()); // "48CML"
`Options
The generated code can be customised passing an options object to the
constructor:`javascript
var code = TokenGen({
tokenLength: 3, // Default: 5
alphabet: 'ABC' // Default: '0123456789ACDEFGHJKLMNPQRTUVWXYZ'
});console.log(code.toString()); // "BBA"
`Full list of options
$3
It's the generated code character length.* default:
5Note: The amount of different codes you could generate is
alphabetLength ^ codeLength. Therefore, with the default
alphabet, the maximum amount of different
codes with a 5 characters code is 33554432. We found 33 million codes is more
than enough for most usages. If you need more, the code
length can be customised.As this is the most common option, it is possible to call the generator
directly with a number, which represents the tokenLength:
`javascript
var code = TokenGen(3);console.log(code.toString()); // "20A"
`$3
The character included in this string will be the only characters used
to generate the code. The generated codes are any combination of these
characters.* default:
0123456789ACDEFGHJKLMNPQRTUVWXYZ`We use a default alphabet composed of
32 characters that are not confusing to read (e.g. we removed B because it
could be confused with an 8).