react-native-mask-input
Version:
TextInput with mask for ReactNative on both iOS and Android. Includes obfuscation characters feature.
32 lines (28 loc) • 1.09 kB
JavaScript
export default function createNumberMask(props) {
const {
delimiter = '.',
precision = 2,
prefix = [],
separator = ','
} = props || {};
return value => {
const numericValue = (value === null || value === void 0 ? void 0 : value.replace(/\D+/g, '')) || '';
let mask = numericValue.split('').map(() => /\d/);
const shouldAddSeparatorOnMask = precision > 0 && !!separator;
if (mask.length > precision && shouldAddSeparatorOnMask) {
mask.splice(-precision, 0, separator);
}
const amountOfDelimiters = Math.ceil((numericValue.length - precision) / 3) - 1;
if (delimiter) {
for (let i = 0; i < amountOfDelimiters; i++) {
const precisionOffset = precision;
const separatorOffset = shouldAddSeparatorOnMask ? 1 : 0;
const thousandOffset = 3 + (delimiter ? 1 : 0);
const delimiterPosition = -precisionOffset - separatorOffset - i * thousandOffset - 3;
mask.splice(delimiterPosition, 0, delimiter);
}
}
return [...prefix, ...mask];
};
}
//# sourceMappingURL=createNumberMask.js.map