-
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.
- Loading branch information
OlgaLarina
committed
Feb 9, 2024
1 parent
b0c6330
commit ba37774
Showing
7 changed files
with
132 additions
and
54 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,54 @@ | ||
import { InputMaskBase } from "./mask_base"; | ||
import { MaskManagerType } from "./mask_manager"; | ||
import { IMaskSettings } from "./mask_settings"; | ||
|
||
export interface IDateTimeMaskLiteral { | ||
type: "month" | "day" | "year" | "separator"; | ||
value: any; | ||
length: number; | ||
} | ||
|
||
export function getDateTimeLiterals(mask: string): Array<IDateTimeMaskLiteral> { | ||
const result: Array<IDateTimeMaskLiteral> = []; | ||
let prevLiteralType: string; | ||
|
||
const createOrUpdateLiteral = (currentLiteralType: "month" | "day" | "year" | "separator", currentChar: string) => { | ||
if(!!prevLiteralType && prevLiteralType === currentLiteralType) { | ||
result[result.length - 1].length++; | ||
} else { | ||
result.push({ type: currentLiteralType, value: currentChar, length: 1 }); | ||
} | ||
}; | ||
|
||
for(let index = 0; index < mask.length; index++) { | ||
const currentChar = mask[index]; | ||
switch (currentChar) { | ||
case "m": | ||
createOrUpdateLiteral("month", "m"); | ||
break; | ||
case "d": | ||
createOrUpdateLiteral("day", "d"); | ||
break; | ||
case "y": | ||
createOrUpdateLiteral("year", "y"); | ||
break; | ||
default: | ||
result.push({ type: "separator", value: currentChar, length: 1 }); | ||
break; | ||
} | ||
prevLiteralType = result[result.length - 1].type; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export class InputMaskDateTime extends InputMaskBase { | ||
// public processInput(args: ITextMaskInputArgs): IMaskedValue { | ||
// return { text: args.prevValue, cursorPosition: args.selectionEnd, cancelPreventDefault: false }; | ||
// } | ||
|
||
public getUnmaskedValue(src: string): string { return src; } | ||
public getMaskedValue(src: string): string { return src; } | ||
} | ||
|
||
MaskManagerType.Instance.registerMaskManagerType("datetime", (maskOptions: IMaskSettings) => { return new InputMaskDateTime(maskOptions); }); |
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
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
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,55 @@ | ||
import { InputMaskDateTime, getDateTimeLiterals } from "../../src/mask/mask_datetime"; | ||
import { settings } from "../../src/mask/mask_utils"; | ||
|
||
export default QUnit.module("Datetime mask"); | ||
|
||
QUnit.test("getDateTimeLiterals simple pattern", function(assert) { | ||
let result = getDateTimeLiterals("m/d/yy"); | ||
assert.equal(result.length, 5); | ||
assert.equal(result[0].type, "month"); | ||
assert.equal(result[0].value, "m"); | ||
assert.equal(result[0].length, 1); | ||
assert.equal(result[1].type, "separator"); | ||
assert.equal(result[1].value, "/"); | ||
assert.equal(result[1].length, 1); | ||
assert.equal(result[2].type, "day"); | ||
assert.equal(result[2].value, "d"); | ||
assert.equal(result[2].length, 1); | ||
assert.equal(result[3].type, "separator"); | ||
assert.equal(result[3].value, "/"); | ||
assert.equal(result[3].length, 1); | ||
assert.equal(result[4].type, "year"); | ||
assert.equal(result[4].value, "y"); | ||
assert.equal(result[4].length, 2); | ||
}); | ||
|
||
QUnit.test("getDateTimeLiterals simple pattern", function(assert) { | ||
let result = getDateTimeLiterals("mm/dd/yyyy"); | ||
assert.equal(result.length, 5); | ||
assert.equal(result[0].type, "month"); | ||
assert.equal(result[0].value, "m"); | ||
assert.equal(result[0].length, 2); | ||
assert.equal(result[1].type, "separator"); | ||
assert.equal(result[1].value, "/"); | ||
assert.equal(result[1].length, 1); | ||
assert.equal(result[2].type, "day"); | ||
assert.equal(result[2].value, "d"); | ||
assert.equal(result[2].length, 2); | ||
assert.equal(result[3].type, "separator"); | ||
assert.equal(result[3].value, "/"); | ||
assert.equal(result[3].length, 1); | ||
assert.equal(result[4].type, "year"); | ||
assert.equal(result[4].value, "y"); | ||
assert.equal(result[4].length, 4); | ||
}); | ||
|
||
QUnit.skip("get masked valid date text matchWholeMask = true", function(assert) { | ||
settings.placeholderChar = "*"; | ||
const maskInstance = new InputMaskDateTime({ type: "datetime", mask: "mm/dd/yyyy" }); | ||
assert.equal(maskInstance.getMaskedValue(""), "**/**/****"); | ||
assert.equal(maskInstance.getMaskedValue("1"), "1*/**/****"); | ||
assert.equal(maskInstance.getMaskedValue("1234"), "12/3*/****"); | ||
assert.equal(maskInstance.getMaskedValue("1230198"), "12/30/567*"); | ||
assert.equal(maskInstance.getMaskedValue("12301987"), "12/30/1987"); | ||
settings.placeholderChar = "_"; | ||
}); |