UNPKG

commerce-components

Version:

Vue.js installer plugin for Commerce.js with a global `<chec-payment-form>`, an enhanced Commerce.js aware `<form>` Vue component to facilitate rapid checkout development. (BETA v0.1.0)

22 lines (20 loc) 553 B
/** * Formats raw form input for a credit card number into segments of numbers separated by a space * * @param {string} value * @returns {string} */ function ccFormat(value) { const v = value.replace(/\s+/g, '').replace(/[^0-9]/gi, ''); const matches = v.match(/\d{4,16}/g); const match = (matches && matches[0]) || ''; const parts = []; for (let i = 0, len = match.length; i < len; i += 4) { parts.push(match.substring(i, i + 4)); } if (parts.length) { return parts.join(' '); } return value; } export default ccFormat;