Skip to content

Commit

Permalink
work for #4842 update getUnmaskedValue function
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaLarina committed Nov 21, 2023
1 parent 9a0e5b4 commit 4c84db0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
21 changes: 5 additions & 16 deletions src/mask/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,25 @@ 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;
}
}
}
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;
return this.getMaskedString(this.getUnmaskedValue(str, false));
}
}
34 changes: 25 additions & 9 deletions tests/mask_pattern_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4c84db0

Please sign in to comment.