-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (34 loc) · 1.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
/**
* Expose transformer
* @api public
*/
var CC_PATTERN = require('credit-card-regex');
var NON_BREAKING_SPACE = 160;
module.exports = function (input, inputOptions) {
var defaultOptions = {"separator": String.fromCharCode(NON_BREAKING_SPACE)},
options = inputOptions || defaultOptions;
var format = function (input) {
var output = "";
for (var i = 0; i < input.length; i++) {
if (i % 4 == 0 && i > 0) {
output += options.separator;
}
output += input[i];
}
return output;
};
var formatCards = function (inputString) {
var cards = inputString.match(CC_PATTERN(inputString));
if (cards === null) {
return inputString;
}
for (var i = 0; i < cards.length; i++) {
var card = cards[i].trim()
, formattedCard = format(card);
inputString = inputString.replace(card, formattedCard);
}
return inputString;
};
return formatCards(input);
};