Skip to content

Commit

Permalink
refactor: rename clean helper to unMask, add usage example in jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
probil committed Jul 9, 2019
1 parent f15ad86 commit 5dfad35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
22 changes: 11 additions & 11 deletions src/__tests__/format.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import format, { clean } from '../format';
import format, { unMask } from '../format';

describe('default export', () => {
it('should be a function', () => {
Expand Down Expand Up @@ -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');
});
});
4 changes: 2 additions & 2 deletions src/directive.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

0 comments on commit 5dfad35

Please sign in to comment.