-
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
Dec 1, 2023
1 parent
1dabc93
commit 5d019a0
Showing
8 changed files
with
274 additions
and
115 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,86 @@ | ||
import { InputMaskBase } from "./mask"; | ||
import { IMaskedValue, settings, syntacticAnalysisMask } from "./mask_utils"; | ||
|
||
export function getMaskedValueByPattern(str: string, pattern: string, matchWholeMask = true): string { | ||
let result = ""; | ||
let strIndex = 0; | ||
|
||
const parsedMask = syntacticAnalysisMask(pattern); | ||
for(let maskIndex = 0; maskIndex < parsedMask.length; maskIndex++) { | ||
if(parsedMask[maskIndex].type === "regex") { | ||
const currentDefinition = settings.definitions[parsedMask[maskIndex].value]; | ||
if(strIndex < str.length && str[strIndex].match(currentDefinition)) { | ||
result += str[strIndex]; | ||
} else if(matchWholeMask) { | ||
result += settings.placeholderChar; | ||
} else { | ||
break; | ||
} | ||
strIndex++; | ||
} else if(parsedMask[maskIndex].type === "const") { | ||
result += parsedMask[maskIndex].value; | ||
if(parsedMask[maskIndex].value === str[strIndex]) { | ||
strIndex++; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
export function getUnmaskedValueByPattern(str: string, pattern: string, matchWholeMask: boolean): string { | ||
let result = ""; | ||
if(!str) return result; | ||
|
||
const parsedMask = syntacticAnalysisMask(pattern); | ||
for(let index = 0; index < parsedMask.length; index++) { | ||
if(parsedMask[index].type === "regex") { | ||
const currentDefinition = settings.definitions[parsedMask[index].value]; | ||
if(!!str[index] && str[index].match(currentDefinition)) { | ||
result += str[index]; | ||
} else if(matchWholeMask) { | ||
result = ""; | ||
break; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
export function processValueWithPattern(str: string, pattern: string, prevСursorPosition: number, currentCursorPosition: number): IMaskedValue { | ||
let result = ""; | ||
if(!str) return <IMaskedValue>{ text: result, cursorPosition: currentCursorPosition }; | ||
let leftPartResult = ""; | ||
let rigthPartResult = ""; | ||
let centerPart = ""; | ||
let newCursorPosition = currentCursorPosition; | ||
|
||
const leftPartRange = Math.min(prevСursorPosition, currentCursorPosition, pattern.length - 1); | ||
leftPartResult = getUnmaskedValueByPattern(str.substring(0, leftPartRange), pattern.substring(0, leftPartRange), false); | ||
rigthPartResult = getUnmaskedValueByPattern(str.substring(currentCursorPosition), pattern.substring(prevСursorPosition), false); | ||
if(currentCursorPosition > prevСursorPosition) { | ||
centerPart = getUnmaskedValueByPattern(str.substring(leftPartRange, currentCursorPosition), pattern.substring(leftPartRange), false); | ||
newCursorPosition = getMaskedValueByPattern(leftPartResult + centerPart, pattern, false).length; | ||
|
||
} | ||
result = getMaskedValueByPattern(leftPartResult + centerPart + rigthPartResult, pattern); | ||
return <IMaskedValue>{ text: result, cursorPosition: newCursorPosition }; | ||
} | ||
|
||
export class InputMaskPattern extends InputMaskBase { | ||
protected getMaskedValue(mask: string, option?: any): string { | ||
return getMaskedValueByPattern(getUnmaskedValueByPattern(this.input.value, mask, false), mask); | ||
} | ||
protected processMaskedValue(mask: string): IMaskedValue { | ||
return processValueWithPattern(this.input.value, mask, this._prevSelectionStart, this.input.selectionStart); | ||
} | ||
|
||
protected updateMaskedString(mask: string, option?: any): void { | ||
if(!!this.input) { | ||
const result = this.processMaskedValue(mask); | ||
this.input.value = result.text; | ||
this.input.setSelectionRange(result.cursorPosition, result.cursorPosition); | ||
} | ||
} | ||
} |
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,68 @@ | ||
import { InputMaskBase } from "./mask"; | ||
import { IMaskedValue, settings, syntacticAnalysisMask } from "./mask_utils"; | ||
|
||
interface INumberMaskOption { | ||
mask?: string; | ||
align?: "left" | "right"; | ||
allowNegative?: boolean; | ||
decimal?: string; | ||
precision?: number; | ||
thousands?: string; | ||
} | ||
interface INumericalСomposition { | ||
integralPart: number; | ||
fractionalPart?: number; | ||
} | ||
|
||
export function parseNumber(str: any): INumericalСomposition { | ||
const result: INumericalСomposition = { integralPart: 0, fractionalPart: 0 }; | ||
const input = str.toString(); | ||
|
||
const parts = input.trim().split("."); | ||
if(parts.length >= 2) { | ||
result.integralPart = parseInt(parts[0].trim()); | ||
result.fractionalPart = parseInt(parts[1].trim()); | ||
} else if(parts.length == 1) { | ||
result.integralPart = parseInt(parts[0].trim()); | ||
} else { | ||
result.integralPart = parseInt(input.trim()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function getNumberMaskedValue(str: string | number, mask: string, option?: INumberMaskOption) { | ||
const decimalSeparator = option?.decimal || settings.numberOptions.decimal; | ||
const parsedMask = syntacticAnalysisMask(mask); | ||
|
||
const input = str.toString(); | ||
const parsedNumber = parseNumber(input); | ||
|
||
let result = ""; | ||
let maskIndex = 0; | ||
|
||
for(let index = 0; index < parsedNumber.integralPart.toString().length; index++) { | ||
|
||
} | ||
return result; | ||
} | ||
|
||
export function getNumberUnmaskedValue(str: string, mask: string, option?: INumberMaskOption) { | ||
return str; | ||
} | ||
|
||
export class InputMaskNumber extends InputMaskBase { | ||
|
||
constructor(input: HTMLInputElement, mask?: INumberMaskOption) { | ||
super(input, mask); | ||
} | ||
|
||
protected getMaskedValue(mask: string, option: INumberMaskOption): string { | ||
return getNumberMaskedValue(getNumberUnmaskedValue(this.input.value, mask, option), mask, option); | ||
} | ||
|
||
protected processMaskedValue(mask: string): IMaskedValue { | ||
// return processValueWithPattern(this.input.value, mask, this._prevSelectionStart, this.input.selectionStart); | ||
return { text: this.input.value, cursorPosition: this.input.selectionStart }; | ||
} | ||
} |
Oops, something went wrong.