diff --git a/src/__tests__/format.test.js b/src/__tests__/format.test.js index a97c61fe..187ea52e 100644 --- a/src/__tests__/format.test.js +++ b/src/__tests__/format.test.js @@ -1,4 +1,4 @@ -import format, { clean } from '../format'; +import format, { unMask } from '../format'; describe('default export', () => { it('should be a function', () => { @@ -81,25 +81,25 @@ describe('default export', () => { }); }); -describe('.clean()', () => { +describe('.unMask()', () => { it('should return the entry value without the mask', () => { // time with seconds - expect(clean('11:15:15', '##:##:##')).toBe('111515'); + expect(unMask('11:15:15', '##:##:##')).toBe('111515'); // hours and minutes - expect(clean('20h15m', '##h##m')).toBe('2015'); + expect(unMask('20h15m', '##h##m')).toBe('2015'); // date-time - expect(clean('27/10/2016 23:15', '##/##/#### ##:##')).toBe('271020162315'); + expect(unMask('27/10/2016 23:15', '##/##/#### ##:##')).toBe('271020162315'); // credit card - expect(clean('4532 4782 5524 7634', '#### #### #### ####')).toBe('4532478255247634'); + expect(unMask('4532 4782 5524 7634', '#### #### #### ####')).toBe('4532478255247634'); // phone number - expect(clean('(999) 999-9999', '(###) ###-####')).toBe('9999999999'); + expect(unMask('(999) 999-9999', '(###) ###-####')).toBe('9999999999'); // phone number (US) - expect(clean('+1 (999) 999-9999', '+1 (###) ###-####')).toBe('9999999999'); + expect(unMask('+1 (999) 999-9999', '+1 (###) ###-####')).toBe('9999999999'); // CPF - expect(clean('390.533.447-05', '###.###.###-##')).toBe('39053344705'); + expect(unMask('390.533.447-05', '###.###.###-##')).toBe('39053344705'); // CPNJ - expect(clean('53.288.196/0001-28', '##.###.###/####-##')).toBe('53288196000128'); + expect(unMask('53.288.196/0001-28', '##.###.###/####-##')).toBe('53288196000128'); // Social Security number - expect(clean('365-03-8704', '###-##-####')).toBe('365038704'); + expect(unMask('365-03-8704', '###-##-####')).toBe('365038704'); }); }); diff --git a/src/directive.js b/src/directive.js index 5b700ec2..c7fa2499 100644 --- a/src/directive.js +++ b/src/directive.js @@ -1,5 +1,5 @@ /* eslint-disable no-param-reassign */ -import format, { clean } from './format'; +import format, { unMask } from './format'; import { trigger, queryInputElementInside } from './utils'; import { isAndroid, isChrome } from './utils/env'; @@ -21,7 +21,7 @@ function updateValue(el, force = false) { } el.dataset.previousValue = value; - el.dataset.rawValue = clean(value, mask); + el.dataset.unmaskedValue = unMask(value, mask); } /** diff --git a/src/format.js b/src/format.js index f5f6a397..020b451a 100644 --- a/src/format.js +++ b/src/format.js @@ -48,13 +48,15 @@ export const mask = (text, wholeMask) => { }; /** - * Cleans data. + * Returns text without mask-provided delimiters + * @example + * unMask('11:12', '##:##'); // -> 1112 * - * @param {String} text String to clean (input value) - * @param {String} [wholeMask] Mask format, like `####-##` + * @param {String} text - String to clean (input value) + * @param {String} [wholeMask] - Mask format, like `####-##` * @returns {string} Cleaned text */ -export const clean = (text, wholeMask) => { +export const unMask = (text, wholeMask) => { for (let maskIndex = 0; maskIndex < wholeMask.length; maskIndex += 1) { const maskChar = wholeMask.charAt(maskIndex); switch (maskChar) { @@ -104,5 +106,5 @@ export const prepare = (text, wholeMask) => { export default function (text, wholeMask) { if (!wholeMask) return text; - return mask(clean(prepare(text, wholeMask), wholeMask), wholeMask); + return mask(unMask(prepare(text, wholeMask), wholeMask), wholeMask); }