Skip to content

Commit

Permalink
work for #4842 add syntacticAnalysisMask function
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaLarina committed Nov 29, 2023
1 parent 84d72fa commit 1dabc93
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 10 deletions.
75 changes: 74 additions & 1 deletion src/mask/mask_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,40 @@ interface IMaskedValue {

export var settings = {
placeholderChar: "_",
escapedChar: "\\",
definitions: <{ [key: string]: RegExp }>{
"9": /[0-9]/,
"a": /[a-zA-Z]/,
"*": /[a-zA-Z0-9]/
}
};

export function getMaskedValueByPattern(str: string, pattern: string, matchWholeMask = true): string {
interface IMaskLiteral {
type: "const" | "regex";
value: any;
}

export function syntacticAnalysisMask(mask: string): Array<IMaskLiteral> {
const result: Array<IMaskLiteral> = [];
let prevChartIsEscaped = false;
const definitionsKeys = Object.keys(settings.definitions);

for(let index = 0; index < mask.length; index++) {
const currentChar = mask[index];
if(currentChar === settings.escapedChar) {
prevChartIsEscaped = true;
} else if(prevChartIsEscaped) {
prevChartIsEscaped = false;
result.push({ type: "const", value: currentChar });
} else {
result.push({ type: definitionsKeys.indexOf(currentChar) !== -1 ? "regex" : "const", value: currentChar });
}
}

return result;
}

export function getMaskedValueByPatternOld(str: string, pattern: string, matchWholeMask = true): string {
let result = "";
let strIndex = 0;
for(let maskIndex = 0; maskIndex < pattern.length; maskIndex++) {
Expand All @@ -33,10 +59,57 @@ export function getMaskedValueByPattern(str: string, pattern: string, matchWhole
return result;
}

export function getMaskedValueByPattern(str: string, pattern: string, matchWholeMask = true): string {
let result = "";
let strIndex = 0;

const parsedMask = syntacticAnalysisMask(pattern);
for(let maskIndex = 0; maskIndex < parsedMask.length; maskIndex++) {
if(parsedMask[maskIndex].type === "regex") {
const currentDefinition = settings.definitions[parsedMask[maskIndex].value];
if(strIndex < str.length && str[strIndex].match(currentDefinition)) {
result += str[strIndex];
} else if(matchWholeMask) {
result += settings.placeholderChar;
} else {
break;
}
strIndex++;
} else if(parsedMask[maskIndex].type === "const") {
result += parsedMask[maskIndex].value;
if(parsedMask[maskIndex].value === str[strIndex]) {
strIndex++;
}
}
}
return result;
}

export function getUnmaskedValueByPattern(str: string, pattern: string, matchWholeMask: boolean): string {
let result = "";
if(!str) return result;

const parsedMask = syntacticAnalysisMask(pattern);
for(let index = 0; index < parsedMask.length; index++) {
if(parsedMask[index].type === "regex") {
const currentDefinition = settings.definitions[parsedMask[index].value];
if(!!str[index] && str[index].match(currentDefinition)) {
result += str[index];
} else if(matchWholeMask) {
result = "";
break;
} else {
break;
}
}
}
return result;
}

export function getUnmaskedValueByPatternOld(str: string, pattern: string, matchWholeMask: boolean): string {
let result = "";
if(!str) return result;

for(let index = 0; index < pattern.length; index++) {
const currentDefinition = settings.definitions[pattern[index]];
if(currentDefinition) {
Expand Down
45 changes: 36 additions & 9 deletions tests/mask_pattern_tests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { processValueWithPattern, getMaskedValueByPattern, getUnmaskedValueByPattern } from "../src/mask/mask_utils";
import { syntacticAnalysisMask, processValueWithPattern, getMaskedValueByPattern, getUnmaskedValueByPattern, settings } from "../src/mask/mask_utils";

export default QUnit.module("Pattern mask");

const mask = "+9(999)-999-99-99";

QUnit.test("get masked valid text", function(assert) {
QUnit.test("parsingMask simple pattern", function(assert) {
let result = syntacticAnalysisMask(mask);
assert.equal(result.length, 17);
assert.equal(result[0].type, "const");
assert.equal(result[0].value, "+");
assert.equal(result[1].type, "regex");
assert.equal(result[1].value, "9");
assert.equal(result[2].type, "const");
assert.equal(result[2].value, "(");
assert.equal(result[3].type, "regex");
assert.equal(result[3].value, "9");
});

QUnit.test("parsingMask with fixed character", function(assert) {
let result = syntacticAnalysisMask("+\\9(999)-999-99-99");
assert.equal(result.length, 17);
assert.equal(result[0].type, "const");
assert.equal(result[0].value, "+");
assert.equal(result[1].type, "const");
assert.equal(result[1].value, "9");
assert.equal(result[2].type, "const");
assert.equal(result[2].value, "(");
assert.equal(result[3].type, "regex");
assert.equal(result[3].value, "9");
});

QUnit.test("get masked valid text matchWholeMask = true", function(assert) {
settings.placeholderChar = "*";
assert.equal(getMaskedValueByPattern("", mask, true), "+*(***)-***-**-**");
assert.equal(getMaskedValueByPattern("1", mask, true), "+1(***)-***-**-**");
Expand All @@ -14,23 +40,24 @@ QUnit.test("get masked valid text", function(assert) {
settings.placeholderChar = "_";
});

QUnit.test("get masked valid text", function(assert) {
QUnit.test("get masked valid text matchWholeMask = false", function(assert) {
settings.placeholderChar = "*";
const customMask = "+1(999)-999-99-99";
assert.equal(getMaskedValueByPattern("", customMask, false), "+1(");
assert.equal(getMaskedValueByPattern("1", customMask, false), "+1(1");
assert.equal(getMaskedValueByPattern("123", customMask, false), "+1(123)-");
assert.equal(getMaskedValueByPattern("123456", customMask, false), "+1(123)-456-");
assert.equal(getMaskedValueByPattern("12345678910", customMask, false), "+1(123)-456-78-91");
assert.equal(getMaskedValueByPattern("1", customMask, false), "+1(");
assert.equal(getMaskedValueByPattern("1234", customMask, false), "+1(234)-");
assert.equal(getMaskedValueByPattern("123456", customMask, false), "+1(234)-56");
assert.equal(getMaskedValueByPattern("123456789101", customMask, false), "+1(234)-567-89-10");
settings.placeholderChar = "_";
});

QUnit.test("get masked valid text", function(assert) {
QUnit.test("get masked valid text with fixed character", function(assert) {
settings.placeholderChar = "*";
const customMask = "+\\9(999)-999-99-99";
assert.equal(getMaskedValueByPattern("", customMask, true), "+9(***)-***-**-**");
assert.equal(getMaskedValueByPattern("9", customMask, true), "+9(***)-***-**-**");
assert.equal(getMaskedValueByPattern("1234", customMask, true), "+9(123)-***-**-**");
assert.equal(getMaskedValueByPattern("123", customMask, true), "+9(123)-***-**-**");
assert.equal(getMaskedValueByPattern("9123", customMask, true), "+9(123)-***-**-**");
assert.equal(getMaskedValueByPattern("1234567891", customMask, true), "+9(123)-456-78-91");
settings.placeholderChar = "_";
});
Expand Down

0 comments on commit 1dabc93

Please sign in to comment.