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.27 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).VueMask={})}(this,(function(e){"use strict";function r(e){return(r="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 t(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function n(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 o(e){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?n(Object(o),!0).forEach((function(r){t(e,r,o[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(o,r))}))}return e}var i=[];function a(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:i,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"_";if(!u(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 u(e){return Array.isArray&&Array.isArray(e)||e instanceof Array}function c(e){for(var r,t=[];-1!==(r=e.indexOf("[]"));)t.push(r),e.splice(r,1);return{maskWithoutCaretTraps:e,indexes:t}}var f=[];function l(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!u(t)){if("function"!==r(t))throw new Error("Text-mask:conformToMask; The mask property must be an array.");t=c(t=t(e,n)).maskWithoutCaretTraps}var o=n.guide,i=void 0===o||o,l=n.previousConformedValue,s=void 0===l?"":l,p=n.placeholderChar,v=void 0===p?"_":p,d=n.placeholder,h=void 0===d?a(t,v):d,y=n.currentCaretPosition,g=n.keepCharPositions,m=!1===i&&void 0!==s,b=e.length,O=s.length,k=h.length,w=t.length,j=b-O,x=j>0,E=y+(x?-j:0),P=E+Math.abs(j);if(!0===g&&!x){for(var V="",A=E;A<P;A++)h[A]===v&&(V+=v);e=e.slice(0,E)+V+e.slice(E,b)}for(var M=e.split("").map((function(e,r){return{char:e,isNew:r>=E&&r<P}})),S=b-1;S>=0;S--){var T=M[S].char;if(T!==v){var C=S>=E&&O===w;T===h[C?S-j:S]&&M.splice(S,1)}}var R="",_=!1;e:for(var N=0;N<k;N++){var D=h[N];if(D===v){if(M.length>0)for(;M.length>0;){var U=M.shift(),z=U.char,B=U.isNew;if(z===v&&!0!==m){R+=v;continue e}if(t[N].test(z)){if(!0===g&&!1!==B&&""!==s&&!1!==i&&x){for(var H=M.length,I=null,J=0;J<H;J++){var L=M[J];if(L.char!==v&&!1===L.isNew)break;if(L.char===v){I=J;break}}null!==I?(R+=z,M.splice(I,1)):N--}else R+=z;continue e}_=!0}!1===m&&(R+=h.substr(N,k));break}R+=D}if(m&&!1===x){for(var W=null,$=0;$<R.length;$++)h[$]===v&&(W=$);R=null!==W?R.substr(0,W+1):""}return{conformedValue:R,meta:{someCharsRejected:_}}}var s={__nextCharOptional__:!0},p={"#":/\d/,A:/[a-z]/i,N:/[a-z0-9]/i,"?":s,X:/./},v=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},d=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 h(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p;return e.map((function(e,t,n){var o=r[e]||e,i=n[t-1],a=r[i]||i;return o===s?null:a===s?v(d(o)):o})).filter(Boolean)}function y(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p;return h(e.split(""),r)}var g=function(e){return e instanceof HTMLInputElement?e:e.querySelector("input")||e},m=function(e){return"function"==typeof e},b=function(e){return"string"==typeof e};var O=function(){var e=new Map,r={previousValue:"",mask:[]};function t(t){return e.get(t)||o({},r)}return{partiallyUpdate:function(r,n){e.set(r,o(o({},t(r)),n))},remove:function(r){e.delete(r)},get:t}}();function k(e){!function(e,r){var t=document.createEvent("HTMLEvents");t.initEvent(r,!0,!0),e.dispatchEvent(t)}(e,"input")}function w(e){var r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=e.value,n=O.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=l(t,i,{guide:!1}),s=f.conformedValue;e.value=s,k(e)}O.partiallyUpdate(e,{previousValue:t})}function j(e,r,t){var n;n=Array.isArray(r)?function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p,t=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)}),[]);return h(t,r)}(r,t):m(r)?r:b(r)&&r.length>0?y(r,t):r,O.partiallyUpdate(e,{mask:n})}function x(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p;return null===e||Array.isArray(e)||"object"!==r(e)?n:Object.keys(e).reduce((function(r,n){var i=e[n];return null===i||i instanceof RegExp?o(o({},r),{},t({},n,i)):r}),n)}function E(e){return(Array.isArray(e)?e:[e]).filter((function(e){return b(e)||e instanceof RegExp})).toString()}function P(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=x(e&&e.placeholders);return{bind:function(e,t){var n=t.value;j(e=g(e),n,r),w(e)},componentUpdated:function(e,t){var n=t.value,o=t.oldValue;e=g(e);var i=m(n)||E(o)!==E(n);i&&j(e,n,r),w(e,i)},unbind:function(e){e=g(e),O.remove(e)}}}var V=P(),A=function(e,r){return l(e,y(r),{guide:!1}).conformedValue},M=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e.directive("mask",P(r)),e.filter("VMask",A)};e.VueMaskDirective=V,e.VueMaskFilter=A,e.VueMaskPlugin=M,e.default=M,Object.defineProperty(e,"__esModule",{value:!0})}));