"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var getCreditCardTypeAndTrimmedValue_1 = __importDefault(require("../validators/_common/getCreditCardTypeAndTrimmedValue"));
/**
* Reference: https://github.com/braintree/credit-card-type#pretty-card-numbers
* @ignore
*/
function prettyCardNumber(cardNumber, card, separator) {
if (separator === void 0) { separator = ' '; }
if (card) {
// @ts-expect-error: @TODO: fix type
var offsets = [].concat(0, card.gaps, cardNumber.length);
var components = [];
for (var i = 0; offsets[i] < cardNumber.length; i++) {
var start = offsets[i];
var end = Math.min(offsets[i + 1], cardNumber.length);
components.push(cardNumber.substring(start, end));
}
return components.join(separator);
}
return cardNumber;
}
/**
* Format credit card number (e.g. 4242424242424242)
* to format like 4242-4242-4242-4242 (separator can be customized)
*
* @param value The original credit card number valu like 4242424242424242
* @param separator Customized separator. Default to hyphen '-'.
* @category number
* @module formatCreditCardNum
*/
var formatCreditCardNum = function (value, separator) {
if (separator === void 0) { separator = ' '; }
var _a = (0, getCreditCardTypeAndTrimmedValue_1.default)(value, separator, 'onlyMatchOne'), matchedType = _a[0], trimmedValue = _a[1];
if (matchedType) {
var maxLength = __spreadArray([], matchedType.lengths, true).sort().pop();
var limitedValue = trimmedValue.substring(0, maxLength);
return prettyCardNumber(limitedValue, matchedType, separator);
}
return value;
};
exports.default = formatCreditCardNum;
Source