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.0.5 to 4.0.6

16

dist/react-number-format.es.js
/**
* react-number-format - 4.0.5
* react-number-format - 4.0.6
* Author : Sudhanshu Yadav

@@ -981,13 +981,12 @@ * Copyright (c) 2016, 2018 to Sudhanshu Yadav, released under the MIT license.

value: function formatNumString() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var numStr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var _this$props6 = this.props,
format = _this$props6.format,
allowEmptyFormatting = _this$props6.allowEmptyFormatting;
var formattedValue = value;
var formattedValue = numStr;
if (value === '' && !allowEmptyFormatting) {
if (numStr === '' && !allowEmptyFormatting) {
formattedValue = '';
} else if (value === '-' && !format) {
} else if (numStr === '-' && !format) {
formattedValue = '-';
value = '';
} else if (typeof format === 'string') {

@@ -1027,2 +1026,7 @@ formattedValue = this.formatWithPattern(formattedValue);

isNumericString = true;
} //change infinity value to empty string
if (value === 'Infinity' && isNumericString) {
value = '';
} //round the number based on decimalScale

@@ -1029,0 +1033,0 @@ //format only if non formatted value is provided

/**
* react-number-format - 4.0.5
* react-number-format - 4.0.6
* Author : Sudhanshu Yadav

@@ -987,13 +987,12 @@ * Copyright (c) 2016, 2018 to Sudhanshu Yadav, released under the MIT license.

value: function formatNumString() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var numStr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var _this$props6 = this.props,
format = _this$props6.format,
allowEmptyFormatting = _this$props6.allowEmptyFormatting;
var formattedValue = value;
var formattedValue = numStr;
if (value === '' && !allowEmptyFormatting) {
if (numStr === '' && !allowEmptyFormatting) {
formattedValue = '';
} else if (value === '-' && !format) {
} else if (numStr === '-' && !format) {
formattedValue = '-';
value = '';
} else if (typeof format === 'string') {

@@ -1033,2 +1032,7 @@ formattedValue = this.formatWithPattern(formattedValue);

isNumericString = true;
} //change infinity value to empty string
if (value === 'Infinity' && isNumericString) {
value = '';
} //round the number based on decimalScale

@@ -1035,0 +1039,0 @@ //format only if non formatted value is provided

/**
* react-number-format - 4.0.5
* react-number-format - 4.0.6
* Author : Sudhanshu Yadav

@@ -8,2 +8,2 @@ * Copyright (c) 2016, 2018 to Sudhanshu Yadav, released under the MIT license.

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):t.NumberFormat=e(t.React)}(this,function(h){"use strict";function a(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function g(){return(g=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function s(t){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function o(t,e){return(o=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function u(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function t(t){return function(){return t}}h=h&&h.hasOwnProperty("default")?h.default:h;var e=function(){};e.thatReturns=t,e.thatReturnsFalse=t(!1),e.thatReturnsTrue=t(!0),e.thatReturnsNull=t(null),e.thatReturnsThis=function(){return this},e.thatReturnsArgument=function(t){return t};var n=e;var i=function(t,e,r,n,a,o,i,s){if(!t){var u;if(void 0===e)u=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var l=[r,n,a,o,i,s],c=0;(u=new Error(e.replace(/%s/g,function(){return l[c++]}))).name="Invariant Violation"}throw u.framesToPop=1,u}},r=Object.getOwnPropertySymbols,l=Object.prototype.hasOwnProperty,c=Object.prototype.propertyIsEnumerable;(function(){try{if(!Object.assign)return!1;var t=new String("abc");if(t[5]="de","5"===Object.getOwnPropertyNames(t)[0])return!1;for(var e={},r=0;r<10;r++)e["_"+String.fromCharCode(r)]=r;if("0123456789"!==Object.getOwnPropertyNames(e).map(function(t){return e[t]}).join(""))return!1;var n={};return"abcdefghijklmnopqrst".split("").forEach(function(t){n[t]=t}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},n)).join("")}catch(t){return!1}})()&&Object.assign;var f,p=(function(t){t.exports=function(){function t(t,e,r,n,a,o){"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==o&&i(!1,"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")}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,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e};return r.checkPropTypes=n,r.PropTypes=r}()}(f={exports:{}},f.exports),f.exports);function m(){}function d(t){return!!(t||"").match(/\d/)}function v(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function P(t){var e=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],r="-"===t[0],n=r&&e,a=(t=t.replace("-","")).split(".");return{beforeDecimal:a[0],afterDecimal:a[1]||"",hasNagation:r,addNegation:n}}function y(t,e,r){for(var n="",a=r?"0":"",o=0;o<=e-1;o++)n+=t[o]||a;return n}function S(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 b(t,e,r){return Math.min(Math.max(t,e),r)}function x(t){return Math.max(t.selectionStart,t.selectionEnd)}var O={thousandSeparator:p.oneOfType([p.string,p.oneOf([!0])]),decimalSeparator:p.string,thousandsGroupStyle:p.oneOf(["thousand","lakh","wan"]),decimalScale:p.number,fixedDecimalScale:p.bool,displayType:p.oneOf(["input","text"]),prefix:p.string,suffix:p.string,format:p.oneOfType([p.string,p.func]),removeFormatting:p.func,mask:p.oneOfType([p.string,p.arrayOf(p.string)]),value:p.oneOfType([p.number,p.string]),defaultValue:p.oneOfType([p.number,p.string]),isNumericString:p.bool,customInput:p.func,allowNegative:p.bool,allowEmptyFormatting:p.bool,onValueChange:p.func,onKeyDown:p.func,onMouseUp:p.func,onChange:p.func,onFocus:p.func,onBlur:p.func,type:p.oneOf(["text","tel","password"]),isAllowed:p.func,renderText:p.func,getInputRef:p.func},w={displayType:"input",decimalSeparator:".",thousandsGroupStyle:"thousand",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,allowEmptyFormatting:!1,isNumericString:!1,type:"text",onValueChange:m,onChange:m,onKeyDown:m,onMouseUp:m,onFocus:m,onBlur:m,isAllowed:function(){return!0},getInputRef:m},k=function(t){function i(t){var e,r,n;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,i),r=this,e=!(n=s(i).call(this,t))||"object"!=typeof n&&"function"!=typeof n?u(r):n;var a=t.defaultValue;e.validateProps();var o=e.formatValueProp(a);return e.state={value:o,numAsString:e.removeFormatting(o)},e.selectionBeforeInput={selectionStart:0,selectionEnd:0},e.onChange=e.onChange.bind(u(u(e))),e.onKeyDown=e.onKeyDown.bind(u(u(e))),e.onMouseUp=e.onMouseUp.bind(u(u(e))),e.onFocus=e.onFocus.bind(u(u(e))),e.onBlur=e.onBlur.bind(u(u(e))),e}var e,r,n;return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&o(t,e)}(i,h.Component),e=i,(r=[{key:"componentDidUpdate",value:function(t){this.updateValueIfRequired(t)}},{key:"updateValueIfRequired",value:function(t){var e=this.props,r=this.state,n=this.focusedElm,a=r.value,o=r.numAsString,i=void 0===o?"":o;if(t!==e){this.validateProps();var s=this.formatNumString(i),u=void 0===e.value?s:this.formatValueProp(),l=this.removeFormatting(u),c=parseFloat(l),f=parseFloat(i);(isNaN(c)&&isNaN(f)||c===f)&&s===a&&(null!==n||u===a)||this.updateValue({formattedValue:u,numAsString:l,input:n})}}},{key:"getFloatString",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",e=this.props.decimalScale,r=this.getSeparators().decimalSeparator,n=this.getNumberRegex(!0),a="-"===t[0];a&&(t=t.replace("-","")),r&&0===e&&(t=t.split(r)[0]);var o=(t=(t.match(n)||[]).join("").replace(r,".")).indexOf(".");return-1!==o&&(t="".concat(t.substring(0,o),".").concat(t.substring(o+1,t.length).replace(new RegExp(v(r),"g"),""))),a&&(t="-"+t),t}},{key:"getNumberRegex",value:function(t,e){var r=this.props,n=r.format,a=r.decimalScale,o=this.getSeparators().decimalSeparator;return new RegExp("\\d"+(!o||0===a||e||n?"":"|"+v(o)),t?"g":void 0)}},{key:"getSeparators",value:function(){var t=this.props.decimalSeparator,e=this.props.thousandSeparator;return!0===e&&(e=","),{decimalSeparator:t,thousandSeparator:e}}},{key:"getMaskAtIndex",value:function(t){var e=this.props.mask,r=void 0===e?" ":e;return"string"==typeof r?r:r[t]||" "}},{key:"getValueObject",value:function(t,e){var r=parseFloat(e);return{formattedValue:t,value:e,floatValue:isNaN(r)?void 0:r}}},{key:"validateProps",value: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: ".concat(n,' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: ').concat(r," (default value for decimalSeparator is .)\n "));if(t&&("string"===t?t:t.toString()).match(/\d/g))throw new Error("\n Mask ".concat(t," should not contain numeric character;\n "))}},{key:"setPatchedCaretPosition",value:function(t,e,r){S(t,e),setTimeout(function(){t.value===r&&S(t,e)},0)}},{key:"correctCaretPosition",value:function(t,e,r){var n=this.props,a=n.prefix,o=n.suffix,i=n.format;if(""===t)return 0;if(e=b(e,0,t.length),!i){var s="-"===t[0];return b(e,a.length+(s?1:0),t.length-o.length)}if("function"==typeof i)return e;if("#"===i[e]&&d(t[e]))return e;if("#"===i[e-1]&&d(t[e-1]))return e;var u=i.indexOf("#");e=b(e,u,i.lastIndexOf("#")+1);for(var l=i.substring(e,i.length).indexOf("#"),c=e,f=e+(-1===l?0:l);u<c&&("#"!==i[c]||!d(t[c]));)c-=1;return!d(t[f])||"left"===r&&e!==u||e-c<f-e?d(t[c])?c+1:c:f}},{key:"getCaretPosition",value:function(t,e,r){var n,a,o=this.props.format,i=this.state.value,s=this.getNumberRegex(!0),u=(t.match(s)||[]).join(""),l=(e.match(s)||[]).join("");for(a=n=0;a<r;a++){var c=t[a]||"",f=e[n]||"";if((c.match(s)||c===f)&&("0"!==c||!f.match(s)||"0"===f||u.length===l.length)){for(;c!==e[n]&&n<e.length;)n++;n++}}return"string"!=typeof o||i||(n=e.length),n=this.correctCaretPosition(e,n)}},{key:"removePrefixAndSuffix",value:function(t){var e=this.props,r=e.format,n=e.prefix,a=e.suffix;if(!r&&t){var o="-"===t[0];o&&(t=t.substring(1,t.length));var i=(t=n&&0===t.indexOf(n)?t.substring(n.length,t.length):t).lastIndexOf(a);t=a&&-1!==i&&i===t.length-a.length?t.substring(0,i):t,o&&(t="-"+t)}return t}},{key:"removePatternFormatting",value:function(t){for(var e=this.props.format.split("#").filter(function(t){return""!==t}),r=0,n="",a=0,o=e.length;a<=o;a++){var i=e[a]||"",s=a===o?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("")}},{key:"removeFormatting",value: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}},{key:"formatWithPattern",value:function(t){for(var e=this.props.format,r=0,n=e.split(""),a=0,o=e.length;a<o;a++)"#"===e[a]&&(n[a]=t[r]||this.getMaskAtIndex(r),r+=1);return n.join("")}},{key:"formatAsNumber",value:function(t){var e=this.props,r=e.decimalScale,n=e.fixedDecimalScale,a=e.prefix,o=e.suffix,i=e.allowNegative,s=e.thousandsGroupStyle,u=this.getSeparators(),l=u.thousandSeparator,c=u.decimalSeparator,f=-1!==t.indexOf(".")||r&&n,p=P(t,i),h=p.beforeDecimal,g=p.afterDecimal,m=p.addNegation;if(void 0!==r&&(g=y(g,r,n)),l){var d=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}}(s);h=h.replace(d,"$1"+l)}return a&&(h=a+h),o&&(g+=o),m&&(h="-"+h),t=h+(f&&c||"")+g}},{key:"formatNumString",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",e=this.props,r=e.format,n=e.allowEmptyFormatting,a=t;return""!==t||n?"-"!==t||r?a="string"==typeof r?this.formatWithPattern(a):"function"==typeof r?r(a):this.formatAsNumber(a):(a="-",t=""):a="",a}},{key:"formatValueProp",value:function(t){var e=this.props,r=e.format,n=e.decimalScale,a=e.fixedDecimalScale,o=e.allowEmptyFormatting,i=this.props,s=i.value,u=void 0===s?t:s,l=i.isNumericString,c=!u&&0!==u;return c&&o&&(u=""),c&&!o?"":("number"==typeof u&&(u=u.toString(),l=!0),l&&!r&&"number"==typeof n&&(u=function(t,e,r){if(-1!==["","-"].indexOf(t))return t;var n=-1!==t.indexOf(".")&&e,a=P(t),o=a.beforeDecimal,i=a.afterDecimal,s=a.hasNagation,u=parseFloat("0.".concat(i||"0")).toFixed(e).split("."),l=o.split("").reverse().reduce(function(t,e,r){return t.length>r?(Number(t[0])+Number(e)).toString()+t.substring(1,t.length):e+t},u[0]),c=y(u[1]||"",Math.min(e,i.length),r),f=n?".":"";return"".concat(s?"-":"").concat(l).concat(f).concat(c)}(u,n,a)),l?this.formatNumString(u):this.formatInput(u))}},{key:"formatNegation",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",e=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),a=r.test(t),o=n.test(t);return t=t.replace(/-/g,""),a&&!o&&e&&(t="-"+t),t}},{key:"formatInput",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";return this.props.format||(t=this.removePrefixAndSuffix(t),t=this.formatNegation(t)),t=this.removeFormatting(t),this.formatNumString(t)}},{key:"isCharacterAFormat",value:function(t,e){var r=this.props,n=r.format,a=r.prefix,o=r.suffix,i=r.decimalScale,s=r.fixedDecimalScale,u=this.getSeparators().decimalSeparator;return"string"==typeof n&&"#"!==n[t]||!(n||!(t<a.length||t>=e.length-o.length||i&&s&&e[t]===u))}},{key:"checkIfFormatGotDeleted",value:function(t,e,r){for(var n=t;n<e;n++)if(this.isCharacterAFormat(n,r))return!0;return!1}},{key:"correctInputValue",value:function(t,e,r){var n=this.props,a=n.format,o=n.allowNegative,i=n.prefix,s=n.suffix,u=this.getSeparators().decimalSeparator,l=this.state.numAsString||"",c=this.selectionBeforeInput,f=c.selectionStart,p=c.selectionEnd,h=function(t,e){for(var r=0,n=0,a=t.length,o=e.length;t[r]===e[r]&&r<a;)r++;for(;t[a-1-n]===e[o-1-n]&&r<o-n&&r<a-n;)n++;return{start:r,end:a-n}}(e,r),g=h.start,m=h.end;if(!a&&"."!==u&&g===m&&"."===r[f])return r.substr(0,f)+u+r.substr(f+1,r.length);var d=a?0:i.length,v=e.length-(a?0:s.length);if(r.length>e.length||!r.length||g===m||0===f&&p===e.length||f===d&&p===v)return r;if(this.checkIfFormatGotDeleted(g,m,e)&&(r=e),!a){var y=this.removeFormatting(r),S=P(y,o),b=S.beforeDecimal,x=S.afterDecimal,O=S.addNegation,w=t<r.indexOf(u)+1;if(y.length<l.length&&w&&""===b&&!parseFloat(x))return O?"-":""}return r}},{key:"updateValue",value:function(t){var e=this,r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:m,n=t.formattedValue,a=t.input,o=t.numAsString,i=t.caretPos,s=this.props.onValueChange,u=this.state.value;if(a){if(!i){var l=t.inputValue||a.value,c=x(a);i=this.getCaretPosition(l,n,c)}a.value=n,this.setPatchedCaretPosition(a,i,n)}void 0===o&&(o=this.removeFormatting(n)),n!==u?this.setState({value:n,numAsString:o},function(){s(e.getValueObject(n,o)),r()}):r()}},{key:"onChange",value:function(t){t.persist();var e=t.target,r=e.value,n=this.state,a=this.props,o=a.isAllowed,i=n.value||"",s=x(e);r=this.correctInputValue(s,i,r);var u=this.formatInput(r)||"",l=this.removeFormatting(u);o(this.getValueObject(u,l))||(u=i),this.updateValue({formattedValue:u,numAsString:l,inputValue:r,input:e},function(){a.onChange(t)})}},{key:"onBlur",value:function(t){var e=this.props,r=this.state,n=e.format,a=e.onBlur,o=r.numAsString,i=r.value;if(this.focusedElm=null,!n){o=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",a=r[1]||"";return"".concat(e?"-":"").concat(n).concat(a?".".concat(a):"")}(o);var s=this.formatNumString(o);if(s!==i)return t.persist(),void this.updateValue({formattedValue:s,numAsString:o},function(){a(t)})}a(t)}},{key:"onKeyDown",value:function(t){var e,r=t.target,n=t.key,a=r.selectionStart,o=r.selectionEnd,i=r.value,s=void 0===i?"":i,u=this.props,l=u.decimalScale,c=u.fixedDecimalScale,f=u.prefix,p=u.suffix,h=u.format,g=u.onKeyDown,m=(u.onValueChange,void 0!==l&&c),d=this.getNumberRegex(!1,m),v=new RegExp("-"),y="string"==typeof h;if(this.selectionBeforeInput={selectionStart:a,selectionEnd:o},"ArrowLeft"===n||"Backspace"===n?e=a-1:"ArrowRight"===n?e=a+1:"Delete"===n&&(e=a),void 0!==e&&a===o){var S=e,b=y?h.indexOf("#"):f.length,x=y?h.lastIndexOf("#")+1:s.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var O="ArrowLeft"===n?"left":"right";S=this.correctCaretPosition(s,e,O)}else if("Delete"!==n||d.test(s[e])||v.test(s[e])){if("Backspace"===n&&!d.test(s[e]))if(a<=b+1&&"-"===s[0]&&void 0===h){var w=s.substring(1);t.persist(),this.updateValue({formattedValue:w,caretPos:S,input:r})}else if(!v.test(s[e])){for(;!d.test(s[S-1])&&b<S;)S--;S=this.correctCaretPosition(s,S,"left")}}else for(;!d.test(s[S])&&S<x;)S++;(S!==e||e<b||x<e)&&(t.preventDefault(),this.setPatchedCaretPosition(r,S,s)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,S,s),this.props.onKeyDown(t)}else g(t)}},{key:"onMouseUp",value:function(t){var e=t.target,r=e.selectionStart,n=e.selectionEnd,a=e.value,o=void 0===a?"":a;if(r===n){var i=this.correctCaretPosition(o,r);i!==r&&this.setPatchedCaretPosition(e,i,o)}this.props.onMouseUp(t)}},{key:"onFocus",value:function(i){var s=this;i.persist(),this.focusedElm=i.target,setTimeout(function(){var t=i.target,e=t.selectionStart,r=t.selectionEnd,n=t.value,a=void 0===n?"":n,o=s.correctCaretPosition(a,e);o===e||0===e&&r===a.length||s.setPatchedCaretPosition(t,o,a),s.props.onFocus(i)},0)}},{key:"render",value:function(){var e,r,n,t=this.props,a=t.type,o=t.displayType,i=t.customInput,s=t.renderText,u=t.getInputRef,l=this.state.value,c=(e=this.props,r=O,n={},Object.keys(e).forEach(function(t){r[t]||(n[t]=e[t])}),n),f=g({},c,{type:a,value:l,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===o)return s?s(l)||null:h.createElement("span",g({},c,{ref:u}),l);if(i){var p=i;return h.createElement(p,f)}return h.createElement("input",g({},f,{ref:u}))}}])&&a(e.prototype,r),n&&a(e,n),i}();return k.propTypes=O,k.defaultProps=w,k});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):t.NumberFormat=e(t.React)}(this,function(h){"use strict";function a(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function g(){return(g=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function s(t){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function o(t,e){return(o=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function u(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function t(t){return function(){return t}}h=h&&h.hasOwnProperty("default")?h.default:h;var e=function(){};e.thatReturns=t,e.thatReturnsFalse=t(!1),e.thatReturnsTrue=t(!0),e.thatReturnsNull=t(null),e.thatReturnsThis=function(){return this},e.thatReturnsArgument=function(t){return t};var n=e;var i=function(t,e,r,n,a,o,i,s){if(!t){var u;if(void 0===e)u=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var l=[r,n,a,o,i,s],f=0;(u=new Error(e.replace(/%s/g,function(){return l[f++]}))).name="Invariant Violation"}throw u.framesToPop=1,u}},r=Object.getOwnPropertySymbols,l=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable;(function(){try{if(!Object.assign)return!1;var t=new String("abc");if(t[5]="de","5"===Object.getOwnPropertyNames(t)[0])return!1;for(var e={},r=0;r<10;r++)e["_"+String.fromCharCode(r)]=r;if("0123456789"!==Object.getOwnPropertyNames(e).map(function(t){return e[t]}).join(""))return!1;var n={};return"abcdefghijklmnopqrst".split("").forEach(function(t){n[t]=t}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},n)).join("")}catch(t){return!1}})()&&Object.assign;var c,p=(function(t){t.exports=function(){function t(t,e,r,n,a,o){"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==o&&i(!1,"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")}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,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e};return r.checkPropTypes=n,r.PropTypes=r}()}(c={exports:{}},c.exports),c.exports);function m(){}function d(t){return!!(t||"").match(/\d/)}function v(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}function P(t){var e=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],r="-"===t[0],n=r&&e,a=(t=t.replace("-","")).split(".");return{beforeDecimal:a[0],afterDecimal:a[1]||"",hasNagation:r,addNegation:n}}function y(t,e,r){for(var n="",a=r?"0":"",o=0;o<=e-1;o++)n+=t[o]||a;return n}function S(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 b(t,e,r){return Math.min(Math.max(t,e),r)}function x(t){return Math.max(t.selectionStart,t.selectionEnd)}var O={thousandSeparator:p.oneOfType([p.string,p.oneOf([!0])]),decimalSeparator:p.string,thousandsGroupStyle:p.oneOf(["thousand","lakh","wan"]),decimalScale:p.number,fixedDecimalScale:p.bool,displayType:p.oneOf(["input","text"]),prefix:p.string,suffix:p.string,format:p.oneOfType([p.string,p.func]),removeFormatting:p.func,mask:p.oneOfType([p.string,p.arrayOf(p.string)]),value:p.oneOfType([p.number,p.string]),defaultValue:p.oneOfType([p.number,p.string]),isNumericString:p.bool,customInput:p.func,allowNegative:p.bool,allowEmptyFormatting:p.bool,onValueChange:p.func,onKeyDown:p.func,onMouseUp:p.func,onChange:p.func,onFocus:p.func,onBlur:p.func,type:p.oneOf(["text","tel","password"]),isAllowed:p.func,renderText:p.func,getInputRef:p.func},w={displayType:"input",decimalSeparator:".",thousandsGroupStyle:"thousand",fixedDecimalScale:!1,prefix:"",suffix:"",allowNegative:!0,allowEmptyFormatting:!1,isNumericString:!1,type:"text",onValueChange:m,onChange:m,onKeyDown:m,onMouseUp:m,onFocus:m,onBlur:m,isAllowed:function(){return!0},getInputRef:m},k=function(t){function i(t){var e,r,n;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,i),r=this,e=!(n=s(i).call(this,t))||"object"!=typeof n&&"function"!=typeof n?u(r):n;var a=t.defaultValue;e.validateProps();var o=e.formatValueProp(a);return e.state={value:o,numAsString:e.removeFormatting(o)},e.selectionBeforeInput={selectionStart:0,selectionEnd:0},e.onChange=e.onChange.bind(u(u(e))),e.onKeyDown=e.onKeyDown.bind(u(u(e))),e.onMouseUp=e.onMouseUp.bind(u(u(e))),e.onFocus=e.onFocus.bind(u(u(e))),e.onBlur=e.onBlur.bind(u(u(e))),e}var e,r,n;return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&o(t,e)}(i,h.Component),e=i,(r=[{key:"componentDidUpdate",value:function(t){this.updateValueIfRequired(t)}},{key:"updateValueIfRequired",value:function(t){var e=this.props,r=this.state,n=this.focusedElm,a=r.value,o=r.numAsString,i=void 0===o?"":o;if(t!==e){this.validateProps();var s=this.formatNumString(i),u=void 0===e.value?s:this.formatValueProp(),l=this.removeFormatting(u),f=parseFloat(l),c=parseFloat(i);(isNaN(f)&&isNaN(c)||f===c)&&s===a&&(null!==n||u===a)||this.updateValue({formattedValue:u,numAsString:l,input:n})}}},{key:"getFloatString",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",e=this.props.decimalScale,r=this.getSeparators().decimalSeparator,n=this.getNumberRegex(!0),a="-"===t[0];a&&(t=t.replace("-","")),r&&0===e&&(t=t.split(r)[0]);var o=(t=(t.match(n)||[]).join("").replace(r,".")).indexOf(".");return-1!==o&&(t="".concat(t.substring(0,o),".").concat(t.substring(o+1,t.length).replace(new RegExp(v(r),"g"),""))),a&&(t="-"+t),t}},{key:"getNumberRegex",value:function(t,e){var r=this.props,n=r.format,a=r.decimalScale,o=this.getSeparators().decimalSeparator;return new RegExp("\\d"+(!o||0===a||e||n?"":"|"+v(o)),t?"g":void 0)}},{key:"getSeparators",value:function(){var t=this.props.decimalSeparator,e=this.props.thousandSeparator;return!0===e&&(e=","),{decimalSeparator:t,thousandSeparator:e}}},{key:"getMaskAtIndex",value:function(t){var e=this.props.mask,r=void 0===e?" ":e;return"string"==typeof r?r:r[t]||" "}},{key:"getValueObject",value:function(t,e){var r=parseFloat(e);return{formattedValue:t,value:e,floatValue:isNaN(r)?void 0:r}}},{key:"validateProps",value: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: ".concat(n,' (thousandSeparator = {true} is same as thousandSeparator = ",")\n decimalSeparator: ').concat(r," (default value for decimalSeparator is .)\n "));if(t&&("string"===t?t:t.toString()).match(/\d/g))throw new Error("\n Mask ".concat(t," should not contain numeric character;\n "))}},{key:"setPatchedCaretPosition",value:function(t,e,r){S(t,e),setTimeout(function(){t.value===r&&S(t,e)},0)}},{key:"correctCaretPosition",value:function(t,e,r){var n=this.props,a=n.prefix,o=n.suffix,i=n.format;if(""===t)return 0;if(e=b(e,0,t.length),!i){var s="-"===t[0];return b(e,a.length+(s?1:0),t.length-o.length)}if("function"==typeof i)return e;if("#"===i[e]&&d(t[e]))return e;if("#"===i[e-1]&&d(t[e-1]))return e;var u=i.indexOf("#");e=b(e,u,i.lastIndexOf("#")+1);for(var l=i.substring(e,i.length).indexOf("#"),f=e,c=e+(-1===l?0:l);u<f&&("#"!==i[f]||!d(t[f]));)f-=1;return!d(t[c])||"left"===r&&e!==u||e-f<c-e?d(t[f])?f+1:f:c}},{key:"getCaretPosition",value:function(t,e,r){var n,a,o=this.props.format,i=this.state.value,s=this.getNumberRegex(!0),u=(t.match(s)||[]).join(""),l=(e.match(s)||[]).join("");for(a=n=0;a<r;a++){var f=t[a]||"",c=e[n]||"";if((f.match(s)||f===c)&&("0"!==f||!c.match(s)||"0"===c||u.length===l.length)){for(;f!==e[n]&&n<e.length;)n++;n++}}return"string"!=typeof o||i||(n=e.length),n=this.correctCaretPosition(e,n)}},{key:"removePrefixAndSuffix",value:function(t){var e=this.props,r=e.format,n=e.prefix,a=e.suffix;if(!r&&t){var o="-"===t[0];o&&(t=t.substring(1,t.length));var i=(t=n&&0===t.indexOf(n)?t.substring(n.length,t.length):t).lastIndexOf(a);t=a&&-1!==i&&i===t.length-a.length?t.substring(0,i):t,o&&(t="-"+t)}return t}},{key:"removePatternFormatting",value:function(t){for(var e=this.props.format.split("#").filter(function(t){return""!==t}),r=0,n="",a=0,o=e.length;a<=o;a++){var i=e[a]||"",s=a===o?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("")}},{key:"removeFormatting",value: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}},{key:"formatWithPattern",value:function(t){for(var e=this.props.format,r=0,n=e.split(""),a=0,o=e.length;a<o;a++)"#"===e[a]&&(n[a]=t[r]||this.getMaskAtIndex(r),r+=1);return n.join("")}},{key:"formatAsNumber",value:function(t){var e=this.props,r=e.decimalScale,n=e.fixedDecimalScale,a=e.prefix,o=e.suffix,i=e.allowNegative,s=e.thousandsGroupStyle,u=this.getSeparators(),l=u.thousandSeparator,f=u.decimalSeparator,c=-1!==t.indexOf(".")||r&&n,p=P(t,i),h=p.beforeDecimal,g=p.afterDecimal,m=p.addNegation;if(void 0!==r&&(g=y(g,r,n)),l){var d=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}}(s);h=h.replace(d,"$1"+l)}return a&&(h=a+h),o&&(g+=o),m&&(h="-"+h),t=h+(c&&f||"")+g}},{key:"formatNumString",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",e=this.props,r=e.format,n=e.allowEmptyFormatting,a=t;return a=""!==t||n?"-"!==t||r?"string"==typeof r?this.formatWithPattern(a):"function"==typeof r?r(a):this.formatAsNumber(a):"-":""}},{key:"formatValueProp",value:function(t){var e=this.props,r=e.format,n=e.decimalScale,a=e.fixedDecimalScale,o=e.allowEmptyFormatting,i=this.props,s=i.value,u=void 0===s?t:s,l=i.isNumericString,f=!u&&0!==u;return f&&o&&(u=""),f&&!o?"":("number"==typeof u&&(u=u.toString(),l=!0),"Infinity"===u&&l&&(u=""),l&&!r&&"number"==typeof n&&(u=function(t,e,r){if(-1!==["","-"].indexOf(t))return t;var n=-1!==t.indexOf(".")&&e,a=P(t),o=a.beforeDecimal,i=a.afterDecimal,s=a.hasNagation,u=parseFloat("0.".concat(i||"0")).toFixed(e).split("."),l=o.split("").reverse().reduce(function(t,e,r){return t.length>r?(Number(t[0])+Number(e)).toString()+t.substring(1,t.length):e+t},u[0]),f=y(u[1]||"",Math.min(e,i.length),r),c=n?".":"";return"".concat(s?"-":"").concat(l).concat(c).concat(f)}(u,n,a)),l?this.formatNumString(u):this.formatInput(u))}},{key:"formatNegation",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",e=this.props.allowNegative,r=new RegExp("(-)"),n=new RegExp("(-)(.)*(-)"),a=r.test(t),o=n.test(t);return t=t.replace(/-/g,""),a&&!o&&e&&(t="-"+t),t}},{key:"formatInput",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";return this.props.format||(t=this.removePrefixAndSuffix(t),t=this.formatNegation(t)),t=this.removeFormatting(t),this.formatNumString(t)}},{key:"isCharacterAFormat",value:function(t,e){var r=this.props,n=r.format,a=r.prefix,o=r.suffix,i=r.decimalScale,s=r.fixedDecimalScale,u=this.getSeparators().decimalSeparator;return"string"==typeof n&&"#"!==n[t]||!(n||!(t<a.length||t>=e.length-o.length||i&&s&&e[t]===u))}},{key:"checkIfFormatGotDeleted",value:function(t,e,r){for(var n=t;n<e;n++)if(this.isCharacterAFormat(n,r))return!0;return!1}},{key:"correctInputValue",value:function(t,e,r){var n=this.props,a=n.format,o=n.allowNegative,i=n.prefix,s=n.suffix,u=this.getSeparators().decimalSeparator,l=this.state.numAsString||"",f=this.selectionBeforeInput,c=f.selectionStart,p=f.selectionEnd,h=function(t,e){for(var r=0,n=0,a=t.length,o=e.length;t[r]===e[r]&&r<a;)r++;for(;t[a-1-n]===e[o-1-n]&&r<o-n&&r<a-n;)n++;return{start:r,end:a-n}}(e,r),g=h.start,m=h.end;if(!a&&"."!==u&&g===m&&"."===r[c])return r.substr(0,c)+u+r.substr(c+1,r.length);var d=a?0:i.length,v=e.length-(a?0:s.length);if(r.length>e.length||!r.length||g===m||0===c&&p===e.length||c===d&&p===v)return r;if(this.checkIfFormatGotDeleted(g,m,e)&&(r=e),!a){var y=this.removeFormatting(r),S=P(y,o),b=S.beforeDecimal,x=S.afterDecimal,O=S.addNegation,w=t<r.indexOf(u)+1;if(y.length<l.length&&w&&""===b&&!parseFloat(x))return O?"-":""}return r}},{key:"updateValue",value:function(t){var e=this,r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:m,n=t.formattedValue,a=t.input,o=t.numAsString,i=t.caretPos,s=this.props.onValueChange,u=this.state.value;if(a){if(!i){var l=t.inputValue||a.value,f=x(a);i=this.getCaretPosition(l,n,f)}a.value=n,this.setPatchedCaretPosition(a,i,n)}void 0===o&&(o=this.removeFormatting(n)),n!==u?this.setState({value:n,numAsString:o},function(){s(e.getValueObject(n,o)),r()}):r()}},{key:"onChange",value:function(t){t.persist();var e=t.target,r=e.value,n=this.state,a=this.props,o=a.isAllowed,i=n.value||"",s=x(e);r=this.correctInputValue(s,i,r);var u=this.formatInput(r)||"",l=this.removeFormatting(u);o(this.getValueObject(u,l))||(u=i),this.updateValue({formattedValue:u,numAsString:l,inputValue:r,input:e},function(){a.onChange(t)})}},{key:"onBlur",value:function(t){var e=this.props,r=this.state,n=e.format,a=e.onBlur,o=r.numAsString,i=r.value;if(this.focusedElm=null,!n){o=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",a=r[1]||"";return"".concat(e?"-":"").concat(n).concat(a?".".concat(a):"")}(o);var s=this.formatNumString(o);if(s!==i)return t.persist(),void this.updateValue({formattedValue:s,numAsString:o},function(){a(t)})}a(t)}},{key:"onKeyDown",value:function(t){var e,r=t.target,n=t.key,a=r.selectionStart,o=r.selectionEnd,i=r.value,s=void 0===i?"":i,u=this.props,l=u.decimalScale,f=u.fixedDecimalScale,c=u.prefix,p=u.suffix,h=u.format,g=u.onKeyDown,m=(u.onValueChange,void 0!==l&&f),d=this.getNumberRegex(!1,m),v=new RegExp("-"),y="string"==typeof h;if(this.selectionBeforeInput={selectionStart:a,selectionEnd:o},"ArrowLeft"===n||"Backspace"===n?e=a-1:"ArrowRight"===n?e=a+1:"Delete"===n&&(e=a),void 0!==e&&a===o){var S=e,b=y?h.indexOf("#"):c.length,x=y?h.lastIndexOf("#")+1:s.length-p.length;if("ArrowLeft"===n||"ArrowRight"===n){var O="ArrowLeft"===n?"left":"right";S=this.correctCaretPosition(s,e,O)}else if("Delete"!==n||d.test(s[e])||v.test(s[e])){if("Backspace"===n&&!d.test(s[e]))if(a<=b+1&&"-"===s[0]&&void 0===h){var w=s.substring(1);t.persist(),this.updateValue({formattedValue:w,caretPos:S,input:r})}else if(!v.test(s[e])){for(;!d.test(s[S-1])&&b<S;)S--;S=this.correctCaretPosition(s,S,"left")}}else for(;!d.test(s[S])&&S<x;)S++;(S!==e||e<b||x<e)&&(t.preventDefault(),this.setPatchedCaretPosition(r,S,s)),t.isUnitTestRun&&this.setPatchedCaretPosition(r,S,s),this.props.onKeyDown(t)}else g(t)}},{key:"onMouseUp",value:function(t){var e=t.target,r=e.selectionStart,n=e.selectionEnd,a=e.value,o=void 0===a?"":a;if(r===n){var i=this.correctCaretPosition(o,r);i!==r&&this.setPatchedCaretPosition(e,i,o)}this.props.onMouseUp(t)}},{key:"onFocus",value:function(i){var s=this;i.persist(),this.focusedElm=i.target,setTimeout(function(){var t=i.target,e=t.selectionStart,r=t.selectionEnd,n=t.value,a=void 0===n?"":n,o=s.correctCaretPosition(a,e);o===e||0===e&&r===a.length||s.setPatchedCaretPosition(t,o,a),s.props.onFocus(i)},0)}},{key:"render",value:function(){var e,r,n,t=this.props,a=t.type,o=t.displayType,i=t.customInput,s=t.renderText,u=t.getInputRef,l=this.state.value,f=(e=this.props,r=O,n={},Object.keys(e).forEach(function(t){r[t]||(n[t]=e[t])}),n),c=g({},f,{type:a,value:l,onChange:this.onChange,onKeyDown:this.onKeyDown,onMouseUp:this.onMouseUp,onFocus:this.onFocus,onBlur:this.onBlur});if("text"===o)return s?s(l)||null:h.createElement("span",g({},f,{ref:u}),l);if(i){var p=i;return h.createElement(p,c)}return h.createElement("input",g({},c,{ref:u}))}}])&&a(e.prototype,r),n&&a(e,n),i}();return k.propTypes=O,k.defaultProps=w,k});

@@ -513,13 +513,12 @@ "use strict";

value: function formatNumString() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var numStr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var _this$props6 = this.props,
format = _this$props6.format,
allowEmptyFormatting = _this$props6.allowEmptyFormatting;
var formattedValue = value;
var formattedValue = numStr;
if (value === '' && !allowEmptyFormatting) {
if (numStr === '' && !allowEmptyFormatting) {
formattedValue = '';
} else if (value === '-' && !format) {
} else if (numStr === '-' && !format) {
formattedValue = '-';
value = '';
} else if (typeof format === 'string') {

@@ -559,2 +558,7 @@ formattedValue = this.formatWithPattern(formattedValue);

isNumericString = true;
} //change infinity value to empty string
if (value === 'Infinity' && isNumericString) {
value = '';
} //round the number based on decimalScale

@@ -561,0 +565,0 @@ //format only if non formatted value is provided

{
"name": "react-number-format",
"description": "React component to format number in an input or as a text.",
"version": "4.0.5",
"version": "4.0.6",
"main": "lib/number_format.js",

@@ -6,0 +6,0 @@ "author": "Sudhanshu Yadav",

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