-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaskfy.es5.js
1 lines (1 loc) · 2.19 KB
/
maskfy.es5.js
1
'use strict';function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}var Maskfy=function(){function a(b){return _classCallCheck(this,a),this.tag=b.tag,this.mask=b.mask,this.reverse=b.reverse,this.minSize=b.minSize,this.defaultValue=b.defaultValue,this.letters=b.letters,this.after=b.after,this.tag?void this.init():console.error(new Error('Maskfy: Insert tag selector required. Ex.: Maskfy({tag: "[data-maskfy]"})'))}return a.prototype.init=function init(){var b=this;[].forEach.call(document.querySelectorAll(this.tag),function(c){c.addEventListener('input',function(f){return b.format(f)});var d=c.value.trim();(''!==d||b.defaultValue)&&(c.value=c.value||c.dataset.maskfyValue||b.defaultValue),b.event(c)})},a.prototype.event=function event(b){if(window.navigator.msPointerEnabled){var c=document.createEvent('Event');c.initEvent('input',!1,!0),b.dispatchEvent(c)}else b.dispatchEvent(new Event('input'))},a.prototype.format=function format(b){var j=this,c=b.target.value,d=b.target.dataset.maskfy||this.mask,f='true'===b.target.dataset.maskfyReverse||this.reverse,g='true'===b.target.dataset.maskfyLetters||this.letters,h=void 0;if(!d)return console.error(new Error('Maskfy: Insert input mask required. Ex.: Maskfy({mask: "9999-99-99"})'));if(d.length>=c.length?h=c.replace(g?/\W/g:/\D/g,'').split(''):(h=c.split(''),h.pop(),b.target.selectionEnd!==c.length&&setTimeout(function(){return j.event(b.target)},10)),/^\W+/.test(d)&&d.length>=c.length&&h.unshift(/^\W+/.exec(d)[0]),f){var k=+b.target.dataset.maskfyMinsize||this.minSize,l=d.split(''),m=h.concat('');l.reverse(),m.reverse().shift(),k&&h.length<k?m.push('0'):'0'===m[m.length-1]&&/\d/.test(c[c.length-1])&&m.pop();for(var n=0;n<m.length;n++)/\W/.test(l[n])&&m.splice(n,0,l[n]);l.length>=m.length&&(h=m.reverse())}else for(var o=0;d.length>o&&d.length>=c.length;o++)isNaN(parseInt(d[o]))&&h[o-1]&&h.splice(o,0,d[o]);h.splice(d.length,h.length),b.target.value=h.join('').replace(/(\W+)$/,''),setTimeout(function(){b.target.classList.contains('maskfy--active')||(b.target.classList.add('maskfy--active'),j.event(b.target)),j.after&&j.formatAfter(b.target)},10)},a.prototype.formatAfter=function formatAfter(b){this.after(b,this)},a}();