1 import creditCardType from 'credit-card-type';
3 import { isNumber } from '@proton/shared/lib/helpers/validators';
5 const banks = require.context('@proton/styles/assets/img/credit-card-icons', true, /.svg$/);
8 [bank: string]: () => string | undefined;
11 const banksMap = banks.keys().reduce<BanksMap>((acc, key) => {
12 acc[key] = () => banks(key);
16 export const getBankSvg = (type = '') => {
17 const key = `./cc-${type}.svg`;
23 return banksMap[key]();
26 export const isValidNumber = (v: string) => !v || isNumber(v);
28 const withGaps = (value = '', gaps: number[] = []) => {
29 return [...value].reduce((acc, digit, index) => {
30 if (gaps.includes(index)) {
31 return `${acc} ${digit}`;
33 return `${acc}${digit}`;
37 export const formatCreditCardNumber = (value: string) => {
38 const [firstCreditCardType] = creditCardType(value);
39 const { type = '', niceType = '', gaps = [], code } = firstCreditCardType || {};
40 const bankIcon = getBankSvg(type);
41 const valueWithGaps = gaps.length ? withGaps(value, gaps) : value;
47 codeName: code?.name ?? 'CVV',