From 4c84db03c2c7b67b6e6e5ed720ef3d5aeb65cbff Mon Sep 17 00:00:00 2001 From: OlgaLarina Date: Tue, 21 Nov 2023 18:08:56 +0300 Subject: [PATCH] work for #4842 update getUnmaskedValue function --- src/mask/mask.ts | 21 +++++---------------- tests/mask_pattern_tests.ts | 34 +++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/mask/mask.ts b/src/mask/mask.ts index 971ada38c3..6f43670cbe 100644 --- a/src/mask/mask.ts +++ b/src/mask/mask.ts @@ -28,16 +28,18 @@ export class InputMaskBase { return result; } - getUnmaskedValue(str: string): string { + getUnmaskedValue(str: string, matchWholeMask: boolean): 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 { + } else if(matchWholeMask) { result = ""; break; + } else { + break; } } } @@ -45,19 +47,6 @@ export class InputMaskBase { } 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; + return this.getMaskedString(this.getUnmaskedValue(str, false)); } } \ No newline at end of file diff --git a/tests/mask_pattern_tests.ts b/tests/mask_pattern_tests.ts index 0233471895..2b0f22d772 100644 --- a/tests/mask_pattern_tests.ts +++ b/tests/mask_pattern_tests.ts @@ -23,25 +23,41 @@ QUnit.test("get masked invalid text", function(assert) { assert.equal(inputMask.getMaskedString("123456789101112"), "+1(234)-567-89-10"); }); -QUnit.test("get unmasked value", function(assert) { +QUnit.test("get unmasked value, matchWholeMask is true", 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"); + assert.equal(inputMask.getUnmaskedValue("+_(___)-___-__-__", true), ""); + assert.equal(inputMask.getUnmaskedValue("+1(234)-567-__-__", true), ""); + assert.equal(inputMask.getUnmaskedValue("+1(234)-567-89-10", true), "12345678910"); }); -QUnit.test("get unmasked invalid value", function(assert) { +QUnit.test("get unmasked invalid value, matchWholeMask is true", 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"), ""); + assert.equal(inputMask.getUnmaskedValue("+.(___)-___-__-__", true), ""); + assert.equal(inputMask.getUnmaskedValue("+a(bcd)-567-__-__", true), ""); + assert.equal(inputMask.getUnmaskedValue("++(234)-567-89-10", true), ""); + assert.equal(inputMask.getUnmaskedValue("+1(234)-567-__-10", true), ""); +}); + +QUnit.test("get unmasked value, matchWholeMask is false", function(assert) { + const inputMask = new InputMaskBase(mask); + assert.equal(inputMask.getUnmaskedValue("+_(___)-___-__-__", false), ""); + assert.equal(inputMask.getUnmaskedValue("+1(234)-567-__-__", false), "1234567"); + assert.equal(inputMask.getUnmaskedValue("+1(234)-567-89-10", false), "12345678910"); +}); + +QUnit.test("get unmasked invalid value, matchWholeMask is false", function(assert) { + const inputMask = new InputMaskBase(mask); + assert.equal(inputMask.getUnmaskedValue("+.(___)-___-__-__", false), ""); + assert.equal(inputMask.getUnmaskedValue("+a(bcd)-567-__-__", false), ""); + assert.equal(inputMask.getUnmaskedValue("++(234)-567-89-10", false), ""); + assert.equal(inputMask.getUnmaskedValue("+1(234)-567-__-10", false), "1234567"); }); 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_-__-__"), 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);