Skip to content

Commit

Permalink
work for #4842 add unit tests for pattern mask
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaLarina committed Nov 21, 2023
1 parent eb1a071 commit 9a0e5b4
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/entries/chunks/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export {
sanitizeEditableContent,
IAttachKey2clickOptions
} from "../../utils/utils";
export { InputMaskBase } from "../../mask/mask";
export * from "../../utils/cssClassBuilder";

export { surveyCss, defaultV2Css, defaultV2ThemeName } from "../../defaultCss/defaultV2Css";
Expand Down
63 changes: 63 additions & 0 deletions src/mask/mask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export class InputMaskBase {
placeholderChar = "_";

definitions: { [key: string]: RegExp } = {
"9": /[0-9]/,
"a": /[a-zA-Z]/,
"*": /[a-zA-Z0-9]/
}

constructor(private mask: string) { }

getMaskedString(str: string): string {
let result = "";
let strIndex = 0;
for(let maskIndex = 0; maskIndex < this.mask.length; maskIndex++) {
const currentDefinition = this.definitions[this.mask[maskIndex]];
if(currentDefinition) {
if(strIndex < str.length && str[strIndex].match(currentDefinition)) {
result += str[strIndex];
} else {
result += this.placeholderChar;
}
strIndex++;
} else {
result += this.mask[maskIndex];
}
}
return result;
}

getUnmaskedValue(str: string): string {
let result = "";
for(let index = 0; index < this.mask.length; index++) {
const currentDefinition = this.definitions[this.mask[index]];
if(currentDefinition) {
if(str[index].match(currentDefinition)) {
result += str[index];
} else {
result = "";
break;
}
}
}
return result;
}

updateMaskedString(str: string): string {
let result = "";
for(let index = 0; index < this.mask.length; index++) {
const currentDefinition = this.definitions[this.mask[index]];
if(currentDefinition) {
if(str[index].match(currentDefinition)) {
result += str[index];
} else {
result += this.placeholderChar;
}
} else {
result += this.mask[index];
}
}
return result;
}
}
1 change: 1 addition & 0 deletions tests/entries/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export * from "../components/liststests";
export * from "../responsivityTests";
export * from "../svgRegistryTests";
export * from "../utilstests";
export * from "../mask_pattern_tests";
export * from "../stylesManagerTests";
export * from "../headerTests";

Expand Down
48 changes: 48 additions & 0 deletions tests/mask_pattern_tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { InputMaskBase } from "../src/mask/mask";

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

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

QUnit.test("get masked valid text", function(assert) {
const inputMask = new InputMaskBase(mask);
assert.equal(inputMask.getMaskedString(""), "+_(___)-___-__-__");
assert.equal(inputMask.getMaskedString("1"), "+1(___)-___-__-__");
assert.equal(inputMask.getMaskedString("1234"), "+1(234)-___-__-__");
assert.equal(inputMask.getMaskedString("1234567"), "+1(234)-567-__-__");
assert.equal(inputMask.getMaskedString("12345678910"), "+1(234)-567-89-10");
});

QUnit.test("get masked invalid text", function(assert) {
const resultMaskedText = "+_(___)-___-__-__";
const inputMask = new InputMaskBase(mask);
assert.equal(inputMask.getMaskedString(""), resultMaskedText);
assert.equal(inputMask.getMaskedString("a"), resultMaskedText);
assert.equal(inputMask.getMaskedString("@"), resultMaskedText);
assert.equal(inputMask.getMaskedString("."), resultMaskedText);
assert.equal(inputMask.getMaskedString("123456789101112"), "+1(234)-567-89-10");
});

QUnit.test("get unmasked value", function(assert) {
const inputMask = new InputMaskBase(mask);
assert.equal(inputMask.getUnmaskedValue("+_(___)-___-__-__"), "");
assert.equal(inputMask.getUnmaskedValue("+1(234)-567-__-__"), "");
assert.equal(inputMask.getUnmaskedValue("+1(234)-567-89-10"), "12345678910");
});

QUnit.test("get unmasked invalid value", function(assert) {
const inputMask = new InputMaskBase(mask);
assert.equal(inputMask.getUnmaskedValue("+.(___)-___-__-__"), "");
assert.equal(inputMask.getUnmaskedValue("+a(bcd)-567-__-__"), "");
assert.equal(inputMask.getUnmaskedValue("++(234)-567-89-10"), "");
assert.equal(inputMask.getUnmaskedValue("+1(234)-567-__-10"), "");
});

QUnit.test("update masked value", function(assert) {
const resultMaskedText = "+1(234)-567-__-__";
const inputMask = new InputMaskBase(mask);
assert.equal(inputMask.updateMaskedString("+1(234)-567-__-__"), resultMaskedText);
assert.equal(inputMask.updateMaskedString("+1(234)-567-ab-__"), resultMaskedText);
assert.equal(inputMask.updateMaskedString("+1(234)-567-.,-__"), resultMaskedText);
assert.equal(inputMask.updateMaskedString("+1(234)-567-!?-__"), resultMaskedText);
});

0 comments on commit 9a0e5b4

Please sign in to comment.