lnr0626 / coupon_code_ex

Generate and validate coupon codes in Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CouponCodeEx

BuildStatusCoverage StatusHex pmGitHub

Generate and verify coupon codes in Elixir.

This library aims to be compatible with Algorithm-CouponCode and its tests including the major features such as:

  • Similar character correction
  • Undersirable code filter for bad and transposable parts
  • Plaintext generation

Installation

Add :coupon_code_ex to your project's mix.exs:

def deps do
  [
    {:coupon_code_ex, "~> 0.1.0"}
  ]
end

Usage

This library is primarily about two functions: CouponCode.generate/1 and CouponCode.validate/2. It is primarily used like this:

# Generate a coupon code
CouponCode.generate()
"207Q-DVTV-K4EW"

CouponCode.generate(parts: 4)
"73F3-LMHT-T9JL-WT8W"

CouponCode.generate(parts: 2, part_length: 5)
"HMUCQ-2U12D"


# Static plaintext codes
CouponCode.generate(plaintext: "1234567890")
"1K7Q-CTFM-LMTC"


# Validate a code
CouponCode.validate("7B5M-LJ4J-D5FN")
{:ok, "7B5M-LJ4J-D5FN"}

CouponCode.validate("7B5X-LJ4X-D5FX")
{:error, :part_invalid, 0}

CouponCode.validate("7B5M-LJ4J-D5FN", parts: 2)
{:error, :parts_invalid, 3}

CouponCode.validate("7B5M-LJ4J-D5FN", parts: 2, part_length: 6)
{:error, :part_invalid, 0}


# Validate similar codes
CouponCode.validate("I9oD-V467-8D52")
{:ok, "190D-V467-8D52"}

CouponCode.validate("i9oD-V467-8Dsz")
{:ok, "190D-V467-8D52"}

CouponCode.validate("i9oDV4678Dsz")
{:ok, "190D-V467-8D52"}

Checkout the HexDocs for a more detailed documentation including the bad word list and default configs.

About

Generate and validate coupon codes in Elixir

License:GNU General Public License v3.0


Languages

Language:Elixir 100.0%