-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work for #4842 add unit tests for pattern mask
- Loading branch information
OlgaLarina
committed
Nov 21, 2023
1 parent
eb1a071
commit 9a0e5b4
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |