react-intl-number-format
Version:
Number and currency formatter based on the ECMAScript Internationalization API
3 lines (2 loc) • 17.2 kB
JavaScript
import e from"get-user-locale";import r,{createContext as n,useContext as t,useMemo as o,useState as i}from"react";function a(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function c(){return(c=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)}function u(e){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{},t=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),t.forEach(function(r){a(e,r,n[r])})}return e}function f(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function s(e,r){return e(r={exports:{}},r.exports),r.exports}var l=s(function(e,r){Object.defineProperty(r,"__esModule",{value:!0});var n="function"==typeof Symbol&&Symbol.for,t=n?Symbol.for("react.element"):60103,o=n?Symbol.for("react.portal"):60106,i=n?Symbol.for("react.fragment"):60107,a=n?Symbol.for("react.strict_mode"):60108,c=n?Symbol.for("react.profiler"):60114,u=n?Symbol.for("react.provider"):60109,f=n?Symbol.for("react.context"):60110,s=n?Symbol.for("react.async_mode"):60111,l=n?Symbol.for("react.concurrent_mode"):60111,p=n?Symbol.for("react.forward_ref"):60112,y=n?Symbol.for("react.suspense"):60113,m=n?Symbol.for("react.memo"):60115,d=n?Symbol.for("react.lazy"):60116;function b(e){if("object"==typeof e&&null!==e){var r=e.$$typeof;switch(r){case t:switch(e=e.type){case s:case l:case i:case c:case a:case y:return e;default:switch(e=e&&e.$$typeof){case f:case p:case u:return e;default:return r}}case d:case m:case o:return r}}}function v(e){return b(e)===l}r.typeOf=b,r.AsyncMode=s,r.ConcurrentMode=l,r.ContextConsumer=f,r.ContextProvider=u,r.Element=t,r.ForwardRef=p,r.Fragment=i,r.Lazy=d,r.Memo=m,r.Portal=o,r.Profiler=c,r.StrictMode=a,r.Suspense=y,r.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===i||e===l||e===c||e===a||e===y||"object"==typeof e&&null!==e&&(e.$$typeof===d||e.$$typeof===m||e.$$typeof===u||e.$$typeof===f||e.$$typeof===p)},r.isAsyncMode=function(e){return v(e)||b(e)===s},r.isConcurrentMode=v,r.isContextConsumer=function(e){return b(e)===f},r.isContextProvider=function(e){return b(e)===u},r.isElement=function(e){return"object"==typeof e&&null!==e&&e.$$typeof===t},r.isForwardRef=function(e){return b(e)===p},r.isFragment=function(e){return b(e)===i},r.isLazy=function(e){return b(e)===d},r.isMemo=function(e){return b(e)===m},r.isPortal=function(e){return b(e)===o},r.isProfiler=function(e){return b(e)===c},r.isStrictMode=function(e){return b(e)===a},r.isSuspense=function(e){return b(e)===y}});f(l);var p=s(function(e,r){"production"!==process.env.NODE_ENV&&function(){Object.defineProperty(r,"__esModule",{value:!0});var e="function"==typeof Symbol&&Symbol.for,n=e?Symbol.for("react.element"):60103,t=e?Symbol.for("react.portal"):60106,o=e?Symbol.for("react.fragment"):60107,i=e?Symbol.for("react.strict_mode"):60108,a=e?Symbol.for("react.profiler"):60114,c=e?Symbol.for("react.provider"):60109,u=e?Symbol.for("react.context"):60110,f=e?Symbol.for("react.async_mode"):60111,s=e?Symbol.for("react.concurrent_mode"):60111,l=e?Symbol.for("react.forward_ref"):60112,p=e?Symbol.for("react.suspense"):60113,y=e?Symbol.for("react.memo"):60115,m=e?Symbol.for("react.lazy"):60116;function d(e){if("object"==typeof e&&null!==e){var r=e.$$typeof;switch(r){case n:var d=e.type;switch(d){case f:case s:case o:case a:case i:case p:return d;default:var b=d&&d.$$typeof;switch(b){case u:case l:case c:return b;default:return r}}case m:case y:case t:return r}}}var b=f,v=s,g=u,h=c,O=n,S=l,w=o,E=m,j=y,x=t,P=a,D=i,T=p,_=!1;function I(e){return d(e)===s}r.typeOf=d,r.AsyncMode=b,r.ConcurrentMode=v,r.ContextConsumer=g,r.ContextProvider=h,r.Element=O,r.ForwardRef=S,r.Fragment=w,r.Lazy=E,r.Memo=j,r.Portal=x,r.Profiler=P,r.StrictMode=D,r.Suspense=T,r.isValidElementType=function(e){return"string"==typeof e||"function"==typeof e||e===o||e===s||e===a||e===i||e===p||"object"==typeof e&&null!==e&&(e.$$typeof===m||e.$$typeof===y||e.$$typeof===c||e.$$typeof===u||e.$$typeof===l)},r.isAsyncMode=function(e){return _||(_=!0,function(e,r){if(void 0===r)throw new Error("`lowPriorityWarning(condition, format, ...args)` requires a warning message argument");if(!e){for(var n=arguments.length,t=Array(n>2?n-2:0),o=2;o<n;o++)t[o-2]=arguments[o];(function(e){for(var r=arguments.length,n=Array(r>1?r-1:0),t=1;t<r;t++)n[t-1]=arguments[t];var o=0,i="Warning: "+e.replace(/%s/g,function(){return n[o++]});"undefined"!=typeof console&&console.warn(i);try{throw new Error(i)}catch(e){}}).apply(void 0,[r].concat(t))}}(!1,"The ReactIs.isAsyncMode() alias has been deprecated, and will be removed in React 17+. Update your code to use ReactIs.isConcurrentMode() instead. It has the exact same API.")),I(e)||d(e)===f},r.isConcurrentMode=I,r.isContextConsumer=function(e){return d(e)===u},r.isContextProvider=function(e){return d(e)===c},r.isElement=function(e){return"object"==typeof e&&null!==e&&e.$$typeof===n},r.isForwardRef=function(e){return d(e)===l},r.isFragment=function(e){return d(e)===o},r.isLazy=function(e){return d(e)===m},r.isMemo=function(e){return d(e)===y},r.isPortal=function(e){return d(e)===t},r.isProfiler=function(e){return d(e)===a},r.isStrictMode=function(e){return d(e)===i},r.isSuspense=function(e){return d(e)===p}}()});f(p);var y=s(function(e){e.exports="production"===process.env.NODE_ENV?l:p}),m=Object.getOwnPropertySymbols,d=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable,v=function(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de","5"===Object.getOwnPropertyNames(e)[0])return!1;for(var r={},n=0;n<10;n++)r["_"+String.fromCharCode(n)]=n;if("0123456789"!==Object.getOwnPropertyNames(r).map(function(e){return r[e]}).join(""))return!1;var t={};return"abcdefghijklmnopqrst".split("").forEach(function(e){t[e]=e}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},t)).join("")}catch(e){return!1}}()?Object.assign:function(e,r){for(var n,t,o=function(e){if(null==e)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}(e),i=1;i<arguments.length;i++){for(var a in n=Object(arguments[i]))d.call(n,a)&&(o[a]=n[a]);if(m){t=m(n);for(var c=0;c<t.length;c++)b.call(n,t[c])&&(o[t[c]]=n[t[c]])}}return o},g="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED",h=function(){};if("production"!==process.env.NODE_ENV){var O=g,S={},w=Function.call.bind(Object.prototype.hasOwnProperty);h=function(e){var r="Warning: "+e;"undefined"!=typeof console&&console.error(r);try{throw new Error(r)}catch(e){}}}function E(e,r,n,t,o){if("production"!==process.env.NODE_ENV)for(var i in e)if(w(e,i)){var a;try{if("function"!=typeof e[i]){var c=Error((t||"React class")+": "+n+" type `"+i+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[i]+"`.");throw c.name="Invariant Violation",c}a=e[i](r,i,t,n,null,O)}catch(e){a=e}if(!a||a instanceof Error||h((t||"React class")+": type specification of "+n+" `"+i+"` is invalid; the type checker function must return `null` or an `Error` but returned a "+typeof a+". You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument)."),a instanceof Error&&!(a.message in S)){S[a.message]=!0;var u=o?o():"";h("Failed "+n+" type: "+a.message+(null!=u?u:""))}}}E.resetWarningCache=function(){"production"!==process.env.NODE_ENV&&(S={})};var j=E,x=Function.call.bind(Object.prototype.hasOwnProperty),P=function(){};function D(){return null}function T(){}function _(){}"production"!==process.env.NODE_ENV&&(P=function(e){var r="Warning: "+e;"undefined"!=typeof console&&console.error(r);try{throw new Error(r)}catch(e){}}),_.resetWarningCache=T;var I=s(function(e){e.exports="production"!==process.env.NODE_ENV?function(e,r){var n="function"==typeof Symbol&&Symbol.iterator,t="@@iterator",o="<<anonymous>>",i={array:u("array"),bool:u("boolean"),func:u("function"),number:u("number"),object:u("object"),string:u("string"),symbol:u("symbol"),any:c(D),arrayOf:function(e){return c(function(r,n,t,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+t+"` has invalid PropType notation inside arrayOf.");var c=r[n];if(!Array.isArray(c))return new a("Invalid "+o+" `"+i+"` of type `"+s(c)+"` supplied to `"+t+"`, expected an array.");for(var u=0;u<c.length;u++){var f=e(c,u,t,o,i+"["+u+"]",g);if(f instanceof Error)return f}return null})},element:c(function(r,n,t,o,i){var c=r[n];return e(c)?null:new a("Invalid "+o+" `"+i+"` of type `"+s(c)+"` supplied to `"+t+"`, expected a single ReactElement.")}),elementType:c(function(e,r,n,t,o){var i=e[r];return y.isValidElementType(i)?null:new a("Invalid "+t+" `"+o+"` of type `"+s(i)+"` supplied to `"+n+"`, expected a single ReactElement type.")}),instanceOf:function(e){return c(function(r,n,t,i,c){var u;return r[n]instanceof e?null:new a("Invalid "+i+" `"+c+"` of type `"+((u=r[n]).constructor&&u.constructor.name?u.constructor.name:o)+"` supplied to `"+t+"`, expected instance of `"+(e.name||o)+"`.")})},node:c(function(e,r,n,t,o){return f(e[r])?null:new a("Invalid "+t+" `"+o+"` supplied to `"+n+"`, expected a ReactNode.")}),objectOf:function(e){return c(function(r,n,t,o,i){if("function"!=typeof e)return new a("Property `"+i+"` of component `"+t+"` has invalid PropType notation inside objectOf.");var c=r[n],u=s(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+t+"`, expected an object.");for(var f in c)if(x(c,f)){var l=e(c,f,t,o,i+"."+f,g);if(l instanceof Error)return l}return null})},oneOf:function(e){return Array.isArray(e)?c(function(r,n,t,o,i){for(var c=r[n],u=0;u<e.length;u++)if((f=c)===(s=e[u])?0!==f||1/f==1/s:f!=f&&s!=s)return null;var f,s,p=JSON.stringify(e,function(e,r){return"symbol"===l(r)?String(r):r});return new a("Invalid "+o+" `"+i+"` of value `"+String(c)+"` supplied to `"+t+"`, expected one of "+p+".")}):("production"!==process.env.NODE_ENV&&P(arguments.length>1?"Invalid arguments supplied to oneOf, expected an array, got "+arguments.length+" arguments. A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]).":"Invalid argument supplied to oneOf, expected an array."),D)},oneOfType:function(e){if(!Array.isArray(e))return"production"!==process.env.NODE_ENV&&P("Invalid argument supplied to oneOfType, expected an instance of array."),D;for(var r=0;r<e.length;r++){var n=e[r];if("function"!=typeof n)return P("Invalid argument supplied to oneOfType. Expected an array of check functions, but received "+p(n)+" at index "+r+"."),D}return c(function(r,n,t,o,i){for(var c=0;c<e.length;c++)if(null==(0,e[c])(r,n,t,o,i,g))return null;return new a("Invalid "+o+" `"+i+"` supplied to `"+t+"`.")})},shape:function(e){return c(function(r,n,t,o,i){var c=r[n],u=s(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+t+"`, expected `object`.");for(var f in e){var l=e[f];if(l){var p=l(c,f,t,o,i+"."+f,g);if(p)return p}}return null})},exact:function(e){return c(function(r,n,t,o,i){var c=r[n],u=s(c);if("object"!==u)return new a("Invalid "+o+" `"+i+"` of type `"+u+"` supplied to `"+t+"`, expected `object`.");var f=v({},r[n],e);for(var l in f){var p=e[l];if(!p)return new a("Invalid "+o+" `"+i+"` key `"+l+"` supplied to `"+t+"`.\nBad object: "+JSON.stringify(r[n],null," ")+"\nValid keys: "+JSON.stringify(Object.keys(e),null," "));var y=p(c,l,t,o,i+"."+l,g);if(y)return y}return null})}};function a(e){this.message=e,this.stack=""}function c(e){if("production"!==process.env.NODE_ENV)var n={},t=0;function i(i,c,u,f,s,l,p){if(f=f||o,l=l||u,p!==g){if(r){var y=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 y.name="Invariant Violation",y}if("production"!==process.env.NODE_ENV&&"undefined"!=typeof console){var m=f+":"+u;!n[m]&&t<3&&(P("You are manually calling a React.PropTypes validation function for the `"+l+"` prop on `"+f+"`. This is deprecated and will throw in the standalone `prop-types` package. You may be seeing this warning due to a third-party PropTypes library. See https://fb.me/react-warning-dont-call-proptypes for details."),n[m]=!0,t++)}}return null==c[u]?i?new a(null===c[u]?"The "+s+" `"+l+"` is marked as required in `"+f+"`, but its value is `null`.":"The "+s+" `"+l+"` is marked as required in `"+f+"`, but its value is `undefined`."):null:e(c,u,f,s,l)}var c=i.bind(null,!1);return c.isRequired=i.bind(null,!0),c}function u(e){return c(function(r,n,t,o,i,c){var u=r[n];return s(u)!==e?new a("Invalid "+o+" `"+i+"` of type `"+l(u)+"` supplied to `"+t+"`, expected `"+e+"`."):null})}function f(r){switch(typeof r){case"number":case"string":case"undefined":return!0;case"boolean":return!r;case"object":if(Array.isArray(r))return r.every(f);if(null===r||e(r))return!0;var o=function(e){var o=r&&(n&&r[n]||r[t]);if("function"==typeof o)return o}();if(!o)return!1;var i,a=o.call(r);if(o!==r.entries){for(;!(i=a.next()).done;)if(!f(i.value))return!1}else for(;!(i=a.next()).done;){var c=i.value;if(c&&!f(c[1]))return!1}return!0;default:return!1}}function s(e){var r=typeof e;return Array.isArray(e)?"array":e instanceof RegExp?"object":function(e,r){return"symbol"===e||!!r&&("Symbol"===r["@@toStringTag"]||"function"==typeof Symbol&&r instanceof Symbol)}(r,e)?"symbol":r}function l(e){if(null==e)return""+e;var r=s(e);if("object"===r){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return r}function p(e){var r=l(e);switch(r){case"array":case"object":return"an "+r;case"boolean":case"date":case"regexp":return"a "+r;default:return r}}return a.prototype=Error.prototype,i.checkPropTypes=j,i.resetWarningCache=j.resetWarningCache,i.PropTypes=i,i}(y.isElement,!0):function(){function e(e,r,n,t,o,i){if(i!==g){var a=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 a.name="Invariant Violation",a}}function r(){return e}e.isRequired=e;var n={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:r,element:e,elementType:e,instanceOf:r,node:e,objectOf:r,oneOf:r,oneOfType:r,shape:r,exact:r,checkPropTypes:_,resetWarningCache:T};return n.PropTypes=n,n}()}),N={locale:e(),options:{currency:"USD",currencyDisplay:"symbol",localeMatcher:"best fit",maximumFractionDigits:2,maximumSignificantDigits:21,minimumFractionDigits:2,minimumIntegerDigits:1,minimumSignificantDigits:1,useGrouping:!0}},$=n(),C=function(){return t($)},k=function(e){var n,t=function(e){if(Array.isArray(e))return e}(n=i(u({},N,e.config)))||function(e,r){var n=[],t=!0,o=!1,i=void 0;try{for(var a,c=e[Symbol.iterator]();!(t=(a=c.next()).done)&&(n.push(a.value),2!==n.length);t=!0);}catch(e){o=!0,i=e}finally{try{t||null==c.return||c.return()}finally{if(o)throw i}}return n}(n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}(),a=t[0],f=t[1],s=o(function(){return{config:a,setConfig:f}},[a]);return r.createElement($.Provider,c({value:s},e))},M=function(e){var n=e.as,t=e.locale,o=e.children,i=e.localeMatcher,a=e.currency,c=e.currencyStyle,f=e.currencyDisplay,s=e.useGrouping,l=e.minimumIntegerDigits,p=e.minimumFractionDigits,y=e.maximumFractionDigits,m=e.minimumSignificantDigits,d=e.maximumSignificantDigits,b=e.notation,v=e.compactDisplay,g=C(),h=g&&g.config.locale,O=g&&g.config.options,S=function(e){return void 0!==e},w=u({},S(i)&&{localeMatcher:i},S(a)&&{currency:a},S(c)&&{style:c},S(f)&&{currencyDisplay:f},S(s)&&{useGrouping:s},S(l)&&{minimumIntegerDigits:l},S(p)&&{minimumFractionDigits:p},S(y)&&{maximumFractionDigits:y},S(m)&&{minimumSignificantDigits:m},S(d)&&{maximumSignificantDigits:d},S(b)&&{notation:b},S(v)&&{compactDisplay:v}),E=t||h||N.locale,j=u({},O,w),x=new Intl.NumberFormat(E,j).format(o);return isNaN(o)?null:r.createElement(n,null,x)};M.propTypes={children:I.oneOfType([I.string,I.number]),locale:I.string,localeMatcher:I.oneOf(["lookup","best fit"]),currency:I.string,currencyDisplay:I.oneOf(["symbol","code","name"]),currencyStyle:I.oneOf(["decimal","currency","percent"]),useGrouping:I.bool,minimumIntegerDigits:I.number,minimumFractionDigits:I.number,maximumFractionDigits:I.number,minimumSignificantDigits:I.number,maximumSignificantDigits:I.number,notation:I.oneOf(["standard","scientific","engineering","compact"]),compactDisplay:I.oneOf(["short","long"])},M.defaultProps={as:"div"};var A=function(e){var n=C();return r.createElement(M,c({currency:e.currency||n&&n.config.options.currency||N.options.currency},e))};A.propTypes=u({},M.PropTypes),A.defaultProps={currencyStyle:"currency"};export{M as Number,A as Currency,C as useIntl,k as IntlProvider,N as defaultIntlConfig};
//# sourceMappingURL=index.mjs.map