UNPKG

ngx-mask

Version:
2 lines 33.1 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/forms"),require("@angular/common")):"function"==typeof define&&define.amd?define("ngx-mask",["exports","@angular/core","@angular/forms","@angular/common"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-mask"]={},e.ng.core,e.ng.forms,e.ng.common)}(this,(function(e,t,i,r){"use strict";var s=new t.InjectionToken("config"),a=new t.InjectionToken("NEW_CONFIG"),n=new t.InjectionToken("INITIAL_CONFIG"),l={suffix:"",prefix:"",thousandSeparator:" ",decimalMarker:".",clearIfNotMatch:!1,showTemplate:!1,showMaskTyped:!1,placeHolderCharacter:"_",dropSpecialCharacters:!0,hiddenInput:void 0,shownMaskExpression:"",separatorLimit:"",allowNegativeNumbers:!1,validation:!0,specialCharacters:["-","/","(",")",".",":"," ","+",",","@","[","]",'"',"'"],leadZeroDateTime:!1,patterns:{0:{pattern:new RegExp("\\d")},9:{pattern:new RegExp("\\d"),optional:!0},X:{pattern:new RegExp("\\d"),symbol:"*"},A:{pattern:new RegExp("[a-zA-Z0-9]")},S:{pattern:new RegExp("[a-zA-Z]")},d:{pattern:new RegExp("\\d")},m:{pattern:new RegExp("\\d")},M:{pattern:new RegExp("\\d")},H:{pattern:new RegExp("\\d")},h:{pattern:new RegExp("\\d")},s:{pattern:new RegExp("\\d")}}},o=["Hh:m0:s0","Hh:m0","m0:s0"],h=["percent","Hh","s0","m0","separator","d0/M0/0000","d0/M0","d0","M0"],c=function(e,t){return(c=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])})(e,t)};function u(e,t,i,r){return new(i||(i=Promise))((function(s,a){function n(e){try{o(r.next(e))}catch(e){a(e)}}function l(e){try{o(r.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(n,l)}o((r=r.apply(e,t||[])).next())}))}function p(e,t){var i,r,s,a,n={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return a={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function l(a){return function(l){return function(a){if(i)throw new TypeError("Generator is already executing.");for(;n;)try{if(i=1,r&&(s=2&a[0]?r.return:a[0]?r.throw||((s=r.return)&&s.call(r),0):r.next)&&!(s=s.call(r,a[1])).done)return s;switch(r=0,s&&(a=[2&a[0],s.value]),a[0]){case 0:case 1:s=a;break;case 4:return n.label++,{value:a[1],done:!1};case 5:n.label++,r=a[1],a=[0];continue;case 7:a=n.ops.pop(),n.trys.pop();continue;default:if(!(s=n.trys,(s=s.length>0&&s[s.length-1])||6!==a[0]&&2!==a[0])){n=0;continue}if(3===a[0]&&(!s||a[1]>s[0]&&a[1]<s[3])){n.label=a[1];break}if(6===a[0]&&n.label<s[1]){n.label=s[1],s=a;break}if(s&&n.label<s[2]){n.label=s[2],n.ops.push(a);break}s[2]&&n.ops.pop(),n.trys.pop();continue}a=t.call(e,n)}catch(e){a=[6,e],r=0}finally{i=s=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}Object.create;function f(e,t){var i="function"==typeof Symbol&&e[Symbol.iterator];if(!i)return e;var r,s,a=i.call(e),n=[];try{for(;(void 0===t||t-- >0)&&!(r=a.next()).done;)n.push(r.value)}catch(e){s={error:e}}finally{try{r&&!r.done&&(i=a.return)&&i.call(a)}finally{if(s)throw s.error}}return n}Object.create;var m=function(){function e(e){var t=this;this._config=e,this.maskExpression="",this.actualValue="",this.shownMaskExpression="",this._formatWithSeparators=function(e,i,r,s){var a=e.split(r),n=a.length>1?""+r+a[1]:"",l=a[0],o=t.separatorLimit.replace(/\s/g,"");o&&+o&&(l="-"===l[0]?"-"+l.slice(1,l.length).slice(0,o.length):l.slice(0,o.length));for(var h=/(\d+)(\d{3})/;i&&h.test(l);)l=l.replace(h,"$1"+i+"$2");return void 0===s?l+n:0===s?l:l+n.substr(0,s+1)},this.percentage=function(e){return Number(e)>=0&&Number(e)<=100},this.getPrecision=function(e){var t=e.split(".");return t.length>1?Number(t[t.length-1]):1/0},this.checkAndRemoveSuffix=function(e){for(var i,r,s,a=(null===(i=t.suffix)||void 0===i?void 0:i.length)-1;a>=0;a--){var n=t.suffix.substr(a,null===(r=t.suffix)||void 0===r?void 0:r.length);if(e.includes(n)&&(a-1<0||!e.includes(t.suffix.substr(a-1,null===(s=t.suffix)||void 0===s?void 0:s.length))))return e.replace(n,"")}return e},this.checkInputPrecision=function(e,i,r){if(i<1/0){var s=new RegExp(t._charToRegExpExpression(r)+"\\d{"+i+"}.*$"),a=e.match(s);if(a&&a[0].length-1>i){var n=a[0].length-1-i;e=e.substring(0,e.length-n)}0===i&&e.endsWith(r)&&(e=e.substring(0,e.length-1))}return e},this._shift=new Set,this.clearIfNotMatch=this._config.clearIfNotMatch,this.dropSpecialCharacters=this._config.dropSpecialCharacters,this.maskSpecialCharacters=this._config.specialCharacters,this.maskAvailablePatterns=this._config.patterns,this.prefix=this._config.prefix,this.suffix=this._config.suffix,this.thousandSeparator=this._config.thousandSeparator,this.decimalMarker=this._config.decimalMarker,this.hiddenInput=this._config.hiddenInput,this.showMaskTyped=this._config.showMaskTyped,this.placeHolderCharacter=this._config.placeHolderCharacter,this.validation=this._config.validation,this.separatorLimit=this._config.separatorLimit,this.allowNegativeNumbers=this._config.allowNegativeNumbers,this.leadZeroDateTime=this._config.leadZeroDateTime}return e.prototype.applyMaskWithPattern=function(e,t){var i=f(t,2),r=i[0],s=i[1];return this.customPattern=s,this.applyMask(e,r)},e.prototype.applyMask=function(e,t,i,r,s,a){var n=this;if(void 0===i&&(i=0),void 0===r&&(r=!1),void 0===s&&(s=!1),void 0===a&&(a=function(){}),null==e||void 0===t)return"";var l=0,o="",h=!1,c=!1,u=1,p=!1;e.slice(0,this.prefix.length)===this.prefix&&(e=e.slice(this.prefix.length,e.length)),this.suffix&&(null==e?void 0:e.length)>0&&(e=this.checkAndRemoveSuffix(e));var f=e.toString().split("");"IP"===t&&(this.ipError=!!(f.filter((function(e){return"."===e})).length<3&&f.length<7),t="099.099.099.099");for(var m=[],d=0;d<e.length;d++)e[d].match("\\d")&&m.push(e[d]);if("CPF_CNPJ"===t&&(this.cpfCnpjError=!(11===m.length||14===m.length),t=m.length>11?"00.000.000/0000-00":"000.000.000-00"),t.startsWith("percent")){if(e.match("[a-z]|[A-Z]")||e.match(/[-!$%^&*()_+|~=`{}\[\]:";'<>?,\/.]/)){e=this._stripToDecimal(e);var k=this.getPrecision(t);e=this.checkInputPrecision(e,k,this.decimalMarker)}if(e.indexOf(".")>0&&!this.percentage(e.substring(0,e.indexOf(".")))){var g=e.substring(0,e.indexOf(".")-1);e=""+g+e.substring(e.indexOf("."),e.length)}o=this.percentage(e)?e:e.substring(0,e.length-1)}else if(t.startsWith("separator")){(e.match("[wа-яА-Я]")||e.match("[ЁёА-я]")||e.match("[a-z]|[A-Z]")||e.match(/[-@#!$%\\^&*()_£¬'+|~=`{}\[\]:";<>.?\/]/)||e.match("[^A-Za-z0-9,]"))&&(e=this._stripToDecimal(e)),e=e.length>1&&"0"===e[0]&&e[1]!==this.decimalMarker?e.slice(1,e.length):e;var _=this._charToRegExpExpression(this.thousandSeparator),v=this._charToRegExpExpression(this.decimalMarker),S='@#!$%^&*()_+|~=`{}\\[\\]:\\s,\\.";<>?\\/'.replace(_,"").replace(v,""),y=new RegExp("["+S+"]");e.match(y)&&(e=e.substring(0,e.length-1));k=this.getPrecision(t);var x=(e=this.checkInputPrecision(e,k,this.decimalMarker)).replace(new RegExp(_,"g"),""),C=(o=this._formatWithSeparators(x,this.thousandSeparator,this.decimalMarker,k)).indexOf(",")-e.indexOf(","),b=o.length-e.length;if(b>0&&","!==o[i]){c=!0;var E=0;do{this._shift.add(i+E),E++}while(E<b)}else 0!==C&&i>0&&!(o.indexOf(",")>=i&&i>3)||!(o.indexOf(".")>=i&&i>3)&&b<=0?(this._shift.clear(),c=!0,u=b,i+=b,this._shift.add(i)):this._shift.clear()}else{d=0;for(var V=f[0];d<f.length&&l!==t.length;V=f[++d])if(this._checkSymbolMask(V,t[l])&&"?"===t[l+1])o+=V,l+=2;else if("*"===t[l+1]&&h&&this._checkSymbolMask(V,t[l+2]))o+=V,l+=3,h=!1;else if(this._checkSymbolMask(V,t[l])&&"*"===t[l+1])o+=V,h=!0;else if("?"===t[l+1]&&this._checkSymbolMask(V,t[l+2]))o+=V,l+=3;else if(this._checkSymbolMask(V,t[l])){if("H"===t[l]&&Number(V)>2){l+=1,this._shiftStep(t,l,f.length),d--,this.leadZeroDateTime&&(o+="0");continue}if("h"===t[l]&&"2"===o&&Number(V)>3){l+=1,d--;continue}if("m"===t[l]&&Number(V)>5){l+=1,this._shiftStep(t,l,f.length),d--,this.leadZeroDateTime&&(o+="0");continue}if("s"===t[l]&&Number(V)>5){l+=1,this._shiftStep(t,l,f.length),d--,this.leadZeroDateTime&&(o+="0");continue}if("d"===t[l]&&(Number(V)>3&&this.leadZeroDateTime||Number(e.slice(l,l+2))>31||"/"===e[l+1])){l+=1,this._shiftStep(t,l,f.length),d--,this.leadZeroDateTime&&(o+="0");continue}if("M"===t[l]){var M=12,w=0===l&&(Number(V)>2||Number(e.slice(l,l+2))>M||"/"===e[l+1]),I=e.slice(l-3,l-1).includes("/")&&("/"===e[l-2]&&Number(e.slice(l-1,l+1))>M&&"/"!==e[l]||"/"===e[l]||"/"===e[l-3]&&Number(e.slice(l-2,l))>M&&"/"!==e[l-1]||"/"===e[l-1]),N=Number(e.slice(l-3,l-1))<=31&&!e.slice(l-3,l-1).includes("/")&&"/"===e[l-1]&&(Number(e.slice(l,l+2))>M||"/"===e[l+1]),P=Number(e.slice(l-3,l-1))>31&&!e.slice(l-3,l-1).includes("/")&&!e.slice(l-2,l).includes("/")&&Number(e.slice(l-2,l))>M,T=Number(e.slice(l-3,l-1))<=31&&!e.slice(l-3,l-1).includes("/")&&"/"!==e[l-1]&&Number(e.slice(l-1,l+1))>M;if(Number(V)>1&&this.leadZeroDateTime||w||I||N||P||T){l+=1,this._shiftStep(t,l,f.length),d--,this.leadZeroDateTime&&(o+="0");continue}}o+=V,l++}else-1!==this.maskSpecialCharacters.indexOf(t[l])?(o+=t[l],l++,this._shiftStep(t,l,f.length),d--):this.maskSpecialCharacters.indexOf(V)>-1&&this.maskAvailablePatterns[t[l]]&&this.maskAvailablePatterns[t[l]].optional?(f[l]&&"099.099.099.099"!==t&&"000.000.000-00"!==t&&"00.000.000/0000-00"!==t&&(o+=f[l]),l++,d--):"*"===this.maskExpression[l+1]&&this._findSpecialChar(this.maskExpression[l+2])&&this._findSpecialChar(V)===this.maskExpression[l+2]&&h||"?"===this.maskExpression[l+1]&&this._findSpecialChar(this.maskExpression[l+2])&&this._findSpecialChar(V)===this.maskExpression[l+2]&&h?(l+=3,o+=V):this.showMaskTyped&&this.maskSpecialCharacters.indexOf(V)<0&&V!==this.placeHolderCharacter&&(p=!0)}o.length+1===t.length&&-1!==this.maskSpecialCharacters.indexOf(t[t.length-1])&&(o+=t[t.length-1]);for(var A=i+1;this._shift.has(A);)u++,A++;var H=r?l:this._shift.has(i)?u:0;p&&H--,a(H,c),u<0&&this._shift.clear();var O=!1;s&&(O=f.every((function(e){return n.maskSpecialCharacters.includes(e)})));var R=""+this.prefix+(O?"":o)+this.suffix;return 0===o.length&&(R=""+this.prefix+o),R},e.prototype._findSpecialChar=function(e){return this.maskSpecialCharacters.find((function(t){return t===e}))},e.prototype._checkSymbolMask=function(e,t){return this.maskAvailablePatterns=this.customPattern?this.customPattern:this.maskAvailablePatterns,this.maskAvailablePatterns[t]&&this.maskAvailablePatterns[t].pattern&&this.maskAvailablePatterns[t].pattern.test(e)},e.prototype._stripToDecimal=function(e){var t=this;return e.split("").filter((function(e,i){return e.match("^-?\\d")||e.match("\\s")||"."===e||","===e||"-"===e&&0===i&&t.allowNegativeNumbers})).join("")},e.prototype._charToRegExpExpression=function(e){if(e){return" "===e?"\\s":"[\\^$.|?*+()".indexOf(e)>=0?"\\"+e:e}return e},e.prototype._shiftStep=function(e,t,i){var r=/[*?]/g.test(e.slice(0,t))?i:t;this._shift.add(r+this.prefix.length||0)},e}();m.decorators=[{type:t.Injectable}],m.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[s]}]}]};var d=function(e){function t(t,i,r,s){var a=e.call(this,i)||this;return a.document=t,a._config=i,a._elementRef=r,a._renderer=s,a.maskExpression="",a.isNumberValue=!1,a.placeHolderCharacter="_",a.maskIsShown="",a.selStart=null,a.selEnd=null,a.writingValue=!1,a.onChange=function(e){},a}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function i(){this.constructor=e}c(e,t),e.prototype=null===t?Object.create(t):(i.prototype=t.prototype,new i)}(t,e),t.prototype.applyMask=function(t,i,r,s,a,n){var l=this;if(void 0===r&&(r=0),void 0===s&&(s=!1),void 0===a&&(a=!1),void 0===n&&(n=function(){}),!i)return t;if(this.maskIsShown=this.showMaskTyped?this.showMaskInInput():"","IP"===this.maskExpression&&this.showMaskTyped&&(this.maskIsShown=this.showMaskInInput(t||"#")),"CPF_CNPJ"===this.maskExpression&&this.showMaskTyped&&(this.maskIsShown=this.showMaskInInput(t||"#")),!t&&this.showMaskTyped)return this.formControlResult(this.prefix),this.prefix+this.maskIsShown;var o=t&&"number"==typeof this.selStart?t[this.selStart]:"",h="";if(this.hiddenInput&&!this.writingValue){var c=this.actualValue.split("");""!==t&&c.length?"number"==typeof this.selStart&&"number"==typeof this.selEnd&&(t.length>c.length?c.splice(this.selStart,0,o):t.length<c.length&&(c.length-t.length==1?c.splice(this.selStart-1,1):c.splice(this.selStart,this.selEnd-this.selStart))):c=[],h=this.actualValue.length&&c.length<=t.length?this.shiftTypedSymbols(c.join("")):t}h=Boolean(h)&&h.length?h:t;var u=e.prototype.applyMask.call(this,h,i,r,s,a,n);if(this.actualValue=this.getActualValue(u),"."===this.thousandSeparator&&"."===this.decimalMarker&&(this.decimalMarker=","),this.maskExpression.startsWith("separator")&&!0===this.dropSpecialCharacters&&(this.maskSpecialCharacters=this.maskSpecialCharacters.filter((function(e){return e!==l.decimalMarker}))),this.formControlResult(u),!this.showMaskTyped)return this.hiddenInput&&u&&u.length?this.hideInput(u,this.maskExpression):u;var p=u.length,f=this.prefix+this.maskIsShown;if(this.maskExpression.includes("H")){var m=this._numberSkipedSymbols(u);return u+f.slice(p+m)}return"IP"===this.maskExpression||"CPF_CNPJ"===this.maskExpression?u+f:u+f.slice(p)},t.prototype._numberSkipedSymbols=function(e){for(var t=/(^|\D)(\d\D)/g,i=t.exec(e),r=0;null!=i;)r+=1,i=t.exec(e);return r},t.prototype.applyValueChanges=function(e,t,i,r){void 0===e&&(e=0),void 0===r&&(r=function(){});var s=this._elementRef.nativeElement;s.value=this.applyMask(s.value,this.maskExpression,e,t,i,r),s!==this.document.activeElement&&this.clearIfNotMatchFn()},t.prototype.hideInput=function(e,t){var i=this;return e.split("").map((function(e,r){return i.maskAvailablePatterns&&i.maskAvailablePatterns[t[r]]&&i.maskAvailablePatterns[t[r]].symbol?i.maskAvailablePatterns[t[r]].symbol:e})).join("")},t.prototype.getActualValue=function(e){var t=this,i=e.split("").filter((function(e,i){return t._checkSymbolMask(e,t.maskExpression[i])||t.maskSpecialCharacters.includes(t.maskExpression[i])&&e===t.maskExpression[i]}));return i.join("")===e?i.join(""):e},t.prototype.shiftTypedSymbols=function(e){var t=this,i="";return(e&&e.split("").map((function(r,s){if(t.maskSpecialCharacters.includes(e[s+1])&&e[s+1]!==t.maskExpression[s+1])return i=r,e[s+1];if(i.length){var a=i;return i="",a}return r}))||[]).join("")},t.prototype.showMaskInInput=function(e){if(this.showMaskTyped&&this.shownMaskExpression){if(this.maskExpression.length!==this.shownMaskExpression.length)throw new Error("Mask expression must match mask placeholder length");return this.shownMaskExpression}if(this.showMaskTyped){if(e){if("IP"===this.maskExpression)return this._checkForIp(e);if("CPF_CNPJ"===this.maskExpression)return this._checkForCpfCnpj(e)}return this.maskExpression.replace(/\w/g,this.placeHolderCharacter)}return""},t.prototype.clearIfNotMatchFn=function(){var e=this._elementRef.nativeElement;this.clearIfNotMatch&&this.prefix.length+this.maskExpression.length+this.suffix.length!==e.value.replace(/_/g,"").length&&(this.formElementProperty=["value",""],this.applyMask(e.value,this.maskExpression))},Object.defineProperty(t.prototype,"formElementProperty",{set:function(e){var t=this,i=f(e,2),r=i[0],s=i[1];Promise.resolve().then((function(){return t._renderer.setProperty(t._elementRef.nativeElement,r,s)}))},enumerable:!1,configurable:!0}),t.prototype.checkSpecialCharAmount=function(e){var t=this;return e.split("").filter((function(e){return t._findSpecialChar(e)})).length},t.prototype.removeMask=function(e){return this._removeMask(this._removeSuffix(this._removePrefix(e)),this.maskSpecialCharacters.concat("_").concat(this.placeHolderCharacter))},t.prototype._checkForIp=function(e){if("#"===e)return this.placeHolderCharacter+"."+this.placeHolderCharacter+"."+this.placeHolderCharacter+"."+this.placeHolderCharacter;for(var t=[],i=0;i<e.length;i++)e[i].match("\\d")&&t.push(e[i]);return t.length<=3?this.placeHolderCharacter+"."+this.placeHolderCharacter+"."+this.placeHolderCharacter:t.length>3&&t.length<=6?this.placeHolderCharacter+"."+this.placeHolderCharacter:t.length>6&&t.length<=9?this.placeHolderCharacter:(t.length>9&&t.length,"")},t.prototype._checkForCpfCnpj=function(e){var t=""+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+"."+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+"."+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+"-"+this.placeHolderCharacter+this.placeHolderCharacter,i=""+this.placeHolderCharacter+this.placeHolderCharacter+"."+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+"."+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+"/"+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+this.placeHolderCharacter+"-"+this.placeHolderCharacter+this.placeHolderCharacter;if("#"===e)return t;for(var r=[],s=0;s<e.length;s++)e[s].match("\\d")&&r.push(e[s]);return r.length<=3?t.slice(r.length,t.length):r.length>3&&r.length<=6?t.slice(r.length+1,t.length):r.length>6&&r.length<=9?t.slice(r.length+2,t.length):r.length>9&&r.length<11?t.slice(r.length+3,t.length):11===r.length?"":12===r.length?17===e.length?i.slice(16,i.length):i.slice(15,i.length):r.length>12&&r.length<=14?i.slice(r.length+4,i.length):""},t.prototype.formControlResult=function(e){this.writingValue||(Array.isArray(this.dropSpecialCharacters)?this.onChange(this._toNumber(this._removeMask(this._removeSuffix(this._removePrefix(e)),this.dropSpecialCharacters))):this.dropSpecialCharacters?this.onChange(this._toNumber(this._checkSymbols(e))):this.onChange(this._removeSuffix(e)))},t.prototype._toNumber=function(e){if(!this.isNumberValue||""===e)return e;var t=Number(e);return Number.isNaN(t)?e:t},t.prototype._removeMask=function(e,t){return e?e.replace(this._regExpForRemove(t),""):e},t.prototype._removePrefix=function(e){return this.prefix&&e?e.replace(this.prefix,""):e},t.prototype._removeSuffix=function(e){return this.suffix&&e?e.replace(this.suffix,""):e},t.prototype._retrieveSeparatorValue=function(e){return this._removeMask(this._removeSuffix(this._removePrefix(e)),this.maskSpecialCharacters)},t.prototype._regExpForRemove=function(e){return new RegExp(e.map((function(e){return"\\"+e})).join("|"),"gi")},t.prototype._checkSymbols=function(e){if(""===e)return e;var t=this._retrieveSeparatorPrecision(this.maskExpression),i=this._retrieveSeparatorValue(e);return"."!==this.decimalMarker&&(i=i.replace(this.decimalMarker,".")),this.isNumberValue?t?e===this.decimalMarker?null:this._checkPrecision(this.maskExpression,i):Number(i):i},t.prototype._retrieveSeparatorPrecision=function(e){var t=e.match(new RegExp("^separator\\.([^d]*)"));return t?Number(t[1]):null},t.prototype._checkPrecision=function(e,t){return e.indexOf("2")>0?Number(t).toFixed(2):Number(t)},t}(m);d.decorators=[{type:t.Injectable}],d.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[r.DOCUMENT]}]},{type:void 0,decorators:[{type:t.Inject,args:[s]}]},{type:t.ElementRef},{type:t.Renderer2}]};var k=function(){function e(e,t,i){this.document=e,this._maskService=t,this._config=i,this.maskExpression="",this.specialCharacters=[],this.patterns={},this.prefix="",this.suffix="",this.thousandSeparator=" ",this.decimalMarker=".",this.dropSpecialCharacters=null,this.hiddenInput=null,this.showMaskTyped=null,this.placeHolderCharacter=null,this.shownMaskExpression=null,this.showTemplate=null,this.clearIfNotMatch=null,this.validation=null,this.separatorLimit=null,this.allowNegativeNumbers=null,this.leadZeroDateTime=null,this._maskValue="",this._position=null,this._maskExpressionArray=[],this._justPasted=!1,this.onChange=function(e){},this.onTouch=function(){}}return e.prototype.ngOnChanges=function(e){var t=e.maskExpression,i=e.specialCharacters,r=e.patterns,s=e.prefix,a=e.suffix,n=e.thousandSeparator,l=e.decimalMarker,o=e.dropSpecialCharacters,h=e.hiddenInput,c=e.showMaskTyped,u=e.placeHolderCharacter,p=e.shownMaskExpression,f=e.showTemplate,m=e.clearIfNotMatch,d=e.validation,k=e.separatorLimit,g=e.allowNegativeNumbers,_=e.leadZeroDateTime;if(t&&(this._maskValue=t.currentValue||"",t.currentValue&&t.currentValue.split("||").length>1&&(this._maskExpressionArray=t.currentValue.split("||").sort((function(e,t){return e.length-t.length})),this._maskValue=this._maskExpressionArray[0],this.maskExpression=this._maskExpressionArray[0],this._maskService.maskExpression=this._maskExpressionArray[0])),i){if(!i.currentValue||!Array.isArray(i.currentValue))return;this._maskService.maskSpecialCharacters=i.currentValue||[]}r&&r.currentValue&&(this._maskService.maskAvailablePatterns=r.currentValue),s&&(this._maskService.prefix=s.currentValue),a&&(this._maskService.suffix=a.currentValue),n&&(this._maskService.thousandSeparator=n.currentValue),l&&(this._maskService.decimalMarker=l.currentValue),o&&(this._maskService.dropSpecialCharacters=o.currentValue),h&&(this._maskService.hiddenInput=h.currentValue),c&&(this._maskService.showMaskTyped=c.currentValue),u&&(this._maskService.placeHolderCharacter=u.currentValue),p&&(this._maskService.shownMaskExpression=p.currentValue),f&&(this._maskService.showTemplate=f.currentValue),m&&(this._maskService.clearIfNotMatch=m.currentValue),d&&(this._maskService.validation=d.currentValue),k&&(this._maskService.separatorLimit=k.currentValue),g&&(this._maskService.allowNegativeNumbers=g.currentValue,this._maskService.allowNegativeNumbers&&(this._maskService.maskSpecialCharacters=this._maskService.maskSpecialCharacters.filter((function(e){return"-"!==e})))),_&&(this._maskService.leadZeroDateTime=_.currentValue),this._applyMask()},e.prototype.validate=function(e){var t=e.value;if(!this._maskService.validation||!this._maskValue)return null;if(this._maskService.ipError)return this._createValidationError(t);if(this._maskService.cpfCnpjError)return this._createValidationError(t);if(this._maskValue.startsWith("separator"))return null;if(h.includes(this._maskValue))return null;if(this._maskService.clearIfNotMatch)return null;if(o.includes(this._maskValue))return this._validateTime(t);if(t&&t.toString().length>=1){var i=0,r=function(e){if(s._maskService.maskAvailablePatterns[e].optional&&!0===s._maskService.maskAvailablePatterns[e].optional){if(s._maskValue.indexOf(e)!==s._maskValue.lastIndexOf(e)){var r=s._maskValue.split("").filter((function(t){return t===e})).join("");i+=r.length}else-1!==s._maskValue.indexOf(e)&&i++;if(-1!==s._maskValue.indexOf(e)&&t.toString().length>=s._maskValue.indexOf(e))return{value:null};if(i===s._maskValue.length)return{value:null}}},s=this;for(var a in this._maskService.maskAvailablePatterns){var n=r(a);if("object"==typeof n)return n.value}if(1===this._maskValue.indexOf("{")&&t.toString().length===this._maskValue.length+Number(this._maskValue.split("{")[1].split("}")[0])-4)return null;if(1===this._maskValue.indexOf("*")||1===this._maskValue.indexOf("?"))return null;if(this._maskValue.indexOf("*")>1&&t.toString().length<this._maskValue.indexOf("*")||this._maskValue.indexOf("?")>1&&t.toString().length<this._maskValue.indexOf("?")||1===this._maskValue.indexOf("{"))return this._createValidationError(t);if(-1===this._maskValue.indexOf("*")||-1===this._maskValue.indexOf("?")){var l=this._maskService.dropSpecialCharacters?this._maskValue.length-this._maskService.checkSpecialCharAmount(this._maskValue)-i:this._maskValue.length-i;if(t.toString().length<l)return this._createValidationError(t)}}return null},e.prototype.onPaste=function(){this._justPasted=!0},e.prototype.onInput=function(e){var t=this,i=e.target;if(this._inputValue=i.value,this._setMask(),this._maskValue){var r=1===i.selectionStart?i.selectionStart+this._maskService.prefix.length:i.selectionStart,s=0,a=!1;if(this._maskService.applyValueChanges(r,this._justPasted,"Backspace"===this._code,(function(e,i){t._justPasted=!1,s=e,a=i})),this.document.activeElement===i){this._position=1===this._position&&1===this._inputValue.length?null:this._position;var n=this._position?this._inputValue.length+r+s:r+("Backspace"!==this._code||a?s:0);n>this._getActualInputLength()&&(n=this._getActualInputLength()),i.setSelectionRange(n,n),this._position=null}}else this.onChange(i.value)},e.prototype.onBlur=function(){this._maskValue&&this._maskService.clearIfNotMatchFn(),this.onTouch()},e.prototype.onFocus=function(e){if(this._maskValue){var t=e.target;null!==t&&null!==t.selectionStart&&t.selectionStart===t.selectionEnd&&t.selectionStart>this._maskService.prefix.length&&38!==e.keyCode&&this._maskService.showMaskTyped&&(this._maskService.maskIsShown=this._maskService.showMaskInInput(),t.setSelectionRange&&this._maskService.prefix+this._maskService.maskIsShown===t.value?(t.focus(),t.setSelectionRange(0,0)):t.selectionStart>this._maskService.actualValue.length&&t.setSelectionRange(this._maskService.actualValue.length,this._maskService.actualValue.length));var i=t.value&&t.value!==this._maskService.prefix?t.value:this._maskService.prefix+this._maskService.maskIsShown;t.value!==i&&(t.value=i),(t.selectionStart||t.selectionEnd)<=this._maskService.prefix.length?t.selectionStart=this._maskService.prefix.length:t.selectionEnd>this._getActualInputLength()&&(t.selectionEnd=this._getActualInputLength())}},e.prototype.onKeyDown=function(e){var t;if(this._maskValue){this._code=e.code?e.code:e.key;var i=e.target;if(this._inputValue=i.value,this._setMask(),38===e.keyCode&&e.preventDefault(),37===e.keyCode||8===e.keyCode||46===e.keyCode){if(8===e.keyCode&&0===i.value.length&&(i.selectionStart=i.selectionEnd),8===e.keyCode&&0!==i.selectionStart)if(this.specialCharacters=(null===(t=this.specialCharacters)||void 0===t?void 0:t.length)?this.specialCharacters:this._config.specialCharacters,this.prefix.length>1&&i.selectionStart<=this.prefix.length)i.setSelectionRange(this.prefix.length,this.prefix.length);else{if(this._inputValue.length!==i.selectionStart&&1!==i.selectionStart)for(;this.specialCharacters.includes(this._inputValue[i.selectionStart-1].toString())&&(this.prefix.length>=1&&i.selectionStart>this.prefix.length||0===this.prefix.length);)i.setSelectionRange(i.selectionStart-1,i.selectionStart-1);this.suffixCheckOnPressDelete(e.keyCode,i)}this.suffixCheckOnPressDelete(e.keyCode,i),this._maskService.prefix.length&&i.selectionStart<=this._maskService.prefix.length&&i.selectionEnd<=this._maskService.prefix.length&&e.preventDefault();var r=i.selectionStart;8!==e.keyCode||i.readOnly||0!==r||i.selectionEnd!==i.value.length||0===i.value.length||(this._position=this._maskService.prefix?this._maskService.prefix.length:0,this._maskService.applyMask(this._maskService.prefix,this._maskService.maskExpression,this._position))}this.suffix&&this.suffix.length>1&&this._inputValue.length-this.suffix.length<i.selectionStart?i.setSelectionRange(this._inputValue.length-this.suffix.length,this._inputValue.length):(65===e.keyCode&&!0===e.ctrlKey||65===e.keyCode&&!0===e.metaKey)&&(i.setSelectionRange(0,this._getActualInputLength()),e.preventDefault()),this._maskService.selStart=i.selectionStart,this._maskService.selEnd=i.selectionEnd}},e.prototype.writeValue=function(e){return u(this,void 0,void 0,(function(){return p(this,(function(t){return"object"==typeof e&&null!==e&&"value"in e&&("disable"in e&&this.setDisabledState(Boolean(e.disable)),e=e.value),void 0===e&&(e=""),"number"==typeof e&&(e=String(e),e="."!==this.decimalMarker?e.replace(".",this.decimalMarker):e,this._maskService.isNumberValue=!0),this._inputValue=e,this._setMask(),e&&this._maskService.maskExpression||this._maskService.maskExpression&&(this._maskService.prefix||this._maskService.showMaskTyped)?(this._maskService.writingValue=!0,this._maskService.formElementProperty=["value",this._maskService.applyMask(e,this._maskService.maskExpression)],this._maskService.writingValue=!1):this._maskService.formElementProperty=["value",e],this._inputValue=e,[2]}))}))},e.prototype.registerOnChange=function(e){this.onChange=e,this._maskService.onChange=this.onChange},e.prototype.registerOnTouched=function(e){this.onTouch=e},e.prototype.suffixCheckOnPressDelete=function(e,t){46===e&&this.suffix.length>0&&this._inputValue.length-this.suffix.length<=t.selectionStart&&t.setSelectionRange(this._inputValue.length-this.suffix.length,this._inputValue.length),8===e&&(this.suffix.length>1&&this._inputValue.length-this.suffix.length<t.selectionStart&&t.setSelectionRange(this._inputValue.length-this.suffix.length,this._inputValue.length),1===this.suffix.length&&this._inputValue.length===t.selectionStart&&t.setSelectionRange(t.selectionStart-1,t.selectionStart-1))},e.prototype.setDisabledState=function(e){this._maskService.formElementProperty=["disabled",e]},e.prototype._repeatPatternSymbols=function(e){var t=this;return e.match(/{[0-9]+}/)&&e.split("").reduce((function(i,r,s){if(t._start="{"===r?s:t._start,"}"!==r)return t._maskService._findSpecialChar(r)?i+r:i;t._end=s;var a=Number(e.slice(t._start+1,t._end));return i+new Array(a+1).join(e[t._start-1])}),"")||e},e.prototype._applyMask=function(){this._maskService.maskExpression=this._repeatPatternSymbols(this._maskValue||""),this._maskService.formElementProperty=["value",this._maskService.applyMask(this._inputValue,this._maskService.maskExpression)]},e.prototype._validateTime=function(e){var t=this._maskValue.split("").filter((function(e){return":"!==e})).length;return null===e||0===e.length?null:0==+e[e.length-1]&&e.length<t||e.length<=t-2?this._createValidationError(e):null},e.prototype._getActualInputLength=function(){return this._maskService.actualValue.length||this._maskService.actualValue.length+this._maskService.prefix.length},e.prototype._createValidationError=function(e){return{mask:{requiredMask:this._maskValue,actualValue:e}}},e.prototype._setMask=function(){var e=this;this._maskExpressionArray.length>0&&this._maskExpressionArray.some((function(t){var i=e._maskService.removeMask(e._inputValue).length<=e._maskService.removeMask(t).length;if(e._inputValue&&i)return e._maskValue=t,e.maskExpression=t,e._maskService.maskExpression=t,i;e._maskValue=e._maskExpressionArray[e._maskExpressionArray.length-1],e.maskExpression=e._maskExpressionArray[e._maskExpressionArray.length-1],e._maskService.maskExpression=e._maskExpressionArray[e._maskExpressionArray.length-1]}))},e}();k.decorators=[{type:t.Directive,args:[{selector:"input[mask], textarea[mask]",providers:[{provide:i.NG_VALUE_ACCESSOR,useExisting:t.forwardRef((function(){return k})),multi:!0},{provide:i.NG_VALIDATORS,useExisting:t.forwardRef((function(){return k})),multi:!0},d]}]}],k.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[r.DOCUMENT]}]},{type:d},{type:void 0,decorators:[{type:t.Inject,args:[s]}]}]},k.propDecorators={maskExpression:[{type:t.Input,args:["mask"]}],specialCharacters:[{type:t.Input}],patterns:[{type:t.Input}],prefix:[{type:t.Input}],suffix:[{type:t.Input}],thousandSeparator:[{type:t.Input}],decimalMarker:[{type:t.Input}],dropSpecialCharacters:[{type:t.Input}],hiddenInput:[{type:t.Input}],showMaskTyped:[{type:t.Input}],placeHolderCharacter:[{type:t.Input}],shownMaskExpression:[{type:t.Input}],showTemplate:[{type:t.Input}],clearIfNotMatch:[{type:t.Input}],validation:[{type:t.Input}],separatorLimit:[{type:t.Input}],allowNegativeNumbers:[{type:t.Input}],leadZeroDateTime:[{type:t.Input}],onPaste:[{type:t.HostListener,args:["paste"]}],onInput:[{type:t.HostListener,args:["input",["$event"]]}],onBlur:[{type:t.HostListener,args:["blur"]}],onFocus:[{type:t.HostListener,args:["click",["$event"]]}],onKeyDown:[{type:t.HostListener,args:["keydown",["$event"]]}]};var g=function(){function e(e){this._maskService=e}return e.prototype.transform=function(e,t,i){return void 0===i&&(i=null),e||"number"==typeof e?(i&&(this._maskService.thousandSeparator=i),"string"==typeof t?this._maskService.applyMask(""+e,t):this._maskService.applyMaskWithPattern(""+e,t)):""},e}();g.decorators=[{type:t.Pipe,args:[{name:"mask",pure:!0}]}],g.ctorParameters=function(){return[{type:m}]};var _=function(){function e(){}return e.forRoot=function(t){return{ngModule:e,providers:[{provide:a,useValue:t},{provide:n,useValue:l},{provide:s,useFactory:v,deps:[n,a]},m]}},e.forChild=function(){return{ngModule:e}},e}();function v(e,t){return t instanceof Function?Object.assign(Object.assign({},e),t()):Object.assign(Object.assign({},e),t)}_.decorators=[{type:t.NgModule,args:[{exports:[k,g],declarations:[k,g]}]}];var S="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};S.KeyboardEvent||(S.KeyboardEvent=function(e,t){}),e.INITIAL_CONFIG=n,e.MaskApplierService=m,e.MaskDirective=k,e.MaskPipe=g,e.MaskService=d,e.NEW_CONFIG=a,e.NgxMaskModule=_,e._configFactory=v,e.config=s,e.initialConfig=l,e.timeMasks=o,e.withoutValidation=h,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=ngx-mask.umd.min.js.map