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 20 21 22 23 | 54x 54x 54x 19x 19x 1x 18x 18x 49x 34x | import { number } from 'card-validator'; import unformatCreditCardNumber from './unformatCreditCardNumber'; export default function formatCreditCardNumber(value: string, separator: string = ' '): string { const { card } = number(value); if (!card) { return value; } const unformattedValue = unformatCreditCardNumber(value, separator); return card.gaps .filter(gapIndex => unformattedValue.length > gapIndex) .reduce((output, gapIndex, index) => ( [ output.slice(0, gapIndex + index), output.slice(gapIndex + index), ].join(separator) ), unformattedValue); } |