react-number-format
Advanced tools
Comparing version 5.2.0 to 5.2.1
/** | ||
* react-number-format - 5.2.0 | ||
* react-number-format - 5.2.1 | ||
* Author : Sudhanshu Yadav | ||
@@ -400,3 +400,3 @@ * Copyright (c) 2016, 2023 to Sudhanshu Yadav, released under the MIT license. | ||
numAsString = removeFormatting(value, undefined); | ||
formattedValue = value; | ||
formattedValue = format(numAsString); | ||
} | ||
@@ -403,0 +403,0 @@ return { formattedValue: formattedValue, numAsString: numAsString }; |
/** | ||
* react-number-format - 5.2.0 | ||
* react-number-format - 5.2.1 | ||
* Author : Sudhanshu Yadav | ||
@@ -393,3 +393,3 @@ * Copyright (c) 2016, 2023 to Sudhanshu Yadav, released under the MIT license. | ||
numAsString = removeFormatting(value, undefined); | ||
formattedValue = value; | ||
formattedValue = format(numAsString); | ||
} | ||
@@ -396,0 +396,0 @@ return { formattedValue: formattedValue, numAsString: numAsString }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).NumberFormat={},e.React)}(this,function(e,I){"use strict";var k,t,M="default"in I?I.default:I;function P(e,t){var r={};for(a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(r[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,a=Object.getOwnPropertySymbols(e);n<a.length;n++)t.indexOf(a[n])<0&&Object.prototype.propertyIsEnumerable.call(e,a[n])&&(r[a[n]]=e[a[n]]);return r}function K(){}function W(e){return!!(e||"").match(/\d/)}function N(e){return null==e}function O(e){return N(e)||"number"==typeof(t=e)&&isNaN(t)||"number"==typeof e&&!isFinite(e);var t}function h(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function d(e,t,r){var r=function(e){switch(e){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),n=-1===(n=e.search(/[1-9]/))?e.length:n;return e.substring(0,n)+e.substring(n,e.length).replace(r,"$1"+t)}function S(e,t){void 0===t&&(t=!0);var r="-"===e[0],t=r&&t,e=(e=e.replace("-","")).split(".");return{beforeDecimal:e[0],afterDecimal:e[1]||"",hasNegation:r,addNegation:t}}function g(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function a(e,t){return Array(t+1).join(e)}function D(e){var t,e=e+"",r="-"===e[0]?"-":"",e=(e=r?e.substring(1):e).split(/[eE]/g),n=e[0],e=e[1];return(e=Number(e))&&(e=1+e,t=(n=n.replace(".","")).length,e<0?n="0."+a("0",Math.abs(e))+n:t<=e?n+=a("0",e-t):n=(n.substring(0,e)||"0")+"."+n.substring(e)),r+n}function E(e,t,r){var n,a,o,u;return-1!==["","-"].indexOf(e)?e:(n=(-1!==e.indexOf(".")||r)&&t,a=(e=S(e)).beforeDecimal,u=e.afterDecimal,e=e.hasNegation,o=parseFloat("0."+(u||"0")),u=(u.length<=t?"0."+u:o.toFixed(t)).split("."),(e?"-":"")+a.split("").reverse().reduce(function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e},u[0])+(n?".":"")+g(u[1]||"",t,r))}function L(e,t){var r;e.value=e.value,null!==e&&(e.createTextRange?((r=e.createTextRange()).move("character",t),r.select()):e.selectionStart||0===e.selectionStart?(e.focus(),e.setSelectionRange(t,t)):e.focus())}function U(e,t){for(var r=0,n=0,a=e.length,o=t.length;e[r]===t[r]&&r<a;)r++;for(;e[a-1-n]===t[o-1-n]&&r<o-n&&r<a-n;)n++;return{from:{start:r,end:a-n},to:{start:r,end:o-n}}}function $(e){return Math.max(e.selectionStart,e.selectionEnd)}function y(e){return{from:{start:0,end:0},to:{start:0,end:e.length},lastValue:""}}function m(e,t){return"string"==typeof(e=void 0===e?" ":e)?e:e[t]||" "}function G(e,t,r,n){var a,o,u=e.length;if(e=t,a=0,o=u,t=Math.min(Math.max(e,a),o),"left"===n){for(;0<=t&&!r[t];)t--;-1===t&&(t=r.indexOf(!0))}else{for(;t<=u&&!r[t];)t++;u<t&&(t=r.lastIndexOf(!0))}return t=-1===t?u:t}function Z(e){for(var t=Array.from({length:e.length+1}).map(function(){return!0}),r=0,n=t.length;r<n;r++)t[r]=Boolean(W(e[r])||W(e[r-1]));return t}function _(e,t,r,n,a,o){void 0===o&&(o=K);l=function(e,t){var r,t=O(e)?r="":"number"==typeof e||t?(r="number"==typeof e?D(e):e,n(r)):(r=a(e,void 0),e);return{formattedValue:t,numAsString:r}},(u=I.useRef(l)).current=l;var u,i=I.useRef(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return u.current.apply(u,e)}).current,l=I.useState(function(){return i(N(e)?t:e,r)}),s=l[0],c=l[1],l=I.useRef(s),f=e,d=r,f=(N(e)&&(f=s.numAsString,d=!0),i(f,d));return f.formattedValue!==l.current.formattedValue&&(l.current=f,c(f)),[s,function(e,t){e.formattedValue!==s.formattedValue&&c({formattedValue:e.formattedValue,numAsString:e.value}),o(e,t)}]}function q(e){return e.replace(/[^0-9]/g,"")}function z(e){return e}function r(e){function s(e,t){O.current={formattedValue:e.formattedValue,numAsString:e.value},N(e,t)}function i(e,t,r){return G(e,t,b(e),r)}var t=e.type,r=(void 0===t&&(t="text"),e.displayType),n=(void 0===r&&(r="input"),e.customInput),a=e.renderText,o=e.getInputRef,u=e.format,l=(void 0===u&&(u=z),e.removeFormatting),c=(void 0===l&&(l=q),e.defaultValue),f=e.valueIsNumericString,d=e.onValueChange,g=e.isAllowed,m=e.onChange,v=(void 0===m&&(m=K),e.onKeyDown),p=(void 0===v&&(v=K),e.onMouseUp),h=(void 0===p&&(p=K),e.onFocus),S=(void 0===h&&(h=K),e.onBlur),y=(void 0===S&&(S=K),e.value),b=e.getCaretBoundary,w=(void 0===b&&(b=Z),e.isValidInputCharacter),e=(void 0===w&&(w=W),P(e,["type","displayType","customInput","renderText","getInputRef","format","removeFormatting","defaultValue","valueIsNumericString","onValueChange","isAllowed","onChange","onKeyDown","onMouseUp","onFocus","onBlur","value","getCaretBoundary","isValidInputCharacter"])),y=_(y,c,Boolean(f),u,l,d),c=y[0],x=c.formattedValue,V=c.numAsString,N=y[1],O=I.useRef({formattedValue:x,numAsString:V}),f=I.useState(!1),d=f[0],D=f[1],E=I.useRef(null),C=I.useRef({setCaretTimeout:null,focusTimeout:null}),F=(I.useEffect(function(){return D(!0),function(){clearTimeout(C.current.setCaretTimeout),clearTimeout(C.current.focusTimeout)}},[]),u),j=function(e,t){var r=parseFloat(t);return{formattedValue:e,value:t,floatValue:isNaN(r)?void 0:r}},A=function(e,t,r){0===e.selectionStart&&e.selectionEnd===e.value.length||(L(e,t),C.current.setCaretTimeout=setTimeout(function(){e.value===r&&e.selectionStart!==e.selectionEnd&&L(e,t)},0))},B=function(e,t,r){var n=b(t);return G(t,function(e,t,r,n,a,o){var u=(i=U(r,e)).from,i=i.to;if(u.end-u.start==1&&u.end===i.end&&i.end===n)return n;for(var u=a.findIndex(function(e){return e}),i=e.slice(0,u),l=(t||r.startsWith(i)||(r=i+r,n+=i.length),r.length),s=e.length,c={},f=new Array(l),d=0;d<l;d++){f[d]=-1;for(var g=0,m=s;g<m;g++)if(r[d]===e[g]&&!0!==c[g]){c[f[d]=g]=!0;break}}for(var v=n;v<l&&(-1===f[v]||!o(r[v]));)v++;for(a=v===l||-1===f[v]?s:f[v],v=n-1;0<v&&-1===f[v];)v--;return u=-1===v||-1===f[v]?0:f[v]+1,!(a<u)&&n-u<a-n?u:a}(t,x,e,r,n,w),n)},T=function(e){var t,r=e.formattedValue,n=(void 0===r&&(r=""),e.input),a=e.setCaretPosition,o=(void 0===a&&(a=!0),e.source),u=e.event,i=e.numAsString,l=e.caretPos;n&&(void 0===l&&a&&(e=e.inputValue||n.value,t=$(n),n.value=r,l=B(e,r,t)),n.value=r,a&&void 0!==l&&A(n,l,r)),r!==x&&s(j(r,i),{event:u,source:o})},R=(I.useEffect(function(){var e=O.current,t=e.formattedValue,e=e.numAsString;x===t||x===V&&t===e||s(j(x,V),{event:void 0,source:k.props})},[x,V]),E.current?$(E.current):void 0),c=(I.useLayoutEffect(function(){var e,t=E.current;x!==O.current.formattedValue&&t&&(e=B(O.current.formattedValue,x,R),t.value=x,A(t,e,x))},[x]),d&&!("undefined"==typeof navigator||navigator.platform&&/iPhone|iPod/.test(navigator.platform))?"numeric":void 0),y=Object.assign({inputMode:c},e,{type:t,value:x,onChange:function(e){var t,r,n,a,o,u,i=e.target.value;i=i,t=e,r=k.event,o=U(x,i),o=Object.assign(Object.assign({},o),{lastValue:x}),o=l(i,o),u=F(o),o=l(u,void 0),(g&&!g(j(u,o))?(a=$(n=t.target),a=B(i,x,a),n.value=x,A(n,a,x),1):(T({formattedValue:u,numAsString:o,inputValue:i,event:t,source:r,setCaretPosition:!0,input:t.target}),0))||m(e)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),"ArrowLeft"===n||"Backspace"===n?t=Math.max(a-1,0):"ArrowRight"===n?t=Math.min(a+1,u.length):"Delete"===n&&(t=a),void 0!==t&&a===o&&(a=t,"ArrowLeft"===n||"ArrowRight"===n?(a=i(u,t,"ArrowLeft"===n?"left":"right"))!==t&&e.preventDefault():"Delete"!==n||w(u[t])?"Backspace"!==n||w(u[t])||(a=i(u,t,"left")):a=i(u,t,"right"),a!==t&&A(r,a,u),e.isUnitTestRun&&A(r,a,u)),v(e)},onMouseUp:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;void 0===a&&(a=""),r===n&&(n=i(a,r))!==r&&A(t,n,a),p(e)},onFocus:function(a){a.persist&&a.persist();var o=a.target;E.current=o,C.current.focusTimeout=setTimeout(function(){var e=o.selectionStart,t=o.selectionEnd,r=o.value,n=i(r=void 0===r?"":r,e);n===e||0===e&&t===r.length||A(o,n,r),h(a)},0)},onBlur:function(e){E.current=null,clearTimeout(C.current.focusTimeout),clearTimeout(C.current.setCaretTimeout),S(e)}});return"text"===r?a?M.createElement(M.Fragment,null,a(x,e)||null):M.createElement("span",Object.assign({},e,{ref:o}),x):n?M.createElement(n,Object.assign({},y,{ref:o})):M.createElement("input",Object.assign({},y,{ref:o}))}function C(e,t){var r,n,a,o,u=t.decimalScale,i=t.fixedDecimalScale,l=t.prefix,s=(void 0===l&&(l=""),t.suffix),c=(void 0===s&&(s=""),t.allowNegative),f=t.thousandsGroupStyle;return void 0===f&&(f="thousand"),""!==e&&"-"!==e&&(r=(t=F(t)).thousandSeparator,t=t.decimalSeparator,n=0!==u&&-1!==e.indexOf(".")||u&&i,a=(c=S(e,c)).beforeDecimal,o=c.afterDecimal,c=c.addNegation,void 0!==u&&(o=g(o,u,!!i)),r&&(a=d(a,r,f)),l&&(a=l+a),s&&(o+=s),e=(a=c?"-"+a:a)+(n&&t||"")+o),e}function F(e){var t=e.decimalSeparator,r=e.thousandSeparator,e=e.allowedDecimalSeparators;return{decimalSeparator:t=void 0===t?".":t,thousandSeparator:r=!0===r?",":r,allowedDecimalSeparators:e=e||[t,"."]}}function j(e,t,r){void 0===t&&(t=y(e));var n,a,o,u,i=r.allowNegative,l=r.prefix,s=(void 0===l&&(l=""),r.suffix),c=(void 0===s&&(s=""),r.decimalScale),f=t.from,d=t.to,g=d.start,m=d.end,r=F(r),v=r.allowedDecimalSeparators,r=r.decimalSeparator,p=e[m]===r;return W(e)&&(e===l||e===s)&&""===t.lastValue||(m-g==1&&-1!==v.indexOf(e[g])&&(v=0===c?"":r,e=e.substring(0,g)+v+e.substring(g+1,e.length)),v=(c=function(e,t,r){var n=!1,a=!1,o=(l.startsWith("-")?n=!1:e.startsWith("--")?a=!(n=!1):s.startsWith("-")&&e.length===s.length?n=!1:"-"===e[0]&&(n=!0),n?1:0);return(o=a?2:o)&&(e=e.substring(o),t-=o,r-=o),{value:e,start:t,end:r,hasNegation:n}})(e,g,m),u=v.hasNegation,e=v.value,g=v.start,m=v.end,c=(v=c(t.lastValue,f.start,f.end)).start,t=v.end,v=v.value,n=e.substring(g,m),a=0,(e=!(e.length&&v.length&&(c>v.length-s.length||t<l.length))||n&&s.startsWith(n)?e:v).startsWith(l)?a+=l.length:g<l.length&&(a=g),m-=a,c=(e=e.substring(a)).length,t=e.length-s.length,e.endsWith(s)?c=t:(t<m||m>e.length-s.length)&&(c=m),e=e.substring(0,c),n=i,void 0===(v=u?"-"+e:e)&&(v=""),g=new RegExp("(-)"),a=new RegExp("(-)(.)*(-)"),g=g.test(v),a=a.test(v),v=v.replace(/-/g,""),o=(e=((e=v=g&&!a&&n?"-"+v:v).match((t=!0,new RegExp("(^-)|[0-9]|"+h(r),t?"g":void 0)))||[]).join("")).indexOf(r),c=(m=S(e=e.replace(new RegExp(h(r),"g"),function(e,t){return t===o?".":""}),i)).beforeDecimal,u=m.afterDecimal,g=m.addNegation,d.end-d.start<f.end-f.start&&""===c&&p&&!parseFloat(u)&&(e=g?"-":"")),e}function A(e,t){var r=t.prefix,t=(void 0===r&&(r=""),t.suffix),n=(void 0===t&&(t=""),Array.from({length:e.length+1}).map(function(){return!0})),a="-"===e[0],r=(n.fill(!1,0,r.length+(a?1:0)),e.length);return n.fill(!1,r-t.length+1,r+1),n}function n(i){function e(e){return C(e,i)}function t(e,t){return j(e,t,i)}function r(e){return!O(e)&&("number"==typeof e&&(e=D(e)),b&&"number"==typeof f)?E(e,f,Boolean(d)):e}var n=(i=function(e){var t=(r=F(e)).thousandSeparator,r=r.decimalSeparator,n=e.prefix,a=(void 0===n&&(n=""),e.allowNegative);if(void 0===a&&(a=!0),t===r)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+t+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");return n.startsWith("-")&&a&&(console.error("\n Prefix can't start with '-' when allowNegative is true.\n prefix: "+n+"\n allowNegative: "+a+"\n "),a=!1),Object.assign(Object.assign({},e),{allowNegative:a})}(i)).decimalSeparator,a=(void 0===n&&(n="."),i.allowedDecimalSeparators,i.thousandsGroupStyle,i.suffix),l=i.allowNegative,o=i.allowLeadingZeros,s=i.onKeyDown,u=(void 0===s&&(s=K),i.onBlur),c=(void 0===u&&(u=K),i.thousandSeparator),f=i.decimalScale,d=i.fixedDecimalScale,g=i.prefix,m=(void 0===g&&(g=""),i.defaultValue),v=i.value,p=i.valueIsNumericString,h=i.onValueChange,S=P(i,["decimalSeparator","allowedDecimalSeparators","thousandsGroupStyle","suffix","allowNegative","allowLeadingZeros","onKeyDown","onBlur","thousandSeparator","decimalScale","fixedDecimalScale","prefix","defaultValue","value","valueIsNumericString","onValueChange"]),y=N(v)?m:v,b=null!=p?p:(w=g,a=a,""===(y=y)||!(null!=w&&w.match(/\d/)||null!=a&&a.match(/\d/)||"string"!=typeof y||isNaN(Number(y)))),w=(N(v)?N(m)||(b=p||"number"==typeof m):b=p||"number"==typeof v,_(r(v),r(m),Boolean(b),e,t,h)),a=w[0],x=a.numAsString,y=a.formattedValue,V=w[1];return Object.assign(Object.assign({},S),{value:y,valueIsNumericString:!1,isValidInputCharacter:function(e){return e===n||W(e)},onValueChange:V,format:e,removeFormatting:t,getCaretBoundary:function(e){return A(e,i)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),a===o&&("Backspace"===n&&"-"===u[0]&&a===g.length+1&&l&&L(r,1),t=(o=F(i)).decimalSeparator,o=o.allowedDecimalSeparators,"Backspace"===n&&u[a-1]===t&&f&&d&&(L(r,a-1),e.preventDefault()),null!=o&&o.includes(n)&&u[a]===t&&L(r,a+1),o=!0===c?",":c,"Backspace"===n&&u[a-1]===o&&L(r,a-1),"Delete"===n&&u[a]===o&&L(r,a+1)),s(e)},onBlur:function(e){var t,r,n=x;n.match(/\d/g)||(n=""),o||(n=(r=n)&&((t="-"===r[0])?"-":"")+((t=(r=t?r.substring(1,r.length):r).split("."))[0].replace(/^0+/,"")||"0")+((t=t[1]||"")?"."+t:"")),(n=d&&f?E(n,f,d):n)!==x&&(r=C(n,i),V({formattedValue:r,value:n,floatValue:parseFloat(n)},{event:e,source:k.event})),u(e)}})}function v(e,t){var r=t.format,n=t.allowEmptyFormatting,a=t.mask,o=t.patternChar;if(void 0===o&&(o="#"),""===e&&!n)return"";for(var u=0,i=r.split(""),l=0,s=r.length;l<s;l++)r[l]===o&&(i[l]=e[u]||m(a,u),u+=1);return i.join("")}function p(e,t,r){void 0===t&&(t=y(e));function a(e){return u[e]===i}function n(e,t){for(var r="",n=0;n<e.length;n++)a(t+n)&&W(e[n])&&(r+=e[n]);return r}function o(e){return e.replace(/[^0-9]/g,"")}var u=r.format,i=r.patternChar,r=(void 0===i&&(i="#"),t.from),l=t.to,t=t.lastValue;void 0===t&&(t="");if(!u.match(/\d/))return o(e);if(""===t&&e.length===u.length){for(var s="",c=0;c<e.length;c++)if(a(c))W(e[c])&&(s+=e[c]);else if(e[c]!==u[c])return o(e);return s}var f=t.substring(0,r.start),l=e.substring(l.start,l.end),t=t.substring(r.end);return""+n(f,0)+o(l)+n(t,r.end)}function b(n,e){for(var t=e.format,a=e.mask,o=e.patternChar,r=(void 0===o&&(o="#"),Array.from({length:n.length+1}).map(function(){return!0})),u=0,i=-1,l={},s=(t.split("").forEach(function(e,t){var r=void 0;e===o&&(r=m(a,++u-1),-1===i&&n[t]===r&&(i=t)),l[t]=r}),function(e){return t[e]===o&&n[e]!==l[e]}),c=0,f=r.length;c<f;c++)r[c]=c===i||s(c)||s(c-1);return r[t.indexOf(o)]=!0,r}function o(t){t.mask,t.allowEmptyFormatting;var l=t.format,e=t.inputMode,s=(void 0===e&&(e="numeric"),t.onKeyDown),c=(void 0===s&&(s=K),t.patternChar),r=(void 0===c&&(c="#"),t.value),n=t.defaultValue,a=t.valueIsNumericString,o=P(t,["mask","allowEmptyFormatting","format","inputMode","onKeyDown","patternChar","value","defaultValue","valueIsNumericString"]),u=t;if((u=u.mask)&&("string"===u?u:u.toString()).match(/\d/g))throw new Error("Mask "+u+" should not contain numeric character;");function f(e){return b(e,t)}var u=N(r)?n:r,a=null!=a?a:(a=l,""===(u=u)||!(null!=a&&a.match(/\d/)||"string"!=typeof u||!u.match(/^\d+$/)&&""!==u)),i=Object.assign(Object.assign({},t),{valueIsNumericString:a});return Object.assign(Object.assign({},o),{value:r,defaultValue:n,valueIsNumericString:a,inputMode:e,format:function(e){return v(e,i)},removeFormatting:function(e,t){return p(e,t,i)},getCaretBoundary:f,onKeyDown:function(e){var t=e.key,r=e.target,n=r.selectionStart,a=r.selectionEnd,o=r.value;if(n===a){var u=n;if("Backspace"===t||"Delete"===t){a="right";if("Backspace"===t){for(;0<u&&l[u-1]!==c;)u--;a="left"}else{for(var i=l.length;u<i&&l[u]!==c;)u++;a="right"}u=G(o,u,f(o),a)}else l[u]!==c&&"ArrowLeft"!==t&&"ArrowRight"!==t&&(u=G(o,u+1,f(o),"right"));u!==n&&L(r,u)}s(e)}})}(t=k=k||{}).event="event",t.props="prop",e.NumberFormatBase=r,e.NumericFormat=function(e){return e=n(e),M.createElement(r,Object.assign({},e))},e.PatternFormat=function(e){return e=o(e),M.createElement(r,Object.assign({},e))},e.getNumericCaretBoundary=A,e.getPatternCaretBoundary=b,e.numericFormatter=C,e.patternFormatter=v,e.removeNumericFormat=j,e.removePatternFormat=p,e.useNumericFormat=n,e.usePatternFormat=o,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).NumberFormat={},e.React)}(this,function(e,I){"use strict";var k,t,M="default"in I?I.default:I;function P(e,t){var r={};for(a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(r[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,a=Object.getOwnPropertySymbols(e);n<a.length;n++)t.indexOf(a[n])<0&&Object.prototype.propertyIsEnumerable.call(e,a[n])&&(r[a[n]]=e[a[n]]);return r}function K(){}function W(e){return!!(e||"").match(/\d/)}function N(e){return null==e}function O(e){return N(e)||"number"==typeof(t=e)&&isNaN(t)||"number"==typeof e&&!isFinite(e);var t}function h(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function d(e,t,r){var r=function(e){switch(e){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),n=-1===(n=e.search(/[1-9]/))?e.length:n;return e.substring(0,n)+e.substring(n,e.length).replace(r,"$1"+t)}function S(e,t){void 0===t&&(t=!0);var r="-"===e[0],t=r&&t,e=(e=e.replace("-","")).split(".");return{beforeDecimal:e[0],afterDecimal:e[1]||"",hasNegation:r,addNegation:t}}function g(e,t,r){for(var n="",a=r?"0":"",o=0;o<=t-1;o++)n+=e[o]||a;return n}function a(e,t){return Array(t+1).join(e)}function D(e){var t,e=e+"",r="-"===e[0]?"-":"",e=(e=r?e.substring(1):e).split(/[eE]/g),n=e[0],e=e[1];return(e=Number(e))&&(e=1+e,t=(n=n.replace(".","")).length,e<0?n="0."+a("0",Math.abs(e))+n:t<=e?n+=a("0",e-t):n=(n.substring(0,e)||"0")+"."+n.substring(e)),r+n}function E(e,t,r){var n,a,o,u;return-1!==["","-"].indexOf(e)?e:(n=(-1!==e.indexOf(".")||r)&&t,a=(e=S(e)).beforeDecimal,u=e.afterDecimal,e=e.hasNegation,o=parseFloat("0."+(u||"0")),u=(u.length<=t?"0."+u:o.toFixed(t)).split("."),(e?"-":"")+a.split("").reverse().reduce(function(e,t,r){return e.length>r?(Number(e[0])+Number(t)).toString()+e.substring(1,e.length):t+e},u[0])+(n?".":"")+g(u[1]||"",t,r))}function L(e,t){var r;e.value=e.value,null!==e&&(e.createTextRange?((r=e.createTextRange()).move("character",t),r.select()):e.selectionStart||0===e.selectionStart?(e.focus(),e.setSelectionRange(t,t)):e.focus())}function U(e,t){for(var r=0,n=0,a=e.length,o=t.length;e[r]===t[r]&&r<a;)r++;for(;e[a-1-n]===t[o-1-n]&&r<o-n&&r<a-n;)n++;return{from:{start:r,end:a-n},to:{start:r,end:o-n}}}function $(e){return Math.max(e.selectionStart,e.selectionEnd)}function y(e){return{from:{start:0,end:0},to:{start:0,end:e.length},lastValue:""}}function m(e,t){return"string"==typeof(e=void 0===e?" ":e)?e:e[t]||" "}function G(e,t,r,n){var a,o,u=e.length;if(e=t,a=0,o=u,t=Math.min(Math.max(e,a),o),"left"===n){for(;0<=t&&!r[t];)t--;-1===t&&(t=r.indexOf(!0))}else{for(;t<=u&&!r[t];)t++;u<t&&(t=r.lastIndexOf(!0))}return t=-1===t?u:t}function Z(e){for(var t=Array.from({length:e.length+1}).map(function(){return!0}),r=0,n=t.length;r<n;r++)t[r]=Boolean(W(e[r])||W(e[r-1]));return t}function _(e,t,r,n,a,o){void 0===o&&(o=K);l=function(e,t){var r,t=O(e)?r="":(r="number"==typeof e||t?"number"==typeof e?D(e):e:a(e,void 0),n(r));return{formattedValue:t,numAsString:r}},(u=I.useRef(l)).current=l;var u,i=I.useRef(function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return u.current.apply(u,e)}).current,l=I.useState(function(){return i(N(e)?t:e,r)}),s=l[0],c=l[1],l=I.useRef(s),f=e,d=r,f=(N(e)&&(f=s.numAsString,d=!0),i(f,d));return f.formattedValue!==l.current.formattedValue&&(l.current=f,c(f)),[s,function(e,t){e.formattedValue!==s.formattedValue&&c({formattedValue:e.formattedValue,numAsString:e.value}),o(e,t)}]}function q(e){return e.replace(/[^0-9]/g,"")}function z(e){return e}function r(e){function s(e,t){O.current={formattedValue:e.formattedValue,numAsString:e.value},N(e,t)}function i(e,t,r){return G(e,t,b(e),r)}var t=e.type,r=(void 0===t&&(t="text"),e.displayType),n=(void 0===r&&(r="input"),e.customInput),a=e.renderText,o=e.getInputRef,u=e.format,l=(void 0===u&&(u=z),e.removeFormatting),c=(void 0===l&&(l=q),e.defaultValue),f=e.valueIsNumericString,d=e.onValueChange,g=e.isAllowed,m=e.onChange,v=(void 0===m&&(m=K),e.onKeyDown),p=(void 0===v&&(v=K),e.onMouseUp),h=(void 0===p&&(p=K),e.onFocus),S=(void 0===h&&(h=K),e.onBlur),y=(void 0===S&&(S=K),e.value),b=e.getCaretBoundary,w=(void 0===b&&(b=Z),e.isValidInputCharacter),e=(void 0===w&&(w=W),P(e,["type","displayType","customInput","renderText","getInputRef","format","removeFormatting","defaultValue","valueIsNumericString","onValueChange","isAllowed","onChange","onKeyDown","onMouseUp","onFocus","onBlur","value","getCaretBoundary","isValidInputCharacter"])),y=_(y,c,Boolean(f),u,l,d),c=y[0],x=c.formattedValue,V=c.numAsString,N=y[1],O=I.useRef({formattedValue:x,numAsString:V}),f=I.useState(!1),d=f[0],D=f[1],E=I.useRef(null),C=I.useRef({setCaretTimeout:null,focusTimeout:null}),F=(I.useEffect(function(){return D(!0),function(){clearTimeout(C.current.setCaretTimeout),clearTimeout(C.current.focusTimeout)}},[]),u),j=function(e,t){var r=parseFloat(t);return{formattedValue:e,value:t,floatValue:isNaN(r)?void 0:r}},A=function(e,t,r){0===e.selectionStart&&e.selectionEnd===e.value.length||(L(e,t),C.current.setCaretTimeout=setTimeout(function(){e.value===r&&e.selectionStart!==e.selectionEnd&&L(e,t)},0))},B=function(e,t,r){var n=b(t);return G(t,function(e,t,r,n,a,o){var u=(i=U(r,e)).from,i=i.to;if(u.end-u.start==1&&u.end===i.end&&i.end===n)return n;for(var u=a.findIndex(function(e){return e}),i=e.slice(0,u),l=(t||r.startsWith(i)||(r=i+r,n+=i.length),r.length),s=e.length,c={},f=new Array(l),d=0;d<l;d++){f[d]=-1;for(var g=0,m=s;g<m;g++)if(r[d]===e[g]&&!0!==c[g]){c[f[d]=g]=!0;break}}for(var v=n;v<l&&(-1===f[v]||!o(r[v]));)v++;for(a=v===l||-1===f[v]?s:f[v],v=n-1;0<v&&-1===f[v];)v--;return u=-1===v||-1===f[v]?0:f[v]+1,!(a<u)&&n-u<a-n?u:a}(t,x,e,r,n,w),n)},T=function(e){var t,r=e.formattedValue,n=(void 0===r&&(r=""),e.input),a=e.setCaretPosition,o=(void 0===a&&(a=!0),e.source),u=e.event,i=e.numAsString,l=e.caretPos;n&&(void 0===l&&a&&(e=e.inputValue||n.value,t=$(n),n.value=r,l=B(e,r,t)),n.value=r,a&&void 0!==l&&A(n,l,r)),r!==x&&s(j(r,i),{event:u,source:o})},R=(I.useEffect(function(){var e=O.current,t=e.formattedValue,e=e.numAsString;x===t||x===V&&t===e||s(j(x,V),{event:void 0,source:k.props})},[x,V]),E.current?$(E.current):void 0),c=(I.useLayoutEffect(function(){var e,t=E.current;x!==O.current.formattedValue&&t&&(e=B(O.current.formattedValue,x,R),t.value=x,A(t,e,x))},[x]),d&&!("undefined"==typeof navigator||navigator.platform&&/iPhone|iPod/.test(navigator.platform))?"numeric":void 0),y=Object.assign({inputMode:c},e,{type:t,value:x,onChange:function(e){var t,r,n,a,o,u,i=e.target.value;i=i,t=e,r=k.event,o=U(x,i),o=Object.assign(Object.assign({},o),{lastValue:x}),o=l(i,o),u=F(o),o=l(u,void 0),(g&&!g(j(u,o))?(a=$(n=t.target),a=B(i,x,a),n.value=x,A(n,a,x),1):(T({formattedValue:u,numAsString:o,inputValue:i,event:t,source:r,setCaretPosition:!0,input:t.target}),0))||m(e)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),"ArrowLeft"===n||"Backspace"===n?t=Math.max(a-1,0):"ArrowRight"===n?t=Math.min(a+1,u.length):"Delete"===n&&(t=a),void 0!==t&&a===o&&(a=t,"ArrowLeft"===n||"ArrowRight"===n?(a=i(u,t,"ArrowLeft"===n?"left":"right"))!==t&&e.preventDefault():"Delete"!==n||w(u[t])?"Backspace"!==n||w(u[t])||(a=i(u,t,"left")):a=i(u,t,"right"),a!==t&&A(r,a,u),e.isUnitTestRun&&A(r,a,u)),v(e)},onMouseUp:function(e){var t=e.target,r=t.selectionStart,n=t.selectionEnd,a=t.value;void 0===a&&(a=""),r===n&&(n=i(a,r))!==r&&A(t,n,a),p(e)},onFocus:function(a){a.persist&&a.persist();var o=a.target;E.current=o,C.current.focusTimeout=setTimeout(function(){var e=o.selectionStart,t=o.selectionEnd,r=o.value,n=i(r=void 0===r?"":r,e);n===e||0===e&&t===r.length||A(o,n,r),h(a)},0)},onBlur:function(e){E.current=null,clearTimeout(C.current.focusTimeout),clearTimeout(C.current.setCaretTimeout),S(e)}});return"text"===r?a?M.createElement(M.Fragment,null,a(x,e)||null):M.createElement("span",Object.assign({},e,{ref:o}),x):n?M.createElement(n,Object.assign({},y,{ref:o})):M.createElement("input",Object.assign({},y,{ref:o}))}function C(e,t){var r,n,a,o,u=t.decimalScale,i=t.fixedDecimalScale,l=t.prefix,s=(void 0===l&&(l=""),t.suffix),c=(void 0===s&&(s=""),t.allowNegative),f=t.thousandsGroupStyle;return void 0===f&&(f="thousand"),""!==e&&"-"!==e&&(r=(t=F(t)).thousandSeparator,t=t.decimalSeparator,n=0!==u&&-1!==e.indexOf(".")||u&&i,a=(c=S(e,c)).beforeDecimal,o=c.afterDecimal,c=c.addNegation,void 0!==u&&(o=g(o,u,!!i)),r&&(a=d(a,r,f)),l&&(a=l+a),s&&(o+=s),e=(a=c?"-"+a:a)+(n&&t||"")+o),e}function F(e){var t=e.decimalSeparator,r=e.thousandSeparator,e=e.allowedDecimalSeparators;return{decimalSeparator:t=void 0===t?".":t,thousandSeparator:r=!0===r?",":r,allowedDecimalSeparators:e=e||[t,"."]}}function j(e,t,r){void 0===t&&(t=y(e));var n,a,o,u,i=r.allowNegative,l=r.prefix,s=(void 0===l&&(l=""),r.suffix),c=(void 0===s&&(s=""),r.decimalScale),f=t.from,d=t.to,g=d.start,m=d.end,r=F(r),v=r.allowedDecimalSeparators,r=r.decimalSeparator,p=e[m]===r;return W(e)&&(e===l||e===s)&&""===t.lastValue||(m-g==1&&-1!==v.indexOf(e[g])&&(v=0===c?"":r,e=e.substring(0,g)+v+e.substring(g+1,e.length)),v=(c=function(e,t,r){var n=!1,a=!1,o=(l.startsWith("-")?n=!1:e.startsWith("--")?a=!(n=!1):s.startsWith("-")&&e.length===s.length?n=!1:"-"===e[0]&&(n=!0),n?1:0);return(o=a?2:o)&&(e=e.substring(o),t-=o,r-=o),{value:e,start:t,end:r,hasNegation:n}})(e,g,m),u=v.hasNegation,e=v.value,g=v.start,m=v.end,c=(v=c(t.lastValue,f.start,f.end)).start,t=v.end,v=v.value,n=e.substring(g,m),a=0,(e=!(e.length&&v.length&&(c>v.length-s.length||t<l.length))||n&&s.startsWith(n)?e:v).startsWith(l)?a+=l.length:g<l.length&&(a=g),m-=a,c=(e=e.substring(a)).length,t=e.length-s.length,e.endsWith(s)?c=t:(t<m||m>e.length-s.length)&&(c=m),e=e.substring(0,c),n=i,void 0===(v=u?"-"+e:e)&&(v=""),g=new RegExp("(-)"),a=new RegExp("(-)(.)*(-)"),g=g.test(v),a=a.test(v),v=v.replace(/-/g,""),o=(e=((e=v=g&&!a&&n?"-"+v:v).match((t=!0,new RegExp("(^-)|[0-9]|"+h(r),t?"g":void 0)))||[]).join("")).indexOf(r),c=(m=S(e=e.replace(new RegExp(h(r),"g"),function(e,t){return t===o?".":""}),i)).beforeDecimal,u=m.afterDecimal,g=m.addNegation,d.end-d.start<f.end-f.start&&""===c&&p&&!parseFloat(u)&&(e=g?"-":"")),e}function A(e,t){var r=t.prefix,t=(void 0===r&&(r=""),t.suffix),n=(void 0===t&&(t=""),Array.from({length:e.length+1}).map(function(){return!0})),a="-"===e[0],r=(n.fill(!1,0,r.length+(a?1:0)),e.length);return n.fill(!1,r-t.length+1,r+1),n}function n(i){function e(e){return C(e,i)}function t(e,t){return j(e,t,i)}function r(e){return!O(e)&&("number"==typeof e&&(e=D(e)),b&&"number"==typeof f)?E(e,f,Boolean(d)):e}var n=(i=function(e){var t=(r=F(e)).thousandSeparator,r=r.decimalSeparator,n=e.prefix,a=(void 0===n&&(n=""),e.allowNegative);if(void 0===a&&(a=!0),t===r)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+t+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");return n.startsWith("-")&&a&&(console.error("\n Prefix can't start with '-' when allowNegative is true.\n prefix: "+n+"\n allowNegative: "+a+"\n "),a=!1),Object.assign(Object.assign({},e),{allowNegative:a})}(i)).decimalSeparator,a=(void 0===n&&(n="."),i.allowedDecimalSeparators,i.thousandsGroupStyle,i.suffix),l=i.allowNegative,o=i.allowLeadingZeros,s=i.onKeyDown,u=(void 0===s&&(s=K),i.onBlur),c=(void 0===u&&(u=K),i.thousandSeparator),f=i.decimalScale,d=i.fixedDecimalScale,g=i.prefix,m=(void 0===g&&(g=""),i.defaultValue),v=i.value,p=i.valueIsNumericString,h=i.onValueChange,S=P(i,["decimalSeparator","allowedDecimalSeparators","thousandsGroupStyle","suffix","allowNegative","allowLeadingZeros","onKeyDown","onBlur","thousandSeparator","decimalScale","fixedDecimalScale","prefix","defaultValue","value","valueIsNumericString","onValueChange"]),y=N(v)?m:v,b=null!=p?p:(w=g,a=a,""===(y=y)||!(null!=w&&w.match(/\d/)||null!=a&&a.match(/\d/)||"string"!=typeof y||isNaN(Number(y)))),w=(N(v)?N(m)||(b=p||"number"==typeof m):b=p||"number"==typeof v,_(r(v),r(m),Boolean(b),e,t,h)),a=w[0],x=a.numAsString,y=a.formattedValue,V=w[1];return Object.assign(Object.assign({},S),{value:y,valueIsNumericString:!1,isValidInputCharacter:function(e){return e===n||W(e)},onValueChange:V,format:e,removeFormatting:t,getCaretBoundary:function(e){return A(e,i)},onKeyDown:function(e){var t,r=e.target,n=e.key,a=r.selectionStart,o=r.selectionEnd,u=r.value;void 0===u&&(u=""),a===o&&("Backspace"===n&&"-"===u[0]&&a===g.length+1&&l&&L(r,1),t=(o=F(i)).decimalSeparator,o=o.allowedDecimalSeparators,"Backspace"===n&&u[a-1]===t&&f&&d&&(L(r,a-1),e.preventDefault()),null!=o&&o.includes(n)&&u[a]===t&&L(r,a+1),o=!0===c?",":c,"Backspace"===n&&u[a-1]===o&&L(r,a-1),"Delete"===n&&u[a]===o&&L(r,a+1)),s(e)},onBlur:function(e){var t,r,n=x;n.match(/\d/g)||(n=""),o||(n=(r=n)&&((t="-"===r[0])?"-":"")+((t=(r=t?r.substring(1,r.length):r).split("."))[0].replace(/^0+/,"")||"0")+((t=t[1]||"")?"."+t:"")),(n=d&&f?E(n,f,d):n)!==x&&(r=C(n,i),V({formattedValue:r,value:n,floatValue:parseFloat(n)},{event:e,source:k.event})),u(e)}})}function v(e,t){var r=t.format,n=t.allowEmptyFormatting,a=t.mask,o=t.patternChar;if(void 0===o&&(o="#"),""===e&&!n)return"";for(var u=0,i=r.split(""),l=0,s=r.length;l<s;l++)r[l]===o&&(i[l]=e[u]||m(a,u),u+=1);return i.join("")}function p(e,t,r){void 0===t&&(t=y(e));function a(e){return u[e]===i}function n(e,t){for(var r="",n=0;n<e.length;n++)a(t+n)&&W(e[n])&&(r+=e[n]);return r}function o(e){return e.replace(/[^0-9]/g,"")}var u=r.format,i=r.patternChar,r=(void 0===i&&(i="#"),t.from),l=t.to,t=t.lastValue;void 0===t&&(t="");if(!u.match(/\d/))return o(e);if(""===t&&e.length===u.length){for(var s="",c=0;c<e.length;c++)if(a(c))W(e[c])&&(s+=e[c]);else if(e[c]!==u[c])return o(e);return s}var f=t.substring(0,r.start),l=e.substring(l.start,l.end),t=t.substring(r.end);return""+n(f,0)+o(l)+n(t,r.end)}function b(n,e){for(var t=e.format,a=e.mask,o=e.patternChar,r=(void 0===o&&(o="#"),Array.from({length:n.length+1}).map(function(){return!0})),u=0,i=-1,l={},s=(t.split("").forEach(function(e,t){var r=void 0;e===o&&(r=m(a,++u-1),-1===i&&n[t]===r&&(i=t)),l[t]=r}),function(e){return t[e]===o&&n[e]!==l[e]}),c=0,f=r.length;c<f;c++)r[c]=c===i||s(c)||s(c-1);return r[t.indexOf(o)]=!0,r}function o(t){t.mask,t.allowEmptyFormatting;var l=t.format,e=t.inputMode,s=(void 0===e&&(e="numeric"),t.onKeyDown),c=(void 0===s&&(s=K),t.patternChar),r=(void 0===c&&(c="#"),t.value),n=t.defaultValue,a=t.valueIsNumericString,o=P(t,["mask","allowEmptyFormatting","format","inputMode","onKeyDown","patternChar","value","defaultValue","valueIsNumericString"]),u=t;if((u=u.mask)&&("string"===u?u:u.toString()).match(/\d/g))throw new Error("Mask "+u+" should not contain numeric character;");function f(e){return b(e,t)}var u=N(r)?n:r,a=null!=a?a:(a=l,""===(u=u)||!(null!=a&&a.match(/\d/)||"string"!=typeof u||!u.match(/^\d+$/)&&""!==u)),i=Object.assign(Object.assign({},t),{valueIsNumericString:a});return Object.assign(Object.assign({},o),{value:r,defaultValue:n,valueIsNumericString:a,inputMode:e,format:function(e){return v(e,i)},removeFormatting:function(e,t){return p(e,t,i)},getCaretBoundary:f,onKeyDown:function(e){var t=e.key,r=e.target,n=r.selectionStart,a=r.selectionEnd,o=r.value;if(n===a){var u=n;if("Backspace"===t||"Delete"===t){a="right";if("Backspace"===t){for(;0<u&&l[u-1]!==c;)u--;a="left"}else{for(var i=l.length;u<i&&l[u]!==c;)u++;a="right"}u=G(o,u,f(o),a)}else l[u]!==c&&"ArrowLeft"!==t&&"ArrowRight"!==t&&(u=G(o,u+1,f(o),"right"));u!==n&&L(r,u)}s(e)}})}(t=k=k||{}).event="event",t.props="prop",e.NumberFormatBase=r,e.NumericFormat=function(e){return e=n(e),M.createElement(r,Object.assign({},e))},e.PatternFormat=function(e){return e=o(e),M.createElement(r,Object.assign({},e))},e.getNumericCaretBoundary=A,e.getPatternCaretBoundary=b,e.numericFormatter=C,e.patternFormatter=v,e.removeNumericFormat=j,e.removePatternFormat=p,e.useNumericFormat=n,e.usePatternFormat=o,Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "react-number-format", | ||
"description": "React component to format number in an input or as a text.", | ||
"version": "5.2.0", | ||
"version": "5.2.1", | ||
"main": "dist/react-number-format.cjs.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/react-number-format.es.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231878