finanza/card
Payment card numbers (PANs): Luhn check, brand detection, masking and expiry dates.
Brands are detected from a fixed snapshot of prefix and length rules taken in May 2026. This is not a BIN database and does not identify issuers.
Every function that reads a card number or an expiry rejects input
over 1,000 bytes before reading it, as decimal.parse does: a card
number has at most 19 digits, so the limit leaves room for any
spacing, and a long input costs no more than a short one. validate,
mask, last_four and bin return InvalidLength with the byte
count, detect_brand returns Unknown, luhn_valid returns False
and parse_expiry returns Error(Nil). normalize returns its whole
input transformed, whatever its length.
Types
A card brand. Unknown means no prefix and length rule matched.
pub type Brand {
Visa
Mastercard
AmericanExpress
Discover
Jcb
DinersClub
UnionPay
Unknown
}
Constructors
-
Visa -
Mastercard -
AmericanExpress -
Discover -
Jcb -
DinersClub -
UnionPay -
Unknown
A card expiry month and four-digit year.
pub type Expiry {
Expiry(month: Int, year: Int)
}
Constructors
-
Expiry(month: Int, year: Int)
Options for mask. Start from default_mask and change the fields
you need.
pub type MaskOptions {
MaskOptions(
keep_first: Int,
keep_last: Int,
mask_character: String,
group_size: Int,
group_separator: String,
)
}
Constructors
-
MaskOptions( keep_first: Int, keep_last: Int, mask_character: String, group_size: Int, group_separator: String, )Arguments
- keep_first
-
Digits to show at the start.
- keep_last
-
Digits to show at the end.
- mask_character
-
Shown in place of each hidden digit.
- group_size
-
Digits per group. 0 or less turns grouping off.
- group_separator
-
Put between groups.
Why a card number was rejected.
pub type ValidationError {
EmptyInput
InvalidCharacter
InvalidLength(length: Int)
InvalidLuhn
UnknownBrand
}
Constructors
-
EmptyInputNothing was left after removing spaces and separators.
-
InvalidCharacterA character other than a digit was left after
normalize. -
InvalidLength(length: Int)The number has too few or too many digits, or the input is over 1,000 bytes, and then
lengthis its byte count, not a count of digits: such input is not read. -
InvalidLuhnThe number fails the Luhn check.
-
UnknownBrandThe number passes the Luhn check but matches no brand.
Values
pub fn bin(pan: String) -> Result(String, ValidationError)
Returns the first 6 digits of the normalized number, its bank identification number.
pub fn brand_to_string(brand: Brand) -> String
Returns a short upper-case name for the brand, such as "VISA" or
"AMEX".
pub const default_mask: MaskOptions
Shows the first 4 and last 4 digits, hides the rest with *, and
groups by 4 with spaces: "4111 **** **** 1111".
pub fn detect_brand(pan: String) -> Brand
Returns the brand that the prefix and length of the normalized
number match, without the Luhn check. Use validate to check both.
pub fn expiry_valid(expiry: Expiry, today today: Expiry) -> Bool
Returns True when a card with this expiry can still be used in the
month today, that is when expiry is not earlier than today.
Returns False when either month is outside 1 to 12.
Examples
card.expiry_valid(card.Expiry(month: 5, year: 2026), today: card.Expiry(month: 5, year: 2026))
// -> True
pub fn last_four(pan: String) -> Result(String, ValidationError)
Returns the last 4 digits of the normalized number.
pub fn luhn_valid(digits: String) -> Bool
Returns True when digits passes the Luhn check. Returns False
for an empty string, any character other than an ASCII digit, or
input over 1,000 bytes, so call normalize first on typed input.
Examples
card.luhn_valid("79927398713")
// -> True
card.luhn_valid("7992 7398 713")
// -> False
pub fn mask(
pan: String,
with options: MaskOptions,
) -> Result(String, ValidationError)
Normalizes the number and hides the middle digits. The shown start, the hidden middle and the shown end are grouped separately, so a 15-digit Amex keeps its last 4 digits together.
Examples
card.mask("378282246310005", with: card.default_mask)
// -> Ok("3782 **** *** 0005")
pub fn normalize(pan: String) -> String
Removes ASCII whitespace and the separators -, _ and ., and
turns fullwidth and Arabic-Indic digits into ASCII digits. Other
characters are kept, so the result may still not be all digits.
Examples
card.normalize("4111-1111 1111.1111")
// -> "4111111111111111"
pub fn parse_expiry(input: String) -> Result(Expiry, Nil)
Parses an expiry written as MM/YY, MM/YYYY, MMYY or MMYYYY,
with /, - or . between the parts. Surrounding whitespace is
ignored, and a two-digit year YY means 20YY. A one-digit month
needs a separator, since "126" could be January 2026 or December
2006.
Examples
card.parse_expiry("12/28")
// -> Ok(card.Expiry(month: 12, year: 2028))
card.parse_expiry("13/28")
// -> Error(Nil)
pub fn validate(pan: String) -> Result(Brand, ValidationError)
Normalizes the number, then checks that it has 12 to 19 digits, passes the Luhn check and matches a brand.
Examples
card.validate("4111 1111 1111 1111")
// -> Ok(card.Visa)
card.validate("4111 1111 1111 1112")
// -> Error(card.InvalidLuhn)