UNPKG

react-hook-currency

Version:

> This libraries propose to introduce two pseudo hooks, one capable of format currencies and another capable to simulate a cash register.

3 lines (2 loc) 7.22 kB
"use strict";function _extends(){return(_extends=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e}).apply(this,arguments)}var Currencies,Locales;function removeNonNumerics(e){return null==e?void 0:e.replace(/[^0-9]/g,"")}function removeNonNumericsExceptDash(e){return("-"===(null==e?void 0:e.charAt(0))?"-":"")+(null==e?void 0:e.replace(/[^0-9]/g,""))}function removeNonRegister(e,r){var n=new RegExp("[^0-9\\/\\=\\+\\-\\*\\"+r+"]","g");return e?e.replace(n,""):e}function getOperators(e){return removeNonRegister(e).substr(1).split(/[^\+\-\*\/]/).filter(String)}function getNumbers(e,r){var n=new RegExp("[^0-9\\"+r+"]","g");return removeNonRegister(e,r).split(n).filter(String).map((function(r,n){return 0===n?""+hasDash(e)+r:r}))}function hasDash(e){return"-"===e.charAt(0)?"-":""}function getDecimalSeparator(e){return 1.1.toLocaleString(e).substring(1,2)}function calculator(num1,num2,operator,decimalSeparator){var _eval,val1=Number(num1.split(decimalSeparator).join(".")),val2=Number(num2.split(decimalSeparator).join("."));return null==(_eval=eval(""+val1+operator+val2))?void 0:_eval.toFixed(2)}function replaceAllCalc(e){return e.includes("x")&&(e=e.split("x").join("*")),e.includes("÷")&&(e=e.split("÷").join("/")),e}function replaceAllNoCalc(e){return e.includes("*")&&(e=e.split("*").join("x")),e.includes("/")&&(e=e.split("/").join("÷")),e}Object.defineProperty(exports,"__esModule",{value:!0}),function(){if("function"==typeof window.CustomEvent)return!1;function e(e,r){r=r||{bubbles:!1,cancelable:!1,detail:void 0};var n=document.createEvent("CustomEvent");return n.initCustomEvent(e,r.bubbles,r.cancelable,r.detail),n}e.prototype=window.Event.prototype,window.CustomEvent=e,window.Event=e}(),Array.prototype.includes||(Array.prototype.includes=function(e){var r=!1;return-1!==this.indexOf(e)&&(r=!0),r}),String.prototype.includes||(String.prototype.includes=function(e){var r=!1;return-1!==this.indexOf(e)&&(r=!0),r}),Array.prototype.find||(Array.prototype.find=function(e){if(null==this)throw new TypeError("Array.prototype.find called on null or undefined");if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var r,n=Object(this),t=n.length>>>0,l=arguments[1],u=0;u<t;u++)if(u in n&&e.call(l,r=n[u],u,n))return r}),Currencies=exports.Currencies||(exports.Currencies={}),Currencies["Australian Dollar"]="AUD",Currencies["Brazilian Real"]="BRL",Currencies["Canadian Dollar"]="CAD",Currencies.Euro="EUR",Currencies["Pound Sterling"]="GBP",Currencies["US Dollar"]="USD",Currencies["Swiss Franc"]="CHF",Locales=exports.Locales||(exports.Locales={}),Locales.English="en",Locales["English (Australia)"]="en-AU",Locales["English (Canada)"]="en-CA",Locales["English (United Kingdom)"]="en-GB",Locales["English (United States)"]="en-US",Locales["Swiss German"]="gsw",Locales["Swiss German (Switzerland)"]="gsw-CH",Locales["Portuguese (Brazil)"]="pt-BR",Locales.Portuguese="pt";var defaultValues={precision:2,style:"currency",locale:exports.Locales["Portuguese (Brazil)"],currency:exports.Currencies["Brazilian Real"],negative:"allow"},useCurrency=function(e){var r=_extends({},defaultValues,e),n=r.style,t=r.locale,l=r.precision,u=r.currency,i=r.negative,a=getDecimalSeparator(t),o=function(e){return""+c(e)+(Number(removeNonNumerics(e))/Math.pow(10,l)).toLocaleString(t,{style:n,currency:u,minimumIntegerDigits:1,minimumFractionDigits:l})},c=function(e){var r="",n=null==e?void 0:e.charAt(0),t=null==e?void 0:e.substring((null==e?void 0:e.length)-1);return"always"===i?r="-":"allow"===i&&("-"===t?"-"!==n&&(r="-"):"-"===n&&(r="-")),r};return{onClick:function(e){e.currentTarget.setSelectionRange(e.currentTarget.value.length,e.currentTarget.value.length)},onChange:function(e){e.currentTarget.value=o(e.currentTarget.value)},onKeyDown:function(e){if(["Delete"].includes(e.key)){var r=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,"value"),n=null==r?void 0:r.set;if(n){var t,l=e.currentTarget.value;n.call(e.currentTarget,l.substring(0,l.length-1));var u=new Event("input",{bubbles:!0});null==(t=e.currentTarget)||t.dispatchEvent(u)}}},format:o,decimalSeparator:a,toNumber:function(e){return Number(removeNonNumericsExceptDash(e))/Math.pow(10,l)}}},operators=["-","+","/","*"],defaultValues$1={precision:2,style:"currency",locale:exports.Locales["Portuguese (Brazil)"],currency:exports.Currencies["Brazilian Real"]},useCurrencyRegister=function(e){var r=_extends({},defaultValues$1,e),n=r.style,t=r.locale,l=r.precision,u=r.currency,i=getDecimalSeparator(t),a=function(e,r){return(Number(removeNonNumericsExceptDash(e))/Math.pow(10,l)).toLocaleString(t,{style:r||n,currency:u,minimumIntegerDigits:1,minimumFractionDigits:l})},o=function(e){return operators.includes(e)},c=function(e){var r=e.substr(1);return operators.find((function(e){return r.includes(e)}))},s=function(e,r){var n="";return r.length&&e.length>1&&(n=String(calculator(e[0],e[1],r[0],i))),n};return{onClick:function(e){e.currentTarget.setSelectionRange(e.currentTarget.value.length,e.currentTarget.value.length)},onChange:function(e){var r,n,t,l=replaceAllCalc(e.currentTarget.value),u=l.slice(-1),p=getOperators(l),v=getNumbers(l,i);if(o(u))if(1===p.length){var g;e.currentTarget.value=a(null==v?void 0:v[0])+" "+(null==p||null==(g=p[0])?void 0:g.slice(-1))+" "}else{var d=s(v,p);l&&(e.currentTarget.value=a(d)+" "+(null==p?void 0:p[p.length-1]))}else if(u===i&&null!=v&&null!=(r=v[1])&&null!=(n=r.substr(0,(null==v||null==(t=v[1])?void 0:t.length)-1))&&n.includes(i))e.currentTarget.value=l.substr(0,l.length-1);else if(c(removeNonRegister(l))){var f="";"="===u&&(f=1===v.length?null==v?void 0:v[0]:s(v,p)),e.currentTarget.value=f?a(f):a(null==v?void 0:v[0])+" "+(null==p?void 0:p[0])+" "+(["-","+"].includes(null==p?void 0:p[0])?a(null==v?void 0:v[1],"decimal"):(null==v?void 0:v[1])||"")}else e.currentTarget.value=a(l);e.currentTarget.value=replaceAllNoCalc(e.currentTarget.value)},toNumber:function(e){return Number(removeNonNumericsExceptDash(e))/Math.pow(10,l)},onKeyDown:function(e){if(["Delete","Backspace"].includes(e.key)){e.preventDefault();var r=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,"value"),n=null==r?void 0:r.set;if(n){var t,l=e.currentTarget.value.trim(),u=getNumbers(l,i),a=removeNonNumerics(null==u?void 0:u[1]);null==n||n.call(e.currentTarget,0===Number(a)?l.substring(0,l.length-5):l.substring(0,l.length-1));var o=new Event("input",{bubbles:!0});null==(t=e.currentTarget)||t.dispatchEvent(o)}}},triggerChange:function(e,r){if(r){var n=null==r?void 0:r.value,t=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,"value"),l=null==t?void 0:t.set;if(l){var u=new Event("input",{bubbles:!0});switch(e){case"L":var i=new KeyboardEvent("keydown",{bubbles:!0,key:"Delete"});null==r||r.dispatchEvent(i);break;case"C":null==l||l.call(r,""),null==r||r.dispatchEvent(u);break;default:null==l||l.call(r,""+n+e),null==r||r.dispatchEvent(u)}}}},format:a,decimalSeparator:i}};exports.useCurrency=useCurrency,exports.useCurrencyRegister=useCurrencyRegister; //# sourceMappingURL=react-hook-currency.cjs.production.min.js.map