All files / app/payment/creditCard formatCreditCardExpiryDate.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 1953x 6x 6x 6x 6x     6x         6x 1x     5x    
export default function formatCreditCardExpiryDate(value: string): string {
    const separator = '/';
    const [month = '', year = ''] = value.split(new RegExp(`\\s*${separator}\\s*`));
    const trimmedMonth = month.slice(0, 2);
    const trimmedYear = year.length === 4 ? year.slice(-2) : (year ? year.slice(0, 2) : month.slice(2));
 
    // i.e.: '1'
    Iif (value.length < 2) {
        return month;
    }
 
    // ie.: '10 /' (without trailing space)
    if (value.length > 3 && !trimmedYear) {
        return trimmedMonth;
    }
 
    return `${trimmedMonth} / ${trimmedYear}`;
}