react-input-mask-plus
Version:
Masked input component for React
53 lines (48 loc) • 1.2 kB
JavaScript
import { defaultCharsRules, defaultMaskChar } from '../constants';
export default function (mask, maskChar, charsRules) {
if (maskChar === undefined) {
maskChar = defaultMaskChar;
}
if (charsRules == null) {
charsRules = defaultCharsRules;
}
if (!mask || typeof mask !== 'string') {
return {
maskChar: maskChar,
charsRules: charsRules,
mask: null,
prefix: null,
lastEditablePos: null,
permanents: []
};
}
var str = '';
var prefix = '';
var permanents = [];
var isPermanent = false;
var lastEditablePos = null;
mask.split('').forEach(function (character) {
if (!isPermanent && character === '\\') {
isPermanent = true;
} else {
if (isPermanent || !charsRules[character]) {
permanents.push(str.length);
if (str.length === permanents.length - 1) {
prefix += character;
}
} else {
lastEditablePos = str.length + 1;
}
str += character;
isPermanent = false;
}
});
return {
maskChar: maskChar,
charsRules: charsRules,
prefix: prefix,
mask: str,
lastEditablePos: lastEditablePos,
permanents: permanents
};
}