Skip to content

Commit

Permalink
this commit adds rawValue to element dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Teles authored and probil committed Nov 3, 2018
1 parent fbfbd65 commit 07aae69
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 23 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/clean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { clean } from '../format';

describe('format.js - clean function', () => {

it('should return the entry value without the mask', () => {
// time with seconds
expect(clean('11:15:15', '##:##:##')).toBe('111515');
// hours and minutes
expect(clean('20h15m', '##h##m')).toBe('2015');
// date-time
expect(clean('27/10/2016 23:15', '##/##/#### ##:##')).toBe('271020162315');
// credit card
expect(clean('4532 4782 5524 7634', '#### #### #### ####')).toBe('4532478255247634');
// phone number
expect(clean('(999) 999-9999', '(###) ###-####')).toBe('9999999999');
// phone number (US)
expect(clean('+1 (999) 999-9999', '+1 (###) ###-####')).toBe('9999999999');
// CPF
expect(clean('390.533.447-05', '###.###.###-##')).toBe('39053344705');
// CPNJ
expect(clean('53.288.196/0001-28', '##.###.###/####-##')).toBe('53288196000128');
// Social Security number
expect(clean('365-03-8704', '###-##-####')).toBe('365038704');
});
});
3 changes: 2 additions & 1 deletion src/directive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-param-reassign */
import format from './format';
import format, { clean } from './format';
import { trigger, queryInputElementInside } from './utils';
import { isAndroid, isChrome } from './utils/env';

Expand All @@ -21,6 +21,7 @@ function updateValue(el, force = false) {
}

el.dataset.previousValue = value;
el.dataset.rawValue = clean(value, mask)
}

/**
Expand Down
83 changes: 61 additions & 22 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,22 @@
export default function (text, wholeMask) {
if (!wholeMask) return text;

const maskStartRegExp = /^([^#ANX]+)/;
return mask(clean(prepare(text, wholeMask), wholeMask), wholeMask);
}

if (+text.length === 1 && maskStartRegExp.test(wholeMask)) {
text = maskStartRegExp.exec(wholeMask)[0] + text;
}
/**
* Applies mask to data.
*
* @param {String} text String to mask (input value)
* @param {String} [wholeMask] Mask format, like `####-##`
* @returns {string} Formatted text
*/
export const mask = (text, wholeMask) => {
if (!wholeMask) return text;

let newText = '';
let charOffset = 0;

// Cleans data to avoid value loss on dynamic mask changing
for (let maskIndex = 0; maskIndex < wholeMask.length; maskIndex += 1) {
const mask = wholeMask.charAt(maskIndex);
switch (mask) {
case '#':
break;
case 'A':
break;
case '?':
break;
case 'N':
break;
case 'X':
break;
default:
text = text.replace(mask, '');
}
}
for (let maskIndex = 0, x = 1; x && maskIndex < wholeMask.length; maskIndex += 1) {
const char = text.charAt(maskIndex - charOffset);
const mask = wholeMask.charAt(maskIndex);
Expand Down Expand Up @@ -73,3 +62,53 @@ export default function (text, wholeMask) {
}
return newText;
}

/**
* Cleans data.
*
* @param {String} text String to clean (input value)
* @param {String} [wholeMask] Mask format, like `####-##`
* @returns {string} Cleaned text
*/
export const clean = (text, wholeMask) => {
if (!wholeMask) return text;

for (let maskIndex = 0; maskIndex < wholeMask.length; maskIndex += 1) {
const mask = wholeMask.charAt(maskIndex);
switch (mask) {
case '#':
break;
case 'A':
break;
case '?':
break;
case 'N':
break;
case 'X':
break;
default:
text = text.replace(mask, '');
}
}

return text;
}

/**
* Prepares data.
*
* @param {String} text String to prepare (input value)
* @param {String} [wholeMask] Mask format, like `####-##`
* @returns {string} Prepared text
*/
export const prepare = (text, wholeMask) => {
if (!wholeMask) return text;

const maskStartRegExp = /^([^#ANX]+)/;

if (+text.length === 1 && maskStartRegExp.test(wholeMask)) {
text = maskStartRegExp.exec(wholeMask)[0] + text;
}

return text;
}

0 comments on commit 07aae69

Please sign in to comment.