max-mask
Version:
Simple masking solution for any frontend as well as nodejs. Provides composable for Vue3
40 lines (39 loc) • 1.14 kB
JavaScript
// src/maskInput.ts
import { isForeverModeSymbol, isValidFormatSymbol, applyMasking, } from "./commonUtils.js";
export const maskInput = (input, format) => {
let formatted = "";
let i = 0;
let j = 0;
let foreverMode = false;
let lastFormatChar = null;
while (i < input.length && (j < format.length || foreverMode)) {
const formatChar = foreverMode ? lastFormatChar : format[j];
const inputChar = input[i];
if (isForeverModeSymbol(formatChar)) {
foreverMode = true;
j++;
continue;
}
if (isValidFormatSymbol(formatChar)) {
lastFormatChar = formatChar;
const maskedChar = applyMasking(inputChar, formatChar);
if (maskedChar !== null) {
formatted += maskedChar;
i++;
}
else {
break;
}
}
else {
formatted += formatChar;
if (formatChar === inputChar) {
i++;
}
}
if (!foreverMode) {
j++;
}
}
return formatted;
};