UNPKG

rxmask

Version:

Simple, but advanced mask parser for html input or parsing provided string directly

1 lines 4.71 kB
!function(t,s){"object"==typeof exports&&"object"==typeof module?module.exports=s():"function"==typeof define&&define.amd?define([],s):"object"==typeof exports?exports.rxmask=s():t.rxmask=s()}(self,(function(){return(()=>{"use strict";var t={950:(t,s,o)=>{o.r(s),o.d(s,{default:()=>e});class e{constructor(t={},s){this.options={mask:"",placeholderSymbol:"*",rxmask:[],value:"",cursorPos:0,allowedCharacters:".",maxMaskLength:0,trailing:!0},this.errors=[],this._output="",this._parsedValue="",this._prevValue="",this._isRemovingSymbols=!1,this._actualCursorPos=0,this._finalCursorPos=0,this.input=s,this.setOptions(t),this.input?this.onInput():this.parseMask()}get output(){return this._output}get parsedValue(){return this._parsedValue}get finalCursorPos(){return this._finalCursorPos}setOptions({mask:t="",placeholderSymbol:s="*",rxmask:o="",allowedCharacters:e=".",maxMaskLength:r="0",trailing:i="true",value:a="",cursorPos:n=-1}){this.options.mask=t,this.options.placeholderSymbol=s,this.options.rxmask=this.strToRxmask(o),this.options.allowedCharacters=e,this.options.maxMaskLength=Number(r),this.options.trailing="true"===i,this.options.value=a,this.options.cursorPos=n,0===this.options.rxmask.length&&(this.options.rxmask=this.options.mask.split("").map((t=>t===this.options.placeholderSymbol?"[^]":t)))}parseOptionsFromInput(){if(this.input){const t=this.input.dataset;this.options.mask=t.mask||this.options.mask,this.options.placeholderSymbol=t.placeholdersymbol||this.options.placeholderSymbol,this.options.rxmask=t.rxmask?this.strToRxmask(t.rxmask):this.options.rxmask,this.options.allowedCharacters=t.allowedcharacters||this.options.allowedCharacters,this.options.maxMaskLength=void 0!==t.maxmasklength?Number(t.maxmasklength):this.options.maxMaskLength,this.options.trailing=void 0!==t.trailing?"true"===t.trailing:this.options.trailing,this.options.value=this.input.value,this.options.cursorPos=this.input.selectionStart||0,0===this.options.rxmask.length&&(this.options.rxmask=this.options.mask.split("").map((t=>t===this.options.placeholderSymbol?"[^]":t)))}}onInput(){this.parseOptionsFromInput(),this.parseMask(),this.input&&(this.input.value=this.output,this.input.setSelectionRange(this.finalCursorPos,this.finalCursorPos))}parseMask(){this.errors=[];const t=this.parseOutMask(),s=this.parseRxmask(t);this._parsedValue=s,this._output=this.getOutput(s),this._prevValue=this._output}parseOutMask(){const{value:t,cursorPos:s,rxmask:o,placeholderSymbol:e,allowedCharacters:r}=this.options,i=t.length-this._prevValue.length;this._isRemovingSymbols=!(i>=0);let a=/./;try{a=new RegExp(r)}catch(t){console.error("Wrong regex for allowedCharacters!")}let n="";for(let r=s;r<t.length;r++)t[r]!==o[r-i]&&t[r]!==e&&t[r].match(a)&&(n+=t[r]);let l="";for(let r=0;r<s;r++)t[r]!==o[r]&&(t[r]!==e&&t[r].match(a)?l.length<o.filter((t=>t.match(/\[.*\]/))).length-n.length?l+=t[r]:this.errors.push({symbol:t[r],position:r,type:"length"}):t[r]===e||t[r].match(a)||this.errors.push({symbol:t[r],position:r,type:"allowedCharacters"}));return this._actualCursorPos=l.length,l+n}parseRxmask([...t]){const{rxmask:s}=this.options;let o="";const e=s.filter((t=>t.match(/\[.*\]/)));let r=0,i=0;for(;t.length>0&&r<t.length;){let s=/./;try{s=new RegExp(e[r])}catch(t){console.error("Wrong regex for rxmask!")}t[r].match(s)?(o+=t[r],r++):(this.errors.push({symbol:t[r],position:r+i,type:"rxmask"}),t.shift(),i++,this._actualCursorPos>r&&this._actualCursorPos--)}return o}getOutput([...t]){const{rxmask:s,maxMaskLength:o,placeholderSymbol:e,trailing:r}=this.options;this._finalCursorPos=0;let i="";const a=0===t.length,n=s.filter((t=>t.match(/\[.*\]/))).length===t.length;let l=!1;for(let h=0;h<s.length;h++)if(s[h].match(/\[.*\]/)){if(t.length>0)i+=t.shift();else{if(!(o>h))break;i+=e,l=!0}this._actualCursorPos>0&&this._finalCursorPos++,this._actualCursorPos--}else{if(!(o>h||t.length>0||(r||n)&&!l&&!this._isRemovingSymbols))break;i+=s[h],(!l&&a&&o>h||this._actualCursorPos>0||r&&0===this._actualCursorPos&&!this._isRemovingSymbols)&&this._finalCursorPos++}return i}strToRxmask(t){return(t||"").match(/(\[.*?\])|(.)/g)||[]}}!function(){const t=document.getElementsByClassName("rxmask");for(let s=0;s<t.length;s++){const o=t[s],r=new e({},o);o.oninput=()=>r.onInput()}}()}},s={};function o(e){if(s[e])return s[e].exports;var r=s[e]={exports:{}};return t[e](r,r.exports,o),r.exports}return o.d=(t,s)=>{for(var e in s)o.o(s,e)&&!o.o(t,e)&&Object.defineProperty(t,e,{enumerable:!0,get:s[e]})},o.o=(t,s)=>Object.prototype.hasOwnProperty.call(t,s),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o(950)})()}));