Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-number-format

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-number-format - npm Package Compare versions

Comparing version 4.6.1 to 4.6.2

4

dist/react-number-format.cjs.js
/**
* react-number-format - 4.6.1
* react-number-format - 4.6.2
* Author : Sudhanshu Yadav

@@ -251,3 +251,3 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license.

var floatValueStr =
afterDecimal.length <= scale ? toNumericString(floatValue) : floatValue.toFixed(scale);
afterDecimal.length <= scale ? ("0." + afterDecimal) : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');

@@ -254,0 +254,0 @@ var intPart = beforeDecimal

/**
* react-number-format - 4.6.1
* react-number-format - 4.6.2
* Author : Sudhanshu Yadav

@@ -247,3 +247,3 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license.

var floatValueStr =
afterDecimal.length <= scale ? toNumericString(floatValue) : floatValue.toFixed(scale);
afterDecimal.length <= scale ? ("0." + afterDecimal) : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');

@@ -250,0 +250,0 @@ var intPart = beforeDecimal

/**
* react-number-format - 4.6.1
* react-number-format - 4.6.2
* Author : Sudhanshu Yadav

@@ -253,3 +253,3 @@ * Copyright (c) 2016, 2021 to Sudhanshu Yadav, released under the MIT license.

var floatValueStr =
afterDecimal.length <= scale ? toNumericString(floatValue) : floatValue.toFixed(scale);
afterDecimal.length <= scale ? ("0." + afterDecimal) : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');

@@ -256,0 +256,0 @@ var intPart = beforeDecimal

@@ -1,1 +0,1 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).NumberFormat=e(t.React)}(this,function(g){"use strict";g=g&&g.hasOwnProperty("default")?g.default:g;function n(){}function o(){}o.resetWarningCache=n;var t,e=(function(t){t.exports=function(){function t(t,e,r,n,o,a){if("SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==a){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}function e(){return t}var r={array:t.isRequired=t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,elementType:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e,checkPropTypes:o,resetWarningCache:n};return r.PropTypes=r}()}(t={exports:{}},t.exports),t.exports);function r(){}function c(t){return!!(t||"").match(/\d/)}function f(t){return null==t}function i(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function R(t,e){void 0===e&&(e=!0);var r="-"===t[0],n=r&&e,o=(t=t.replace("-","")).split(".");return{beforeDecimal:o[0],afterDecimal:o[1]||"",hasNagation:r,addNegation:n}}function d(t,e,r){for(var n="",o=r?"0":"",a=0;a<=e-1;a++)n+=t[a]||o;return n}function s(t,e){return Array(e+1).join(t)}function p(t){var e="-"===(t+="")[0]?"-":"";e&&(t=t.substring(1));var r=t.split(/[eE]/g),n=r[0],o=r[1];if(!(o=Number(o)))return e+n;var a=1+o,i=(n=n.replace(".","")).length;return a<0?n="0."+s("0",Math.abs(a))+n:i<=a?n+=s("0",a-i):n=(n.substring(0,a)||"0")+"."+n.substring(a),e+n}function a(t,e){if(t.value=t.value,null!==t){if(t.createTextRange){var r=t.createTextRange();return r.move("character",e),r.select(),!0}return t.selectionStart||0===t.selectionStart?(t.focus(),t.setSelectionRange(e,e),!0):(t.focus(),!1)}}function h(t,e,r){return Math.min(Math.max(t,e),r)}function m(t){return Math.max(t.selectionStart,t.selectionEnd)}var v={thousandSeparator:e.oneOfType([e.string,e.oneOf([!0])]),decimalSeparator:e.string,allowedDecimalSeparators:e.arrayOf(e.string),thousandsGroupStyle:e.oneOf(["thousand","lakh","wan"]),decimalScale:e.number,fixedDecimalScale:e.bool,displayType:e.oneOf(["input","text"]),prefix:e.string,suffix:e.string,format:e.oneOfType([e.string,e.func]),removeFormatting:e.func,mask:e.oneOfType([e.string,e.arrayOf(e.string)]),value:e.oneOfType([e.number,e.string]),defaultValue:e.oneOfType([e.number,e.string]),isNumericString:e.bool,customInput:e.elementType,allowNegative:e.bool,allowEmptyFormatting:e.bool,allowLeadingZeros:e.bool,onValueChange:e.func,onKeyDown:e.func,onMouseUp:e.func,onChange:e.func,onFocus:e.func,onBlur:e.func,type:e.oneOf(["text","tel","password"]),isAllowed:e.func,renderText:e.func,getInputRef:e.oneOfType([e.func,e.shape({current:e.any})])},u={displayType:"input",decimalSeparator:".",thousandsGroupStyle:"thousand",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,allowEmptyFormatting:!1,allowLeadingZeros:!1,isNumericString:!1,type:"text",onValueChange:r,onChange:r,onKeyDown:r,onMouseUp:r,onFocus:r,onBlur:r,isAllowed:function(){return!0}},l=function(n){function t(t){n.call(this,t);var e=t.defaultValue;this.validateProps();var r=this.formatValueProp(e);this.state={value:r,numAsString:this.removeFormatting(r),mounted:!1},this.selectionBeforeInput={selectionStart:0,selectionEnd:0},this.onChange=this.onChange.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onFocus=this.onFocus.bind(this),this.onBlur=this.onBlur.bind(this)}return n&&(t.__proto__=n),((t.prototype=Object.create(n&&n.prototype)).constructor=t).prototype.componentDidMount=function(){this.setState({mounted:!0})},t.prototype.componentDidUpdate=function(t){this.updateValueIfRequired(t)},t.prototype.componentWillUnmount=function(){clearTimeout(this.focusTimeout)},t.prototype.updateValueIfRequired=function(t){var e=this.props,r=this.state,n=this.focusedElm,o=r.value,a=r.numAsString;if(void 0===a&&(a=""),t!==e){this.validateProps();var i=this.formatNumString(a),s=f(e.value)?i:this.formatValueProp(),u=this.removeFormatting(s),l=parseFloat(u),p=parseFloat(a);(isNaN(l)&&isNaN(p)||l===p)&&i===o&&(null!==n||s===o)||this.updateValue({formattedValue:s,numAsString:u,input:n})}},t.prototype.getFloatString=function(t){void 0===t&&(t="");var e=this.props.decimalScale,r=this.getSeparators().decimalSeparator,n=this.getNumberRegex(!0),o="-"===t[0];o&&(t=t.replace("-","")),r&&0===e&&(t=t.split(r)[0]);var a=(t=(t.match(n)||[]).join("").replace(r,".")).indexOf(".");return-1!==a&&(t=t.substring(0,a)+"."+t.substring(a+1,t.length).replace(new RegExp(i(r),"g"),"")),o&&(t="-"+t),t},t.prototype.getNumberRegex=function(t,e){var r=this.props,n=r.format,o=r.decimalScale,a=this.getSeparators().decimalSeparator;return new RegExp("\\d"+(!a||0===o||e||n?"":"|"+i(a)),t?"g":void 0)},t.prototype.getSeparators=function(){var t=this.props.decimalSeparator,e=this.props,r=e.thousandSeparator,n=e.allowedDecimalSeparators;return!0===r&&(r=","),{decimalSeparator:t,thousandSeparator:r,allowedDecimalSeparators:n=n||[t,"."]}},t.prototype.getMaskAtIndex=function(t){var e=this.props.mask;return void 0===e&&(e=" "),"string"==typeof e?e:e[t]||" "},t.prototype.getValueObject=function(t,e){var r=parseFloat(e);return{formattedValue:t,value:e,floatValue:isNaN(r)?void 0:r}},t.prototype.validateProps=function(){var t=this.props.mask,e=this.getSeparators(),r=e.decimalSeparator,n=e.thousandSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+n+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(t&&("string"===t?t:t.toString()).match(/\d/g))throw new Error("\n Mask "+t+" should not contain numeric character;\n ")},t.prototype.setPatchedCaretPosition=function(t,e,r){a(t,e),setTimeout(function(){t.value===r&&a(t,e)},0)},t.prototype.correctCaretPosition=function(t,e,r){var n=this.props,o=n.prefix,a=n.suffix,i=n.format;if(""===t)return 0;if(e=h(e,0,t.length),!i){var s="-"===t[0];return h(e,o.length+(s?1:0),t.length-a.length)}if("function"==typeof i)return e;if("#"===i[e]&&c(t[e]))return e;if("#"===i[e-1]&&c(t[e-1]))return e;var u=i.indexOf("#");e=h(e,u,i.lastIndexOf("#")+1);for(var l=i.substring(e,i.length).indexOf("#"),p=e,f=e+(-1===l?0:l);u<p&&("#"!==i[p]||!c(t[p]));)p-=1;return!c(t[f])||"left"===r&&e!==u||e-p<f-e?c(t[p])?p+1:p:f},t.prototype.getCaretPosition=function(t,e,r){var n,o,a=this.props.format,i=this.state.value,s=this.getNumberRegex(!0),u=(t.match(s)||[]).join(""),l=(e.match(s)||[]).join("");for(o=n=0;o<r;o++){var p=t[o]||"",f=e[n]||"";if((p.match(s)||p===f)&&("0"!==p||!f.match(s)||"0"===f||u.length===l.length)){for(;p!==e[n]&&n<e.length;)n++;n++}}return"string"!=typeof a||i||(n=e.length),n=this.correctCaretPosition(e,n)},t.prototype.removePrefixAndSuffix=function(t){var e=this.props,r=e.format,n=e.prefix,o=e.suffix;if(!r&&t){var a="-"===t[0];a&&(t=t.substring(1,t.length));var i=(t=n&&0===t.indexOf(n)?t.substring(n.length,t.length):t).lastIndexOf(o);t=o&&-1!==i&&i===t.length-o.length?t.substring(0,i):t,a&&(t="-"+t)}return t},t.prototype.removePatternFormatting=function(t){for(var e=this.props.format.split("#").filter(function(t){return""!==t}),r=0,n="",o=0,a=e.length;o<=a;o++){var i=e[o]||"",s=o===a?t.length:t.indexOf(i,r);if(-1===s){n=t;break}n+=t.substring(r,s),r=s+i.length}return(n.match(/\d/g)||[]).join("")},t.prototype.removeFormatting=function(t){var e=this.props,r=e.format,n=e.removeFormatting;return t?t=r?"string"==typeof r?this.removePatternFormatting(t):"function"==typeof n?n(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),this.getFloatString(t)):t},t.prototype.formatWithPattern=function(t){for(var e=this.props.format,r=0,n=e.split(""),o=0,a=e.length;o<a;o++)"#"===e[o]&&(n[o]=t[r]||this.getMaskAtIndex(r),r+=1);return n.join("")},t.prototype.formatAsNumber=function(t){var e=this.props,r=e.decimalScale,n=e.fixedDecimalScale,o=e.prefix,a=e.suffix,i=e.allowNegative,s=e.thousandsGroupStyle,u=this.getSeparators(),l=u.thousandSeparator,p=u.decimalSeparator,f=-1!==t.indexOf(".")||r&&n,c=R(t,i),h=c.beforeDecimal,g=c.afterDecimal,m=c.addNegation;return void 0!==r&&(g=d(g,r,n)),l&&(h=function(t,e,r){var n=function(t){switch(t){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;case"thousand":default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),o=t.search(/[1-9]/);return o=-1===o?t.length:o,t.substring(0,o)+t.substring(o,t.length).replace(n,"$1"+e)}(h,l,s)),o&&(h=o+h),a&&(g+=a),m&&(h="-"+h),t=h+(f&&p||"")+g},t.prototype.formatNumString=function(t){void 0===t&&(t="");var e=this.props,r=e.format,n=e.allowEmptyFormatting,o=t;return o=""!==t||n?"-"!==t||r?"string"==typeof r?this.formatWithPattern(o):"function"==typeof r?r(o):this.formatAsNumber(o):"-":""},t.prototype.formatValueProp=function(t){var e=this.props,r=e.format,n=e.decimalScale,o=e.fixedDecimalScale,a=e.allowEmptyFormatting,i=this.props,s=i.value,u=i.isNumericString,l=!(s=f(s)?t:s)&&0!==s;return l&&a&&(s=""),l&&!a?"":("number"==typeof s&&(s=p(s),u=!0),"Infinity"===s&&u&&(s=""),u&&!r&&"number"==typeof n&&(s=function(t,e,r){if(-1!==["","-"].indexOf(t))return t;var n=-1!==t.indexOf(".")&&e,o=R(t),a=o.beforeDecimal,i=o.afterDecimal,s=o.hasNagation,u=parseFloat("0."+(i||"0")),l=(i.length<=e?p(u):u.toFixed(e)).split(".");return(s?"-":"")+a.split("").reverse().reduce(function(t,e,r){return t.length>r?(Number(t[0])+Number(e)).toString()+t.substring(1,t.length):e+t},l[0])+(n?".":"")+d(l[1]||"",Math.min(e,i.length),r)}(s,n,o)),u?this.formatNumString(s):this.formatInput(s))},t.prototype.formatNegation=function(t){void 0===t&&(t="");var e=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),o=r.test(t),a=n.test(t);return t=t.replace(/-/g,""),o&&!a&&e&&(t="-"+t),t},t.prototype.formatInput=function(t){return void 0===t&&(t=""),this.props.format||(t=this.removePrefixAndSuffix(t),t=this.formatNegation(t)),t=this.removeFormatting(t),this.formatNumString(t)},t.prototype.isCharacterAFormat=function(t,e){var r=this.props,n=r.format,o=r.prefix,a=r.suffix,i=r.decimalScale,s=r.fixedDecimalScale,u=this.getSeparators().decimalSeparator;return"string"==typeof n&&"#"!==n[t]||!(n||!(t<o.length||t>=e.length-a.length||i&&s&&e[t]===u))},t.prototype.correctInputValue=function(t,r,e){var n=this,o=this.props,a=o.format,i=o.allowNegative,s=o.prefix,u=o.suffix,l=o.decimalScale,p=this.getSeparators(),f=p.allowedDecimalSeparators,c=p.decimalSeparator,h=this.state.numAsString||"",g=this.selectionBeforeInput,m=g.selectionStart,d=g.selectionEnd,v=function(t,e){for(var r=0,n=0,o=t.length,a=e.length;t[r]===e[r]&&r<o;)r++;for(;t[o-1-n]===e[a-1-n]&&r<a-n&&r<o-n;)n++;return{start:r,end:o-n}}(r,e),y=v.start,S=v.end;if(!a&&y===S&&-1!==f.indexOf(e[m])){var x=0===l?"":c;return e.substr(0,m)+x+e.substr(m+1,e.length)}var b=a?0:s.length,w=r.length-(a?0:u.length);if(e.length>r.length||!e.length||y===S||0===m&&d===r.length||0===y&&S===r.length||m===b&&d===w)return e;var P=r.substr(y,S-y);if(!![].concat(P).find(function(t,e){return n.isCharacterAFormat(e+y,r)})){var O=r.substr(y),N={},F=[];[].concat(O).forEach(function(t,e){n.isCharacterAFormat(e+y,r)?N[e]=t:e>P.length-1&&F.push(t)}),Object.keys(N).forEach(function(t){F.length>t?F.splice(t,0,N[t]):F.push(N[t])}),e=r.substr(0,y)+F.join("")}if(!a){var C=this.removeFormatting(e),D=R(C,i),E=D.beforeDecimal,T=D.afterDecimal,V=D.addNegation,A=t<e.indexOf(c)+1;if(C.length<h.length&&A&&""===E&&!parseFloat(T))return V?"-":""}return e},t.prototype.updateValue=function(t){var e=t.formattedValue,r=t.input,n=t.setCaretPosition;void 0===n&&(n=!0);var o=t.numAsString,a=t.caretPos,i=this.props.onValueChange,s=this.state.value;if(r)if(n){if(!a){var u=t.inputValue||r.value,l=m(r);r.value=e,a=this.getCaretPosition(u,e,l)}this.setPatchedCaretPosition(r,a,e)}else r.value=e;void 0===o&&(o=this.removeFormatting(e)),e!==s&&(this.setState({value:e,numAsString:o}),i(this.getValueObject(e,o)))},t.prototype.onChange=function(t){var e=t.target,r=e.value,n=this.state,o=this.props,a=o.isAllowed,i=n.value||"",s=m(e);r=this.correctInputValue(s,i,r);var u=this.formatInput(r)||"",l=this.removeFormatting(u),p=a(this.getValueObject(u,l));p||(u=i),this.updateValue({formattedValue:u,numAsString:l,inputValue:r,input:e}),p&&o.onChange(t)},t.prototype.onBlur=function(t){var e=this.props,r=this.state,n=e.format,o=e.onBlur,a=e.allowLeadingZeros,i=r.numAsString,s=r.value;if(this.focusedElm=null,clearTimeout(this.focusTimeout),!n){isNaN(parseFloat(i))&&(i=""),a||(i=function(t){if(!t)return t;var e="-"===t[0];e&&(t=t.substring(1,t.length));var r=t.split("."),n=r[0].replace(/^0+/,"")||"0",o=r[1]||"";return(e?"-":"")+n+(o?"."+o:"")}(i));var u=this.formatNumString(i);if(u!==s)return this.updateValue({formattedValue:u,numAsString:i,input:t.target,setCaretPosition:!1}),void o(t)}o(t)},t.prototype.onKeyDown=function(t){var e,r=t.target,n=t.key,o=r.selectionStart,a=r.selectionEnd,i=r.value;void 0===i&&(i="");var s=this.props,u=s.decimalScale,l=s.fixedDecimalScale,p=s.prefix,f=s.suffix,c=s.format,h=s.onKeyDown,g=void 0!==u&&l,m=this.getNumberRegex(!1,g),d=new RegExp("-"),v="string"==typeof c;if(this.selectionBeforeInput={selectionStart:o,selectionEnd:a},"ArrowLeft"===n||"Backspace"===n?e=o-1:"ArrowRight"===n?e=o+1:"Delete"===n&&(e=o),void 0!==e&&o===a){var y=e,S=v?c.indexOf("#"):p.length,x=v?c.lastIndexOf("#")+1:i.length-f.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(i,e,b)}else if("Delete"!==n||m.test(i[e])||d.test(i[e])){if("Backspace"===n&&!m.test(i[e]))if(o<=S+1&&"-"===i[0]&&void 0===c){var w=i.substring(1);this.updateValue({formattedValue:w,caretPos:y,input:r})}else if(!d.test(i[e])){for(;!m.test(i[y-1])&&S<y;)y--;y=this.correctCaretPosition(i,y,"left")}}else for(;!m.test(i[y])&&y<x;)y++;(y!==e||e<S||x<e)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,i)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,i),h(t)}else h(t)},t.prototype.onMouseUp=function(t){var e=t.target,r=e.selectionStart,n=e.selectionEnd,o=e.value;if(void 0===o&&(o=""),r===n){var a=this.correctCaretPosition(o,r);a!==r&&this.setPatchedCaretPosition(e,a,o)}this.props.onMouseUp(t)},t.prototype.onFocus=function(a){var i=this;a.persist(),this.focusedElm=a.target,this.focusTimeout=setTimeout(function(){var t=a.target,e=t.selectionStart,r=t.selectionEnd,n=t.value;void 0===n&&(n="");var o=i.correctCaretPosition(n,e);o===e||0===e&&r===n.length||i.setPatchedCaretPosition(t,o,n),i.props.onFocus(a)},0)},t.prototype.render=function(){var t=this.props,e=t.type,r=t.displayType,n=t.customInput,o=t.renderText,a=t.getInputRef,i=t.format,s=this.state,u=s.value,l=s.mounted,p=function(e,r){var n={};return Object.keys(e).forEach(function(t){r[t]||(n[t]=e[t])}),n}(this.props,v),f=l&&function(t){return t||!(navigator.platform&&/iPhone|iPod/.test(navigator.platform))}(i)?"numeric":void 0,c=Object.assign({inputMode:f},p,{type:e,value:u,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return o?o(u,p)||null:g.createElement("span",Object.assign({},p,{ref:a}),u);if(n){var h=n;return g.createElement(h,Object.assign({},c,{ref:a}))}return g.createElement("input",Object.assign({},c,{ref:a}))},t}(g.Component);return l.propTypes=v,l.defaultProps=u,l});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).NumberFormat=e(t.React)}(this,function(g){"use strict";g=g&&g.hasOwnProperty("default")?g.default:g;function n(){}function o(){}o.resetWarningCache=n;var t,e=(function(t){t.exports=function(){function t(t,e,r,n,o,a){if("SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==a){var i=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw i.name="Invariant Violation",i}}function e(){return t}var r={array:t.isRequired=t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,elementType:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e,checkPropTypes:o,resetWarningCache:n};return r.PropTypes=r}()}(t={exports:{}},t.exports),t.exports);function r(){}function c(t){return!!(t||"").match(/\d/)}function f(t){return null==t}function i(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function R(t,e){void 0===e&&(e=!0);var r="-"===t[0],n=r&&e,o=(t=t.replace("-","")).split(".");return{beforeDecimal:o[0],afterDecimal:o[1]||"",hasNagation:r,addNegation:n}}function d(t,e,r){for(var n="",o=r?"0":"",a=0;a<=e-1;a++)n+=t[a]||o;return n}function p(t,e){return Array(e+1).join(t)}function a(t,e){if(t.value=t.value,null!==t){if(t.createTextRange){var r=t.createTextRange();return r.move("character",e),r.select(),!0}return t.selectionStart||0===t.selectionStart?(t.focus(),t.setSelectionRange(e,e),!0):(t.focus(),!1)}}function h(t,e,r){return Math.min(Math.max(t,e),r)}function m(t){return Math.max(t.selectionStart,t.selectionEnd)}var v={thousandSeparator:e.oneOfType([e.string,e.oneOf([!0])]),decimalSeparator:e.string,allowedDecimalSeparators:e.arrayOf(e.string),thousandsGroupStyle:e.oneOf(["thousand","lakh","wan"]),decimalScale:e.number,fixedDecimalScale:e.bool,displayType:e.oneOf(["input","text"]),prefix:e.string,suffix:e.string,format:e.oneOfType([e.string,e.func]),removeFormatting:e.func,mask:e.oneOfType([e.string,e.arrayOf(e.string)]),value:e.oneOfType([e.number,e.string]),defaultValue:e.oneOfType([e.number,e.string]),isNumericString:e.bool,customInput:e.elementType,allowNegative:e.bool,allowEmptyFormatting:e.bool,allowLeadingZeros:e.bool,onValueChange:e.func,onKeyDown:e.func,onMouseUp:e.func,onChange:e.func,onFocus:e.func,onBlur:e.func,type:e.oneOf(["text","tel","password"]),isAllowed:e.func,renderText:e.func,getInputRef:e.oneOfType([e.func,e.shape({current:e.any})])},s={displayType:"input",decimalSeparator:".",thousandsGroupStyle:"thousand",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,allowEmptyFormatting:!1,allowLeadingZeros:!1,isNumericString:!1,type:"text",onValueChange:r,onChange:r,onKeyDown:r,onMouseUp:r,onFocus:r,onBlur:r,isAllowed:function(){return!0}},u=function(n){function t(t){n.call(this,t);var e=t.defaultValue;this.validateProps();var r=this.formatValueProp(e);this.state={value:r,numAsString:this.removeFormatting(r),mounted:!1},this.selectionBeforeInput={selectionStart:0,selectionEnd:0},this.onChange=this.onChange.bind(this),this.onKeyDown=this.onKeyDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onFocus=this.onFocus.bind(this),this.onBlur=this.onBlur.bind(this)}return n&&(t.__proto__=n),((t.prototype=Object.create(n&&n.prototype)).constructor=t).prototype.componentDidMount=function(){this.setState({mounted:!0})},t.prototype.componentDidUpdate=function(t){this.updateValueIfRequired(t)},t.prototype.componentWillUnmount=function(){clearTimeout(this.focusTimeout)},t.prototype.updateValueIfRequired=function(t){var e=this.props,r=this.state,n=this.focusedElm,o=r.value,a=r.numAsString;if(void 0===a&&(a=""),t!==e){this.validateProps();var i=this.formatNumString(a),s=f(e.value)?i:this.formatValueProp(),u=this.removeFormatting(s),l=parseFloat(u),p=parseFloat(a);(isNaN(l)&&isNaN(p)||l===p)&&i===o&&(null!==n||s===o)||this.updateValue({formattedValue:s,numAsString:u,input:n})}},t.prototype.getFloatString=function(t){void 0===t&&(t="");var e=this.props.decimalScale,r=this.getSeparators().decimalSeparator,n=this.getNumberRegex(!0),o="-"===t[0];o&&(t=t.replace("-","")),r&&0===e&&(t=t.split(r)[0]);var a=(t=(t.match(n)||[]).join("").replace(r,".")).indexOf(".");return-1!==a&&(t=t.substring(0,a)+"."+t.substring(a+1,t.length).replace(new RegExp(i(r),"g"),"")),o&&(t="-"+t),t},t.prototype.getNumberRegex=function(t,e){var r=this.props,n=r.format,o=r.decimalScale,a=this.getSeparators().decimalSeparator;return new RegExp("\\d"+(!a||0===o||e||n?"":"|"+i(a)),t?"g":void 0)},t.prototype.getSeparators=function(){var t=this.props.decimalSeparator,e=this.props,r=e.thousandSeparator,n=e.allowedDecimalSeparators;return!0===r&&(r=","),{decimalSeparator:t,thousandSeparator:r,allowedDecimalSeparators:n=n||[t,"."]}},t.prototype.getMaskAtIndex=function(t){var e=this.props.mask;return void 0===e&&(e=" "),"string"==typeof e?e:e[t]||" "},t.prototype.getValueObject=function(t,e){var r=parseFloat(e);return{formattedValue:t,value:e,floatValue:isNaN(r)?void 0:r}},t.prototype.validateProps=function(){var t=this.props.mask,e=this.getSeparators(),r=e.decimalSeparator,n=e.thousandSeparator;if(r===n)throw new Error("\n Decimal separator can't be same as thousand separator.\n thousandSeparator: "+n+' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: '+r+" (default value for decimalSeparator is .)\n ");if(t&&("string"===t?t:t.toString()).match(/\d/g))throw new Error("\n Mask "+t+" should not contain numeric character;\n ")},t.prototype.setPatchedCaretPosition=function(t,e,r){a(t,e),setTimeout(function(){t.value===r&&a(t,e)},0)},t.prototype.correctCaretPosition=function(t,e,r){var n=this.props,o=n.prefix,a=n.suffix,i=n.format;if(""===t)return 0;if(e=h(e,0,t.length),!i){var s="-"===t[0];return h(e,o.length+(s?1:0),t.length-a.length)}if("function"==typeof i)return e;if("#"===i[e]&&c(t[e]))return e;if("#"===i[e-1]&&c(t[e-1]))return e;var u=i.indexOf("#");e=h(e,u,i.lastIndexOf("#")+1);for(var l=i.substring(e,i.length).indexOf("#"),p=e,f=e+(-1===l?0:l);u<p&&("#"!==i[p]||!c(t[p]));)p-=1;return!c(t[f])||"left"===r&&e!==u||e-p<f-e?c(t[p])?p+1:p:f},t.prototype.getCaretPosition=function(t,e,r){var n,o,a=this.props.format,i=this.state.value,s=this.getNumberRegex(!0),u=(t.match(s)||[]).join(""),l=(e.match(s)||[]).join("");for(o=n=0;o<r;o++){var p=t[o]||"",f=e[n]||"";if((p.match(s)||p===f)&&("0"!==p||!f.match(s)||"0"===f||u.length===l.length)){for(;p!==e[n]&&n<e.length;)n++;n++}}return"string"!=typeof a||i||(n=e.length),n=this.correctCaretPosition(e,n)},t.prototype.removePrefixAndSuffix=function(t){var e=this.props,r=e.format,n=e.prefix,o=e.suffix;if(!r&&t){var a="-"===t[0];a&&(t=t.substring(1,t.length));var i=(t=n&&0===t.indexOf(n)?t.substring(n.length,t.length):t).lastIndexOf(o);t=o&&-1!==i&&i===t.length-o.length?t.substring(0,i):t,a&&(t="-"+t)}return t},t.prototype.removePatternFormatting=function(t){for(var e=this.props.format.split("#").filter(function(t){return""!==t}),r=0,n="",o=0,a=e.length;o<=a;o++){var i=e[o]||"",s=o===a?t.length:t.indexOf(i,r);if(-1===s){n=t;break}n+=t.substring(r,s),r=s+i.length}return(n.match(/\d/g)||[]).join("")},t.prototype.removeFormatting=function(t){var e=this.props,r=e.format,n=e.removeFormatting;return t?t=r?"string"==typeof r?this.removePatternFormatting(t):"function"==typeof n?n(t):(t.match(/\d/g)||[]).join(""):(t=this.removePrefixAndSuffix(t),this.getFloatString(t)):t},t.prototype.formatWithPattern=function(t){for(var e=this.props.format,r=0,n=e.split(""),o=0,a=e.length;o<a;o++)"#"===e[o]&&(n[o]=t[r]||this.getMaskAtIndex(r),r+=1);return n.join("")},t.prototype.formatAsNumber=function(t){var e=this.props,r=e.decimalScale,n=e.fixedDecimalScale,o=e.prefix,a=e.suffix,i=e.allowNegative,s=e.thousandsGroupStyle,u=this.getSeparators(),l=u.thousandSeparator,p=u.decimalSeparator,f=-1!==t.indexOf(".")||r&&n,c=R(t,i),h=c.beforeDecimal,g=c.afterDecimal,m=c.addNegation;return void 0!==r&&(g=d(g,r,n)),l&&(h=function(t,e,r){var n=function(t){switch(t){case"lakh":return/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g;case"wan":return/(\d)(?=(\d{4})+(?!\d))/g;case"thousand":default:return/(\d)(?=(\d{3})+(?!\d))/g}}(r),o=t.search(/[1-9]/);return o=-1===o?t.length:o,t.substring(0,o)+t.substring(o,t.length).replace(n,"$1"+e)}(h,l,s)),o&&(h=o+h),a&&(g+=a),m&&(h="-"+h),t=h+(f&&p||"")+g},t.prototype.formatNumString=function(t){void 0===t&&(t="");var e=this.props,r=e.format,n=e.allowEmptyFormatting,o=t;return o=""!==t||n?"-"!==t||r?"string"==typeof r?this.formatWithPattern(o):"function"==typeof r?r(o):this.formatAsNumber(o):"-":""},t.prototype.formatValueProp=function(t){var e=this.props,r=e.format,n=e.decimalScale,o=e.fixedDecimalScale,a=e.allowEmptyFormatting,i=this.props,s=i.value,u=i.isNumericString,l=!(s=f(s)?t:s)&&0!==s;return l&&a&&(s=""),l&&!a?"":("number"==typeof s&&(s=function(t){var e="-"===(t+="")[0]?"-":"";e&&(t=t.substring(1));var r=t.split(/[eE]/g),n=r[0],o=r[1];if(!(o=Number(o)))return e+n;var a=1+o,i=(n=n.replace(".","")).length;return a<0?n="0."+p("0",Math.abs(a))+n:i<=a?n+=p("0",a-i):n=(n.substring(0,a)||"0")+"."+n.substring(a),e+n}(s),u=!0),"Infinity"===s&&u&&(s=""),u&&!r&&"number"==typeof n&&(s=function(t,e,r){if(-1!==["","-"].indexOf(t))return t;var n=-1!==t.indexOf(".")&&e,o=R(t),a=o.beforeDecimal,i=o.afterDecimal,s=o.hasNagation,u=parseFloat("0."+(i||"0")),l=(i.length<=e?"0."+i:u.toFixed(e)).split(".");return(s?"-":"")+a.split("").reverse().reduce(function(t,e,r){return t.length>r?(Number(t[0])+Number(e)).toString()+t.substring(1,t.length):e+t},l[0])+(n?".":"")+d(l[1]||"",Math.min(e,i.length),r)}(s,n,o)),u?this.formatNumString(s):this.formatInput(s))},t.prototype.formatNegation=function(t){void 0===t&&(t="");var e=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),o=r.test(t),a=n.test(t);return t=t.replace(/-/g,""),o&&!a&&e&&(t="-"+t),t},t.prototype.formatInput=function(t){return void 0===t&&(t=""),this.props.format||(t=this.removePrefixAndSuffix(t),t=this.formatNegation(t)),t=this.removeFormatting(t),this.formatNumString(t)},t.prototype.isCharacterAFormat=function(t,e){var r=this.props,n=r.format,o=r.prefix,a=r.suffix,i=r.decimalScale,s=r.fixedDecimalScale,u=this.getSeparators().decimalSeparator;return"string"==typeof n&&"#"!==n[t]||!(n||!(t<o.length||t>=e.length-a.length||i&&s&&e[t]===u))},t.prototype.correctInputValue=function(t,r,e){var n=this,o=this.props,a=o.format,i=o.allowNegative,s=o.prefix,u=o.suffix,l=o.decimalScale,p=this.getSeparators(),f=p.allowedDecimalSeparators,c=p.decimalSeparator,h=this.state.numAsString||"",g=this.selectionBeforeInput,m=g.selectionStart,d=g.selectionEnd,v=function(t,e){for(var r=0,n=0,o=t.length,a=e.length;t[r]===e[r]&&r<o;)r++;for(;t[o-1-n]===e[a-1-n]&&r<a-n&&r<o-n;)n++;return{start:r,end:o-n}}(r,e),y=v.start,S=v.end;if(!a&&y===S&&-1!==f.indexOf(e[m])){var x=0===l?"":c;return e.substr(0,m)+x+e.substr(m+1,e.length)}var b=a?0:s.length,w=r.length-(a?0:u.length);if(e.length>r.length||!e.length||y===S||0===m&&d===r.length||0===y&&S===r.length||m===b&&d===w)return e;var P=r.substr(y,S-y);if(!![].concat(P).find(function(t,e){return n.isCharacterAFormat(e+y,r)})){var O=r.substr(y),N={},F=[];[].concat(O).forEach(function(t,e){n.isCharacterAFormat(e+y,r)?N[e]=t:e>P.length-1&&F.push(t)}),Object.keys(N).forEach(function(t){F.length>t?F.splice(t,0,N[t]):F.push(N[t])}),e=r.substr(0,y)+F.join("")}if(!a){var C=this.removeFormatting(e),D=R(C,i),E=D.beforeDecimal,T=D.afterDecimal,V=D.addNegation,A=t<e.indexOf(c)+1;if(C.length<h.length&&A&&""===E&&!parseFloat(T))return V?"-":""}return e},t.prototype.updateValue=function(t){var e=t.formattedValue,r=t.input,n=t.setCaretPosition;void 0===n&&(n=!0);var o=t.numAsString,a=t.caretPos,i=this.props.onValueChange,s=this.state.value;if(r)if(n){if(!a){var u=t.inputValue||r.value,l=m(r);r.value=e,a=this.getCaretPosition(u,e,l)}this.setPatchedCaretPosition(r,a,e)}else r.value=e;void 0===o&&(o=this.removeFormatting(e)),e!==s&&(this.setState({value:e,numAsString:o}),i(this.getValueObject(e,o)))},t.prototype.onChange=function(t){var e=t.target,r=e.value,n=this.state,o=this.props,a=o.isAllowed,i=n.value||"",s=m(e);r=this.correctInputValue(s,i,r);var u=this.formatInput(r)||"",l=this.removeFormatting(u),p=a(this.getValueObject(u,l));p||(u=i),this.updateValue({formattedValue:u,numAsString:l,inputValue:r,input:e}),p&&o.onChange(t)},t.prototype.onBlur=function(t){var e=this.props,r=this.state,n=e.format,o=e.onBlur,a=e.allowLeadingZeros,i=r.numAsString,s=r.value;if(this.focusedElm=null,clearTimeout(this.focusTimeout),!n){isNaN(parseFloat(i))&&(i=""),a||(i=function(t){if(!t)return t;var e="-"===t[0];e&&(t=t.substring(1,t.length));var r=t.split("."),n=r[0].replace(/^0+/,"")||"0",o=r[1]||"";return(e?"-":"")+n+(o?"."+o:"")}(i));var u=this.formatNumString(i);if(u!==s)return this.updateValue({formattedValue:u,numAsString:i,input:t.target,setCaretPosition:!1}),void o(t)}o(t)},t.prototype.onKeyDown=function(t){var e,r=t.target,n=t.key,o=r.selectionStart,a=r.selectionEnd,i=r.value;void 0===i&&(i="");var s=this.props,u=s.decimalScale,l=s.fixedDecimalScale,p=s.prefix,f=s.suffix,c=s.format,h=s.onKeyDown,g=void 0!==u&&l,m=this.getNumberRegex(!1,g),d=new RegExp("-"),v="string"==typeof c;if(this.selectionBeforeInput={selectionStart:o,selectionEnd:a},"ArrowLeft"===n||"Backspace"===n?e=o-1:"ArrowRight"===n?e=o+1:"Delete"===n&&(e=o),void 0!==e&&o===a){var y=e,S=v?c.indexOf("#"):p.length,x=v?c.lastIndexOf("#")+1:i.length-f.length;if("ArrowLeft"===n||"ArrowRight"===n){var b="ArrowLeft"===n?"left":"right";y=this.correctCaretPosition(i,e,b)}else if("Delete"!==n||m.test(i[e])||d.test(i[e])){if("Backspace"===n&&!m.test(i[e]))if(o<=S+1&&"-"===i[0]&&void 0===c){var w=i.substring(1);this.updateValue({formattedValue:w,caretPos:y,input:r})}else if(!d.test(i[e])){for(;!m.test(i[y-1])&&S<y;)y--;y=this.correctCaretPosition(i,y,"left")}}else for(;!m.test(i[y])&&y<x;)y++;(y!==e||e<S||x<e)&&(t.preventDefault(),this.setPatchedCaretPosition(r,y,i)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,y,i),h(t)}else h(t)},t.prototype.onMouseUp=function(t){var e=t.target,r=e.selectionStart,n=e.selectionEnd,o=e.value;if(void 0===o&&(o=""),r===n){var a=this.correctCaretPosition(o,r);a!==r&&this.setPatchedCaretPosition(e,a,o)}this.props.onMouseUp(t)},t.prototype.onFocus=function(a){var i=this;a.persist(),this.focusedElm=a.target,this.focusTimeout=setTimeout(function(){var t=a.target,e=t.selectionStart,r=t.selectionEnd,n=t.value;void 0===n&&(n="");var o=i.correctCaretPosition(n,e);o===e||0===e&&r===n.length||i.setPatchedCaretPosition(t,o,n),i.props.onFocus(a)},0)},t.prototype.render=function(){var t=this.props,e=t.type,r=t.displayType,n=t.customInput,o=t.renderText,a=t.getInputRef,i=t.format,s=this.state,u=s.value,l=s.mounted,p=function(e,r){var n={};return Object.keys(e).forEach(function(t){r[t]||(n[t]=e[t])}),n}(this.props,v),f=l&&function(t){return t||!(navigator.platform&&/iPhone|iPod/.test(navigator.platform))}(i)?"numeric":void 0,c=Object.assign({inputMode:f},p,{type:e,value:u,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===r)return o?o(u,p)||null:g.createElement("span",Object.assign({},p,{ref:a}),u);if(n){var h=n;return g.createElement(h,Object.assign({},c,{ref:a}))}return g.createElement("input",Object.assign({},c,{ref:a}))},t}(g.Component);return u.propTypes=v,u.defaultProps=s,u});

@@ -176,3 +176,3 @@ "use strict";

var floatValue = parseFloat("0.".concat(afterDecimal || '0'));
var floatValueStr = afterDecimal.length <= scale ? toNumericString(floatValue) : floatValue.toFixed(scale);
var floatValueStr = afterDecimal.length <= scale ? "0.".concat(afterDecimal) : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');

@@ -179,0 +179,0 @@ var intPart = beforeDecimal.split('').reverse().reduce(function (roundedStr, current, idx) {

{
"name": "react-number-format",
"description": "React component to format number in an input or as a text.",
"version": "4.6.1",
"version": "4.6.2",
"main": "dist/react-number-format.cjs.js",

@@ -6,0 +6,0 @@ "module": "dist/react-number-format.es.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc