Skip to content

Commit

Permalink
work for #4842
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaLarina committed Feb 19, 2024
1 parent 75feda8 commit 1185d28
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
15 changes: 11 additions & 4 deletions src/mask/mask_number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ export class InputMaskNumber extends InputMaskBase {

public parseNumber(src: string | number): INumericalComposition {
const result: INumericalComposition = { integralPart: "", fractionalPart: "", decimalSeparatorCount: 0, isNegative: false };
const input = (src === undefined || src === null) ? "" : src.toString();
let input = (src === undefined || src === null) ? "" : src.toString();
if(typeof src === "number") {
input = src.toString().replace(".", this.decimalSeparator);
}
let minusCharCount = 0;

for(let inputIndex = 0; inputIndex < input.length; inputIndex++) {
Expand Down Expand Up @@ -154,7 +157,7 @@ export class InputMaskNumber extends InputMaskBase {
}

public getNumberMaskedValue(src: string | number, matchWholeMask: boolean = false): string {
const input = (src === undefined || src === null) ? "" : src.toString();
const input = (src === undefined || src === null) ? "" : src;
const parsedNumber = this.parseNumber(input);
const displayText = this.displayNumber(parsedNumber, true, matchWholeMask);
return displayText;
Expand All @@ -168,8 +171,8 @@ export class InputMaskNumber extends InputMaskBase {
public getMaskedValue(src: string): string {
return this.getNumberMaskedValue(src, true);
}
public getUnmaskedValue(src: string): string {
return this.getNumberUnmaskedValue(src).toString();
public getUnmaskedValue(src: string): any {
return this.getNumberUnmaskedValue(src);
}
public processInput(args: ITextMaskInputArgs): IMaskedValue {
const result = { text: args.prevValue, cursorPosition: args.selectionEnd, cancelPreventDefault: false };
Expand All @@ -194,6 +197,10 @@ export class InputMaskNumber extends InputMaskBase {
return "numbermask";
}

protected isPropertyEmpty(value: any): boolean {
return value === "" || value === undefined || value === null;
}

public isEmpty(): boolean {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mask/mask_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ITextMaskInputArgs {

export interface IInputMaskType {
getMaskedValue(src: string): string;
getUnmaskedValue(src: string): string;
getUnmaskedValue(src: string): any;
processInput(args: ITextMaskInputArgs): IMaskedValue;
isEmpty(): boolean;
}
10 changes: 5 additions & 5 deletions tests/mask/mask_currency_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ QUnit.test("get currency masked not allow negative value by formated text", func

QUnit.test("get currency unmasked valid text", function(assert) {
const maskInstance = new InputMaskCurrency();
assert.equal(maskInstance.getUnmaskedValue("$ 123"), 123);
assert.equal(maskInstance.getUnmaskedValue("$ 123,456"), 123456);
assert.equal(maskInstance.getUnmaskedValue("$ 123,456.78"), 123456.78);
assert.equal(maskInstance.getUnmaskedValue("$ 123,456.789"), 123456.78);
assert.equal(maskInstance.getUnmaskedValue("$ 123,456,789,101.12"), 123456789101.12);
assert.ok(maskInstance.getUnmaskedValue("$ 123") === 123);
assert.ok(maskInstance.getUnmaskedValue("$ 123,456") === 123456);
assert.ok(maskInstance.getUnmaskedValue("$ 123,456.78") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("$ 123,456.789") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("$ 123,456,789,101.12") === 123456789101.12);
});

QUnit.test("currency processInput: insert characters v2.0", function(assert) {
Expand Down
80 changes: 53 additions & 27 deletions tests/mask/mask_number_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,21 @@ QUnit.test("get numeric masked not allow negative value by formated text", funct

QUnit.test("get numeric unmasked valid text", function(assert) {
const maskInstance = new InputMaskNumber();
assert.equal(maskInstance.getUnmaskedValue("123"), 123);
assert.equal(maskInstance.getUnmaskedValue("123,456"), 123456);
assert.equal(maskInstance.getUnmaskedValue("123,456.78"), 123456.78);
assert.equal(maskInstance.getUnmaskedValue("123,456.789"), 123456.78);
assert.equal(maskInstance.getUnmaskedValue("123,456,789,101.12"), 123456789101.12);
assert.ok(maskInstance.getUnmaskedValue("123") === 123);
assert.ok(maskInstance.getUnmaskedValue("123,456") === 123456);
assert.ok(maskInstance.getUnmaskedValue("123,456.78") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("123,456.789") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("123,456,789,101.12") === 123456789101.12);
});

QUnit.test("get numeric unmasked valid text custom settings", function(assert) {
const maskInstance = new InputMaskNumber();
maskInstance.setData({ "decimalSeparator": ",", "thousandsSeparator": "." });
assert.equal(maskInstance.getUnmaskedValue("123"), 123);
assert.equal(maskInstance.getUnmaskedValue("123.456"), 123456);
assert.equal(maskInstance.getUnmaskedValue("123.456,78"), 123456.78);
assert.equal(maskInstance.getUnmaskedValue("123.456,789"), 123456.78);
assert.equal(maskInstance.getUnmaskedValue("123.456.789.101,12"), 123456789101.12);
assert.ok(maskInstance.getUnmaskedValue("123") === 123);
assert.ok(maskInstance.getUnmaskedValue("123.456") === 123456);
assert.ok(maskInstance.getUnmaskedValue("123.456,78") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("123.456,789") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("123.456.789.101,12") === 123456789101.12);
});

QUnit.test("numeric processInput: insert characters", function(assert) {
Expand Down Expand Up @@ -214,23 +214,6 @@ QUnit.test("numeric processInput: insert characters", function(assert) {
assert.equal(result.cursorPosition, 2, "type #7.1");
});

QUnit.skip("numeric with custom settings processInput: insert characters", function(assert) {
const maskInstance = new InputMaskNumber();
maskInstance.setData({
decimalSeparator: ",",
thousandsSeparator: " ",
precision: 3,
allowNegative: false,
});
let result = maskInstance.processInput({ insertedCharacters: "1", selectionStart: 1, selectionEnd: 1, prevValue: "0", inputDirection: "leftToRight" });
assert.equal(result.text, "1", "type #1");
assert.equal(result.cursorPosition, 1, "type #1");

result = maskInstance.processInput({ insertedCharacters: "4", selectionStart: 3, selectionEnd: 3, prevValue: "123", inputDirection: "leftToRight" });
assert.equal(result.text, "1 234", "type #2.0");
assert.equal(result.cursorPosition, 5, "type #2.0");
});

QUnit.test("numeric processInput simple number: delete characters", function(assert) {
const maskInstance = new InputMaskNumber();

Expand Down Expand Up @@ -563,4 +546,47 @@ QUnit.test("Deserialize InputMaskNumber properties", function (assert) {
assert.equal(maskSettings.allowNegative, true, "numbermask allowNegative");
assert.equal(maskSettings.min, 0, "numbermask min");
assert.equal(maskSettings.max, 1000, "numbermask max");
});

const customMaskSettings = {
decimalSeparator: ",",
thousandsSeparator: " ",
precision: 3,
allowNegative: false,
};
QUnit.test("parseNumber with custom settings", assert => {
const maskInstance = new InputMaskNumber();
maskInstance.setData(customMaskSettings);

assert.equal(maskInstance.parseNumber(123.45).integralPart, 123);
assert.equal(maskInstance.parseNumber(123.45).fractionalPart, 45);
assert.equal(maskInstance.parseNumber("123,45").integralPart, 123);
assert.equal(maskInstance.parseNumber("123,45").fractionalPart, 45);

assert.equal(maskInstance.parseNumber(",45").integralPart, 0);
assert.equal(maskInstance.parseNumber(",45").fractionalPart, 45);
assert.equal(maskInstance.parseNumber("123,").integralPart, 123);
assert.equal(maskInstance.parseNumber("123,").fractionalPart, 0);
});

QUnit.test("get numeric masked value with custom settings", function(assert) {
const maskInstance = new InputMaskNumber();
maskInstance.setData(customMaskSettings);
assert.equal(maskInstance.getMaskedValue(123456.78 as any), "123 456,78");
assert.equal(maskInstance.getMaskedValue(123456.789 as any), "123 456,789");

assert.equal(maskInstance.getMaskedValue("123456,78"), "123 456,78");
assert.equal(maskInstance.getMaskedValue("123456,789"), "123 456,789");
});

QUnit.test("numeric with custom settings processInput: insert characters", function(assert) {
const maskInstance = new InputMaskNumber();
maskInstance.setData(customMaskSettings);
let result = maskInstance.processInput({ insertedCharacters: "1", selectionStart: 1, selectionEnd: 1, prevValue: "0", inputDirection: "leftToRight" });
assert.equal(result.text, "1", "type #1");
assert.equal(result.cursorPosition, 1, "type #1");

result = maskInstance.processInput({ insertedCharacters: "4", selectionStart: 3, selectionEnd: 3, prevValue: "123", inputDirection: "leftToRight" });
assert.equal(result.text, "1 234", "type #2.0");
assert.equal(result.cursorPosition, 5, "type #2.0");
});

0 comments on commit 1185d28

Please sign in to comment.