Voucher Code Generator JS

repository·master·Indexed 43 days ago

https://github.com/voucherifyio/voucher-code-generator-js

A lightweight JavaScript library (v1.3.0) for generating unique, random, and hard-to-guess coupon or voucher codes. It supports custom charsets, prefixes, postfixes, patterns, and sequential code generation via sequenceOffset.

Tokens
1.4K
Snippets
8
Records
8
Agent score
5%

What's inside Voucher Code Generator

  1. Use patterns to define voucher structure

    master

    The pattern option allows you to define a fixed structure for your voucher codes. Use the # symbol to indicate positions where characters from the charset should be inserted.

    • The number of # characters in your pattern determines the effective length of the code.
    • Characters other than # are treated as literal strings.

    Example: A pattern of "XX-####-YY" will result in codes that always start with XX, end with YY, and have 4 variable characters in the middle.

    const voucher_codes = require('voucher_codes');
    
    const codes = voucher_codes.generate({
      count: 2,
      pattern: 'PROMO-####'
    });
    // Example output: ["PROMO-a1b2", "PROMO-c3d4"]
  2. Handle infeasible code configurations

    master

    If a configuration is mathematically impossible (e.g., requesting 1000 unique codes that are only 2 characters long using only numbers), the library will throw an error: "Not possible to generate requested number of codes.".

    You should wrap your generation logic in a try...catch block to handle these cases.

    try {
        voucher_codes.generate({
            count: 1000,
            length: 2,
            charset: "0123456789"
        });
    } catch (e) {
        console.log("Sorry, not possible.");
    }
  3. Use prefixes, postfixes, and patterns in codes

    master

    Prefix and Postfix

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

    voucher_codes.generate({
        prefix: "promo-",
        postfix: "-2015"
    });
    // Result: ["promo-WZ4x1t3U-2015"]

    Patterns

    You can define a specific structure using a pattern. Use the hash symbol (#) as a placeholder for random characters. When a pattern is provided, the length option is ignored.

    voucher_codes.generate({
        pattern: "##-###-##",
    });
    // Result: ["P7-ofW-Ka"]
    voucher_codes.generate({
        pattern: "##-###-##",
    });
  4. Customize charsets for voucher codes

    master

    You can specify a custom string of characters to use for generation, or use one of the library's predefined charsets via voucher_codes.charset(name).

    Predefined Charsets

    namecharset
    "numbers""0123456789"
    "alphabetic""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "alphanumeric""0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    Examples

    Custom string charset:

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

    Using a predefined charset:

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

    master

    Use the voucher_codes.generate(config, [sequenceOffset]) method to create an array of unique, random codes.

    Basic Usage

    Generate 5 codes, each 8 characters long:

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

    Configuration Options

    attributedefault valuedescription
    length8Number of characters in a generated code (excluding prefix and postfix)
    count1Number of codes generated
    charsetalphanumericCharacters 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. Note: if pattern is specified, length is ignored.
  6. Generate sequential codes using sequenceOffset

    master

    You can generate a specific code from the pool of all possible combinations by providing a sequenceOffset as the second argument to generate().

    • An offset of 0 or less returns the first possible combination.
    • An offset greater than or equal to the maximum number of combinations returns the last possible combination.
    • When combined with count, you can generate a series of sequential codes starting from that offset.
    var sequenceOffset = 52;
    
    voucher_codes.generate({
        count: 3,
        length: 2,
        charset: "0123456789"
    }, sequenceOffset);
    // Result: ["52", "53", "54"]
    var sequenceOffset = 52;
    
    voucher_codes.generate({
        count: 3,
        length: 2,
        charset: "0123456789"
    }, sequenceOffset);
  7. Access predefined charsets with charset()

    master

    The charset(name) function returns a string containing a predefined set of characters. This can be used to populate the charset property in your configuration object.

    Supported names:

    • numbers: "0123456789"
    • alphabetic: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    • alphanumeric: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const voucher_codes = require('voucher_codes');
    
    const alpha = voucher_codes.charset('alphabetic');
    // returns "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"