@w.dental/date-mask
Version:
Lib for using as maks for date input
27 lines (21 loc) • 816 B
JavaScript
/*!
* @w.dental/date-mask v0.0.2
* (c) W-Dental
* Released under the MIT License.
*/
;
var LETTERS_REGEX = /[a-z]/gi;
var ONLY_NUMBERS_REGEX = /\D/gi;
function dateMask(num) {
if (num === void 0) {
num = '';
}
if (typeof num !== 'string' || LETTERS_REGEX.test(num)) {
throw new Error('You must to pass a digits as string with pattern DD/MM/YYYY or MM/DD/YYYY');
}
var numString = num.replace(ONLY_NUMBERS_REGEX, '');
if (numString.length === 3) return numString.replace(/(\d{2})/, '$1/');
if (numString.length > 3 && numString.length < 5) return numString.replace(/(\d{2})(\d+)/, '$1/$2');
if (numString.length >= 5 && numString.length <= 8) return numString.replace(/(\d{2})(\d{2})(\d+)/, '$1/$2/$3');else return numString.substring(0, 10);
}
module.exports = dateMask;