@luxuryescapes/lib-voucher-code
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Coupon Code Generator

Generate unique, hard to guess coupon, voucher codes.

Use cases: promo codes, loyalty coupons, gift vouchers, in-app purchases

This library originates from Voucherify.

Installation

In Node.js

Install with npm:

$ npm install --save voucher-code-generator

Include with require:

var voucher_codes = require('voucher-code-generator');

In a browser

<script src="voucher_codes.js"></script>

Usage

Generate 5 codes, each 8 characters long:

voucher_codes.generate({
    length: 8,
    count: 5
});

Sample result: ["FR6bwx1q", "ByamOdWV", "7roFwfQs", "rmWlwvll", "pgih5eAB"]

Charset

Default charset is alphanumeric (numbers and letters). However, you can specify your own charset:

voucher_codes.generate({
    length: 6,
    count: 3,
    charset: "0123456789"
});

Sample result: ["386525", "676442", "019075"]

You can also use one of the predefined charsets by calling voucher_codes.charset(name).

name charset
"numbers" "0123456789"
"alphabetic" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"alphanumeric" "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

For example:

voucher_codes.generate({
    length: 5,
    count: 4,
    charset: voucher_codes.charset("alphabetic")
});

Result: ["odghy", "kZEYc", "eOTCl", "wVCzD"]

Prefix and Postfix

You can optionally surround each generated code with a prefix and/or postfix.

For instance:

voucher_codes.generate({
    prefix: "promo-",
    postfix: "-2015"
});

Result: ["promo-WZ4x1t3U-2015"]

Pattern

Codes may follow a specified pattern. Use hash (#) as a placeholder for random characters. Notice that if pattern is specified then length is ignored.

Example:

voucher_codes.generate({
    pattern: "##-###-##",
});

Result: ["P7-ofW-Ka"]

Infeasible configs

There exist some configs that are not feasible. For example it's not possible to generate 1000 codes if you want your codes to be 2 characters long and consisting only of numbers. Voucher code generator detects such cases and throws an error "Not possible to generate requested number of codes.".

try {
    voucher_codes.generate({
        count: 1000,
        length: 2,
        charset: "0123456789"
    })
catch (e) {
    console.log("Sorry, not possible.");
}

Config reference

attribute default value description
length 8 Number of characters in a generated code (excluding prefix and postfix)
count 1 Number of codes generated.
charset alphanumeric Characters that can appear in the code.
prefix "" A text appended before the code.
postfix "" A text appended after the code.
pattern "########" A pattern for codes where hashes (#) will be replaced with random characters.

Testing

Install dependencies:

npm install

Run tests:

npm run test

License

Code released under the MIT license.

Dependencies (0)

    Dev Dependencies (13)

    Package Sidebar

    Install

    npm i @luxuryescapes/lib-voucher-code

    Weekly Downloads

    66

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    11 kB

    Total Files

    9

    Last publish

    Collaborators

    • georgia-wahib-le
    • jeanbiezus
    • deven.chen
    • emanuel.kozerski
    • elias.vieira
    • roberto-luxuryescapes
    • ethanscavia
    • jahanzeb-hussain-le
    • lukejudd-lux
    • chiamaka-ibeme-le
    • vincent-tran-le
    • welingtonlarsenlux
    • hfrankel
    • sangin.lee
    • tuanhung-pham-le
    • lukas-zimmermann-le
    • lux-alexd
    • william.idoine
    • edward.iskenderian
    • adham-oliveira
    • james-kwok-le
    • aleksandar.vuksanovic
    • nishanthi.n
    • glesage_le
    • daviaazevedo
    • william-ong
    • philippecarvalho
    • dulaj.pathirana
    • hamed.le
    • samhammond
    • paulo.silva3991
    • noahxny
    • dishang
    • ahmad.hafez
    • jordikitto_le
    • steve.dang
    • andrewjohnstone
    • brunohaveroth
    • pavel-lux
    • mary-le
    • pankaj-le
    • rubenbelem
    • gabe-miranda-lux
    • thilina-piyadasun-le
    • gabriel.costa.lux
    • icelic
    • cervantes.kenneth
    • harish-indla-le
    • eran.stiller.luxury.escapes
    • sakethgetla
    • nicholaspuglia
    • sitaraman
    • siddharth.bose
    • aarontoomey-le
    • sergio.costa.luxuryescapes
    • mzilio-lux
    • michael-reid-le
    • matthewdelaneyatluxuryescapes
    • ricardobalbinot
    • callmekev
    • semeer-acheerithodi-le
    • andrey-tryapitsin-le
    • quan.tang
    • alexander.yakovenko
    • greg-cumming-le
    • jerry.huang-le
    • dan-wanigasekera-le
    • dunedayl
    • arminmon-le
    • mattswansonlux
    • cleberricardi
    • felipefeal.lux
    • tracymu
    • chris.nguyen.lux
    • marhalpert
    • reb2020
    • zediah
    • sonyque
    • joshuacullenlux
    • shawn-cx-li
    • colins44
    • diegogadens
    • felixivanov
    • joshua.paling.le
    • operations.luxgroup
    • eddiemcleanlux
    • robert.harris.lux
    • kostya-luxuryescapes
    • alexb.pi
    • kaustuva
    • luizcarraro
    • ryan.tian
    • le_tyang
    • richardbiddle
    • henryshen-le
    • jamesly