UNPKG

v-mask

Version:

Tiny input mask library for Vue.js based on text-mask-core (~5kb) exposed as directive. No dependencies

2 lines (1 loc) 6.51 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).VueMask={})}(this,(function(e){"use strict";function r(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function t(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(r){o(e,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(n,r))}))}return e}function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var i="_",a="function",u=[];function c(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:u,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:i;if(!f(e))throw new Error("Text-mask:convertMaskToPlaceholder; The mask property must be an array.");if(-1!==e.indexOf(r))throw new Error("Placeholder character must not be used as part of the mask. Please specify a character that is not present in your mask as your placeholder character.\n\n"+"The placeholder character that was received is: ".concat(JSON.stringify(r),"\n\n")+"The mask that was received is: ".concat(JSON.stringify(e)));return e.map((function(e){return e instanceof RegExp?r:e})).join("")}function f(e){return Array.isArray&&Array.isArray(e)||e instanceof Array}function l(e){for(var r,t=[];-1!==(r=e.indexOf("[]"));)t.push(r),e.splice(r,1);return{maskWithoutCaretTraps:e,indexes:t}}var s=[],p="";function v(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:p,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:s,t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!f(r)){if(n(r)!==a)throw new Error("Text-mask:conformToMask; The mask property must be an array.");r=l(r=r(e,t)).maskWithoutCaretTraps}var o=t.guide,u=void 0===o||o,v=t.previousConformedValue,d=void 0===v?p:v,h=t.placeholderChar,y=void 0===h?i:h,g=t.placeholder,m=void 0===g?c(r,y):g,b=t.currentCaretPosition,O=t.keepCharPositions,k=!1===u&&void 0!==d,w=e.length,j=d.length,x=m.length,E=r.length,P=w-j,T=P>0,V=b+(T?-P:0),A=V+Math.abs(P);if(!0===O&&!T){for(var M=p,S=V;S<A;S++)m[S]===y&&(M+=y);e=e.slice(0,V)+M+e.slice(V,w)}for(var C=e.split(p).map((function(e,r){return{char:e,isNew:r>=V&&r<A}})),R=w-1;R>=0;R--){var N=C[R].char;if(N!==y){var _=R>=V&&j===E;N===m[_?R-P:R]&&C.splice(R,1)}}var D=p,U=!1;e:for(var z=0;z<x;z++){var B=m[z];if(B===y){if(C.length>0)for(;C.length>0;){var F=C.shift(),H=F.char,I=F.isNew;if(H===y&&!0!==k){D+=y;continue e}if(r[z].test(H)){if(!0===O&&!1!==I&&d!==p&&!1!==u&&T){for(var J=C.length,L=null,W=0;W<J;W++){var $=C[W];if($.char!==y&&!1===$.isNew)break;if($.char===y){L=W;break}}null!==L?(D+=H,C.splice(L,1)):z--}else D+=H;continue e}U=!0}!1===k&&(D+=m.substr(z,x));break}D+=B}if(k&&!1===T){for(var q=null,X=0;X<D.length;X++)m[X]===y&&(q=X);D=null!==q?D.substr(0,q+1):p}return{conformedValue:D,meta:{someCharsRejected:U}}}var d={__nextCharOptional__:!0},h={"#":/\d/,A:/[a-z]/i,N:/[a-z0-9]/i,"?":d,X:/./},y=function(e){return r=e.toString().replace(/.(\/)[gmiyus]{0,6}$/,(function(e){return e.replace("/","?/")})),t=r.lastIndexOf("/"),new RegExp(r.slice(1,t),r.slice(t+1));var r,t},g=function(e){return e instanceof RegExp?e:function(e){return new RegExp("/[".concat(function(e){return"[\\^$.|?*+()".indexOf(e)>-1?"\\".concat(e):e}(e),"]/"))}(e)};function m(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:h;return e.map((function(e,t,n){var o=r[e]||e,i=n[t-1],a=r[i]||i;return o===d?null:a===d?y(g(o)):o})).filter(Boolean)}var b=function(e){return e instanceof HTMLInputElement?e:e.querySelector("input")||e},O=function(e){return"function"==typeof e},k=function(e){return"string"==typeof e};function w(e,r){return Array.isArray(e)?function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:h;return m(e.map((function(e){return e instanceof RegExp?e:"string"==typeof e?e.split(""):null})).filter(Boolean).reduce((function(e,r){return e.concat(r)}),[]),r)}(e,r):O(e)?e:k(e)&&e.length>0?function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:h;return m(e.split(""),r)}(e,r):e}function j(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:h;return null===e||Array.isArray(e)||"object"!==n(e)?r:Object.keys(e).reduce((function(r,n){var i=e[n];return null===i||i instanceof RegExp?t(t({},r),{},o({},n,i)):r}),r)}var x=function(){var e=new Map,r={previousValue:"",mask:[]};function n(n){return e.get(n)||t({},r)}return{partiallyUpdate:function(r,o){e.set(r,t(t({},n(r)),o))},remove:function(r){e.delete(r)},get:n}}();function E(e){!function(e,r){var t=document.createEvent("HTMLEvents");t.initEvent(r,!0,!0),e.dispatchEvent(t)}(e,"input")}function P(e){var r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=e.value,n=x.get(e),o=n.previousValue,i=n.mask,a=t!==o,u=t.length>o.length,c=t&&a&&u;if((r||c)&&i){var f=v(t,i,{guide:!1}),l=f.conformedValue;e.value=l,E(e)}x.partiallyUpdate(e,{previousValue:t})}function T(e,r,t){var n=w(r,t);x.partiallyUpdate(e,{mask:n})}function V(e){return(Array.isArray(e)?e:[e]).filter((function(e){return k(e)||e instanceof RegExp})).toString()}function A(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=j(e&&e.placeholders);return{bind:function(e,t){var n=t.value;T(e=b(e),n,r),P(e)},componentUpdated:function(e,t){var n=t.value,o=t.oldValue;e=b(e);var i=O(n)||V(o)!==V(n);i&&T(e,n,r),P(e,i)},unbind:function(e){e=b(e),x.remove(e)}}}var M=A();function S(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=j(e&&e.placeholders);return function(e,t){if(!k(e)&&!Number.isFinite(e))return e;var n=w(t,r);return v("".concat(e),n,{guide:!1}).conformedValue}}var C=S(),R=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e.directive("mask",A(r)),e.filter("VMask",S(r))};e.VueMaskDirective=M,e.VueMaskFilter=C,e.VueMaskPlugin=R,e.default=R,Object.defineProperty(e,"__esModule",{value:!0})}));