All files / app/payment/creditCard unformatCreditCardExpiryDate.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19          52x 10x 10x   10x 1x     9x          
export interface ExpiryDate {
    month: string;
    year: string;
}
 
export default function unformatCreditCardExpiryDate(value: string): ExpiryDate {
    const separator = '/';
    const [month = '', year = ''] = value.split(new RegExp(`\\s*${separator}\\s*`));
 
    if (!/^\d+$/.test(month) || !/^\d+$/.test(year)) {
        return { month: '', year: '' };
    }
 
    return {
        month: month.length === 1 ? `0${month}` : month.slice(0, 2),
        year: year.length === 2 ? `20${year}` : year.slice(0, 4),
    };
}