@visa/nova-react
Version:
Visa Product Design System Nova React library
119 lines (118 loc) • 5.72 kB
TypeScript
/**
* Copyright (c) 2025 Visa, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
export type CardBrand = 'AMERICAN_EXPRESS' | 'DISCOVER' | 'ELO' | 'MASTER_CARD' | 'MAESTRO' | 'VISA';
export type CardValidator<Brand = CardBrand> = {
/** Regex to test if the BIN is valid. The card could still be short digits or invalid, and still have a valid BIN. */
binRegex: RegExp;
/**
* Regex testing the array should be treated as a logical OR (if any of these are true then it's a BIN match).
* These are split for easier development, and each regex test for certain BIN ranges. These aren't currently used for validation, only development, so they are optional.
*/
binRegexes?: RegExp[];
/** Brand ID */
brand: Brand;
/** Allowed number lengths for the card number */
lengths: number[];
/** Maximum length of the card number */
maxLength: number;
} & ({
/**
* Spacing pattern used to add spaces in card number string.
* For example, if a card number should have 4 digits, space, 7 digits, space, 4 digits, set this as [4, 7, 4].
*/
spacingPattern: number[];
spacingPatterns?: never;
} | {
spacingPattern?: never;
/**
* Spacing patterns map used to add spaces in card number string for particular card number lengths.
* For example, if a 16 digit card number should have 10 digits, space, 6 digits, set this as { 16: [10, 6] }.
*/
spacingPatterns: Record<number, number[]>;
});
export declare const defaultSpacingPattern: number[];
/**
* This is all based off the {@link https://design.visa.com/components/card-input#card-number | card number guidance}.
* @devNote This validator array is not comprehensive and is subject to change. VPDS does not maintain acceptance marks for all brands for legal reasons.
*/
export declare const cardValidators: Record<CardBrand, CardValidator>;
export declare const cardValidatorsArray: CardValidator<CardBrand>[];
/**
* Checks if the card number length is valid based on the card validator's lengths array.
* @param {string} cleanCardNumber
* @param {CardValidator['lengths']} lengths
* @returns {boolean}
*/
export declare const cardNumberLengthCheck: (cleanCardNumber: string, lengths?: CardValidator["lengths"]) => boolean;
/**
* Finds the card validator for the given card number based on the BIN regex.
* If it returns undefined then bin is invalid. It is assumed that the card number is already cleaned.
* @param cardNumber
* @param cardValidators
* @returns {CardValidator | undefined}
*/
export declare const findCardValidatorFromBinRegex: <Brand = CardBrand>(cardNumber: string, cardValidators?: CardValidator<Brand>[]) => CardValidator<Brand> | undefined;
/**
* Formats the card number with the given spacing pattern.
* It is assumed that the card number is already cleaned.
* @param cardNumber
* @param spacingPattern
* @returns {string}
*/
export declare const formatCardNumber: (cardNumber: string, spacingPattern?: number[]) => string;
/**
* Formats the card number with the given card validator's spacing pattern.
* It is assumed that the card number is already cleaned.
* @param {string} cardNumber
* @param {CardValidator<Brand>} cardValidator
* @returns {string}
*/
export declare const formatCardNumberFromValidator: <Brand = CardBrand>(cardNumber: string, cardValidator?: CardValidator<Brand>) => string;
/**
* Based off {@link https://en.wikipedia.org/wiki/Luhn_algorithm | Luhn algorithm}.
* This is typically used to verify the last digit of credit card numbers.
* @param {string} numToCheck string as number to check
* @param {number} mod check modulus, defaults to 10
* @returns {boolean} whether the mod check is true or false
*/
export declare const moduloCheck: (numToCheck: string, mod?: number) => boolean;
/**
* Removes all characters after the maxLength
* @param {string} cardNumber raw card number input
* @param {number | undefined} maxLength remove all characters after this length
* @returns {string}
*/
export declare const removeCharactersAfterMaxLength: (cardNumber: string, maxLength?: number) => string;
/**
* Removes all non-numeric characters
* @param {string} cardNumber raw card number input
* @returns {string}
*/
export declare const removeNonDigits: (cardNumber: string) => string;
declare const _default: {
cardNumberLengthCheck: (cleanCardNumber: string, lengths?: CardValidator["lengths"]) => boolean;
cardValidators: Record<CardBrand, CardValidator<CardBrand>>;
cardValidatorsArray: CardValidator<CardBrand>[];
defaultSpacingPattern: number[];
findCardValidatorFromBinRegex: <Brand = CardBrand>(cardNumber: string, cardValidators?: CardValidator<Brand>[]) => CardValidator<Brand> | undefined;
formatCardNumber: (cardNumber: string, spacingPattern?: number[]) => string;
formatCardNumberFromValidator: <Brand = CardBrand>(cardNumber: string, cardValidator?: CardValidator<Brand>) => string;
moduloCheck: (numToCheck: string, mod?: number) => boolean;
removeCharactersAfterMaxLength: (cardNumber: string, maxLength?: number) => string;
removeNonDigits: (cardNumber: string) => string;
};
export default _default;