UNPKG

@react-input/mask

Version:
2 lines (1 loc) 5.57 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./helpers-CbSlekGl.cjs"),t=require("@react-input/core");var a=function(e){return function(){for(var t=arguments.length,a=new Array(t),r=0;r<t;r++)a[r]=arguments[r];return new e("".concat(a.join("\n\n"),"\n"))}};var r,n=["track","modify"];function i(t){var a,r,n,i;return{mask:null!==(a=t.mask)&&void 0!==a?a:"",replacement:"string"==typeof t.replacement?e.formatToReplacementObject(t.replacement):null!==(r=t.replacement)&&void 0!==r?r:{},showMask:null!==(n=t.showMask)&&void 0!==n&&n,separate:null!==(i=t.separate)&&void 0!==i&&i,track:t.track,modify:t.modify}}var o=function(r){function o(){var r,l=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return e._classCallCheck(this,o),(r=e._callSuper(this,o,[{init:function(e){var t=e.initialValue,r=e.controlled,n=i(l),o=n.mask,c=n.replacement,u=n.separate,s=n.showMask;return t=r||t?t:s?o:"","production"!==process.env.NODE_ENV&&function(e){var t=e.initialValue,r=e.mask,n=e.replacement;t.length>r.length&&console.error(a(Error)("The initialized value of the `value` or `defaultValue` property is longer than the value specified in the `mask` property. Check the correctness of the initialized value in the specified property.",'Invalid value: "'.concat(t,'".'),"To initialize an unmasked value, use the `format` utility. More details https://github.com/GoncharukOrg/react-input/tree/main/packages/mask#initializing-the-value."));var i=Object.keys(n).filter((function(e){return e.length>1}));i.length>0&&console.error(a(Error)("Object keys in the `replacement` property are longer than one character. Replacement keys must be one character long. Check the correctness of the value in the specified property.","Invalid keys: ".concat(i.join(", "),"."),"To initialize an unmasked value, use the `format` utility. More details https://github.com/GoncharukOrg/react-input/tree/main/packages/mask#initializing-the-value."));for(var o=r.slice(0,t.length),l=-1,c=0;c<o.length;c++){var u=Object.prototype.hasOwnProperty.call(n,o[c]);if(!(o[c]===t[c]||u&&n[o[c]].test(t[c]))){l=c;break}}-1!==l&&console.error(a(Error)("An invalid character was found in the initialized property value `value` or `defaultValue` (index: ".concat(l,"). Check the correctness of the initialized value in the specified property."),'Invalid value: "'.concat(t,'".'),"To initialize an unmasked value, use the `format` utility. More details https://github.com/GoncharukOrg/react-input/tree/main/packages/mask#initializing-the-value."))}({initialValue:t,mask:o,replacement:c}),{value:t,options:{mask:o,replacement:c,separate:u}}},tracking:function(a){var r=a.inputType,o=a.previousValue,c=a.previousOptions,u=a.addedValue,s=a.changeStart,p=a.changeEnd,d=i(l),h=d.track,v=d.modify,m=e._objectWithoutProperties(d,n),f=m.mask,k=m.replacement,g=m.showMask,y=m.separate,b="insert"===r?{inputType:r,data:u}:{inputType:r,data:null},j=e._objectSpread2(e._objectSpread2({},b),{},{value:o,selectionStart:s,selectionEnd:p}),w=null==h?void 0:h(j);if(!1===w)throw new t.SyntheticChangeError("Custom tracking stop.");null===w?u="":!0!==w&&void 0!==w&&(u=w);var O=null==v?void 0:v(j);void 0!==(null==O?void 0:O.mask)&&(f=O.mask),void 0!==(null==O?void 0:O.replacement)&&(k="string"==typeof(null==O?void 0:O.replacement)?e.formatToReplacementObject(null==O?void 0:O.replacement):O.replacement),void 0!==(null==O?void 0:O.showMask)&&(g=O.showMask),void 0!==(null==O?void 0:O.separate)&&(y=O.separate);var T=e.unformat$1(o,e._objectSpread2({end:s},c)),C=e.unformat$1(o,e._objectSpread2({start:p},c)),M=RegExp("[^".concat(Object.keys(k).join(""),"]"),"g"),S=f.replace(M,"");if(T&&(T=e.filter(T,{replacementChars:S,replacement:k,separate:y}),S=S.slice(T.length)),u&&(u=e.filter(u,{replacementChars:S,replacement:k,separate:!1}),S=S.slice(u.length)),"insert"===r&&""===u)throw new t.SyntheticChangeError("The character does not match the key value of the `replacement` object.");if(y){var V=f.slice(s,p).replace(M,""),_=V.length-u.length;_<0?C=C.slice(-_):_>0&&(C=V.slice(-_)+C)}C&&(C=e.filter(C,{replacementChars:S,replacement:k,separate:y}));var E=T+u+C,z=e.format$1(E,{mask:f,replacement:k,separate:y,showMask:g}),P=function(t){var a,r,n,i=t.inputType,o=t.value,l=t.addedValue,c=t.beforeChangeValue,u=t.mask,s=t.replacement,p=t.separate,d=e.formatToParts$1(o,{mask:u,replacement:s}).filter((function(e){var t=e.type;return"input"===t||p&&"replacement"===t})),h=null===(a=d[c.length+l.length-1])||void 0===a?void 0:a.index,v=null===(r=d[c.length-1])||void 0===r?void 0:r.index,m=null===(n=d[c.length+l.length])||void 0===n?void 0:n.index;if("insert"===i){if(void 0!==h)return h+1;if(void 0!==m)return m;if(void 0!==v)return v+1}if("deleteForward"===i){if(void 0!==m)return m;if(void 0!==v)return v+1}if("deleteBackward"===i){if(void 0!==v)return v+1;if(void 0!==m)return m}var f=o.split("").findIndex((function(e){return Object.prototype.hasOwnProperty.call(s,e)}));return-1!==f?f:o.length}({inputType:r,value:z,addedValue:u,beforeChangeValue:T,mask:f,replacement:k,separate:y});return{value:z,selectionStart:P,selectionEnd:P,options:{mask:f,replacement:k,separate:y}}}}])).format=function(t){return e.format(t,i(l))},r.formatToParts=function(t){return e.formatToParts(t,i(l))},r.unformat=function(t){return e.unformat(t,i(l))},r.generatePattern=function(t){return e.generatePattern(t,i(l))},r}return e._inherits(o,r),e._createClass(o)}(t.Input);r=o,Object.defineProperty(r.prototype,Symbol.toStringTag,{writable:!1,enumerable:!1,configurable:!0,value:"Mask"}),exports.default=o;