diff --git a/src/entries/chunks/model.ts b/src/entries/chunks/model.ts index 742c3715d7..7b36bea528 100644 --- a/src/entries/chunks/model.ts +++ b/src/entries/chunks/model.ts @@ -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"; diff --git a/src/mask/mask.ts b/src/mask/mask.ts new file mode 100644 index 0000000000..971ada38c3 --- /dev/null +++ b/src/mask/mask.ts @@ -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; + } +} \ No newline at end of file diff --git a/tests/entries/test.ts b/tests/entries/test.ts index ac3ea7def4..44b5fdeb38 100644 --- a/tests/entries/test.ts +++ b/tests/entries/test.ts @@ -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"; diff --git a/tests/mask_pattern_tests.ts b/tests/mask_pattern_tests.ts new file mode 100644 index 0000000000..0233471895 --- /dev/null +++ b/tests/mask_pattern_tests.ts @@ -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); +});