File size: 4,085 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { getCreditCardType } from 'calypso/lib/checkout';

/**
 * Formats a credit card card number
 * @param {string} cardNumber unformatted field value
 * @returns {string} formatted value
 */
export function formatCreditCard( cardNumber ) {
	if ( getCreditCardType( cardNumber ) === 'amex' ) {
		return formatAmexCreditCard( cardNumber );
	}
	const digits = cardNumber.replace( /[^0-9]/g, '' ).slice( 0, 19 );
	const formattedNumber = `${ digits.slice( 0, 4 ) } ${ digits.slice( 4, 8 ) } ${ digits.slice(
		8,
		12
	) } ${ digits.slice( 12 ) }`;
	return formattedNumber.trim();
}

/**
 * Formats an American Express card number
 * @param {string} cardNumber unformatted field value
 * @returns {string} formatted value
 */
export function formatAmexCreditCard( cardNumber ) {
	const digits = cardNumber.replace( /[^0-9]/g, '' ).slice( 0, 15 );
	const formattedNumber = `${ digits.slice( 0, 4 ) } ${ digits.slice( 4, 10 ) } ${ digits.slice(
		10,
		15
	) }`;
	return formattedNumber.trim();
}

const fieldMasks = {};
const unmask = ( value ) => value;

fieldMasks[ 'expiration-date' ] = {
	mask: function ( previousValue, nextValue ) {
		// If the user is deleting from the value then don't modify it
		if ( previousValue && previousValue.length > nextValue.length ) {
			return nextValue;
		}

		// If the user is adding a slash then don't modify it
		if (
			previousValue &&
			previousValue.length === 2 &&
			nextValue.length === 3 &&
			nextValue[ 2 ] === '/'
		) {
			return nextValue;
		}

		// Remove anything except digits and slashes
		nextValue = nextValue.replace( /[^\d]/g, '' );

		if ( nextValue.length <= 2 ) {
			return nextValue;
		}

		return nextValue.substring( 0, 2 ) + '/' + nextValue.substring( 2, 4 );
	},

	unmask,
};

fieldMasks.number = {
	mask: function ( previousValue, nextValue ) {
		return formatCreditCard( nextValue );
	},

	unmask: function ( value ) {
		return value.replace( / /g, '' );
	},
};

fieldMasks.cvv = {
	mask: function ( previousValue, nextValue ) {
		return nextValue.replace( /[^\d]/g, '' ).substring( 0, 4 );
	},

	unmask,
};

fieldMasks.nik = {
	mask: function ( previousValue, nextValue ) {
		const digitsOnly = nextValue.replace( /[^0-9]/g, '' );
		return digitsOnly;
	},

	unmask,
};

// `document` is an EBANX field. Currently used for Brazilian CPF numbers
// See isValidCPF()/isValidCNPJ() / ebanx.js
fieldMasks.document = {
	mask: function ( previousValue, nextValue ) {
		let string = nextValue;

		const digits = nextValue.replace( /[^0-9]/g, '' );

		if ( digits.length > 11 ) {
			// CNPJ
			string =
				digits.slice( 0, 2 ) +
				'.' +
				digits.slice( 2, 5 ) +
				'.' +
				digits.slice( 5, 8 ) +
				'/' +
				digits.slice( 8, 12 ) +
				'-' +
				digits.slice( 12, 14 );
		} else {
			// CPF
			string =
				digits.slice( 0, 3 ) +
				'.' +
				digits.slice( 3, 6 ) +
				'.' +
				digits.slice( 6, 9 ) +
				'-' +
				digits.slice( 9, 11 );
		}

		return string.replace( /^[\s.-]+|[\s.-]+$/g, '' );
	},

	unmask,
};

/**
 * Formats a field value
 * @param {string} fieldName name of field corresponding to a child open of `fieldMasks`
 * @param {string} previousValue the current value of the field before change
 * @param {string} nextValue the new, incoming value of the field on change
 * @returns {string} formatted value
 */
export function maskField( fieldName, previousValue, nextValue ) {
	const fieldMask = fieldMasks[ fieldName ];
	if ( ! fieldMask ) {
		return nextValue;
	}

	return fieldMask.mask( previousValue, nextValue );
}

/**
 * Reverses masking formats of a field value
 * @param {string} fieldName name of field corresponding to a child open of `fieldMasks`
 * @param {string} previousValue the current value of the field before change
 * @param {string} nextValue the new, incoming value of the field on change
 * @returns {string} deformatted value
 */
export function unmaskField( fieldName, previousValue, nextValue ) {
	const fieldMask = fieldMasks[ fieldName ];
	if ( ! fieldMask ) {
		return nextValue;
	}

	return fieldMask.unmask( fieldMask.mask( previousValue, nextValue ) );
}