UNPKG

@resk/core

Version:

An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla

4 lines 4.12 kB
'use strict';var d=require('../utils/defaultStr'),z=require('../utils/isNonNullString'),p=require('./session'),P=require('./currencies'),utils=require('./utils');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var d__default=/*#__PURE__*/_interopDefault(d);var z__default=/*#__PURE__*/_interopDefault(z);var p__default=/*#__PURE__*/_interopDefault(p);var P__default=/*#__PURE__*/_interopDefault(P);var D=Object.defineProperty,$=Object.defineProperties;var j=Object.getOwnPropertyDescriptors;var I=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var S=(r,e,t)=>e in r?D(r,e,{enumerable:true,configurable:true,writable:true,value:t}):r[e]=t,N=(r,e)=>{for(var t in e||(e={}))k.call(e,t)&&S(r,t,e[t]);if(I)for(var t of I(e))v.call(e,t)&&S(r,t,e[t]);return r},F=(r,e)=>$(r,j(e));const A=r=>r&&typeof r=="object";function b(r){const e=Object.assign({},p__default.default.getCurrency());if(r&&A(r))for(let t in r)r.hasOwnProperty(t)&&r[t]!==void 0&&(e[t]=r[t]);if(z__default.default(e.format)){const t=V(e.format);t.format&&(e.format=t.format),typeof t.decimalDigits=="number"&&(e.decimalDigits=t.decimalDigits);}return e}function h(r,e){return r=typeof r=="number"?r:0,e=typeof e=="number"?e:0,r=Math.round(Math.abs(r)),isNaN(r)?e:r}function O(r){const e=p__default.default.getCurrency(),t=(e==null?void 0:e.format).toLowerCase();return typeof r=="string"&&(r=r.toLowerCase()),(typeof r!="string"||!r.match("%v"))&&(r=t),{pos:r,neg:r.replace("-","").replace("%v","-%v"),zero:r}}const l=(r,e)=>{const t=p__default.default.getCurrency();if(r=r||0,typeof r=="number")return r;e=e||t.decimalSeparator;const n=new RegExp("[^0-9-"+e+"]","g"),s=parseFloat((""+r).replace(/\((?=\d+)(.*)\)/,"-$1").replace(n,"").replace(e,"."));return isNaN(s)?0:s},C=(r,e)=>{const t=p__default.default.getCurrency();e=h(e,t.decimalDigits);const n=+(l(r)+"e"+e);return (+(Math.round(n)+"e-"+e)).toFixed(e)},x=(r,e,t,n)=>{var m;r=l(r);const s=utils.isValidCurrency(e)?e:{};typeof e=="number"&&(s.decimalDigits=e),typeof s.decimalDigits!="number"&&(s.decimalDigits=(m=String(r).split(".")[1])==null?void 0:m.length),t!==void 0&&(s.thousandSeparator=t),n!==void 0&&(s.decimalSeparator=n);const o=b(s),i=h(o.decimalDigits),a=r<0?"-":"",c=parseInt(C(Math.abs(r||0),i),10)+"",g=c.length>3?c.length%3:0;let u="";if(i){const f=String(parseFloat(C(Math.abs(r),i))||0);f.includes(".")&&(u=d__default.default(f.split(".")[1]).trim());}return a+(g?c.substring(0,g)+o.thousandSeparator:"")+c.substring(g).replace(/(\d{3})(?=\d)/g,"$1"+o.thousandSeparator)+(i&&u?o.decimalSeparator+u:"")},M=(r,e,t,n,s,o)=>{r=l(r);const i=utils.isValidCurrency(e)?e:{};e!==void 0&&typeof e=="string"&&(i.symbol=e),t!==void 0&&(i.decimalDigits=t),n!==void 0&&(i.thousandSeparator=n),s!==void 0&&(i.decimalSeparator=s),o!==void 0&&(i.format=o);const a=b(i),c=O(a.format),g=d__default.default(r>0?c.pos:r<0?c.neg:c.zero),u=d__default.default(a.symbol),m=g.replace(u&&"%s",u),f=x(Math.abs(r),h(a.decimalDigits),a.thousandSeparator,a.decimalSeparator),w=m.replace("%v",f);return F(N({},a),{formattedValue:m,formattedNumber:f,symbol:a.symbol,usedFormat:g,result:w})},R=(r,e,t,n,s,o)=>M(r,e,t,n,s,o).result,V=r=>{r=d__default.default(r).trim();const e={};if(r){const t=/(\.)(\#{0,9}\s*$)/,n=r.match(t);Array.isArray(n)&&n.length===3&&(e.decimalDigits=d__default.default(n[2]).trim().length),r=r.replace(t,"");}return e.format=r,e},L=`Display format for numerical values: a character string consisting of the letters %v and %s, where %v represents the value of the amount and %s represents the currency: example %s%v => $10 and %v %s => $10. To define decimals, use the [.][#{0,n}] pattern, e.g. .### for display with 3 decimals and . to exclude decimals from the display. for example, the format %v %s .## returns: 12.35 $ for the value 12.357777 converted into dollard. `;var Q={parse:l,session:p__default.default,formatMoney:R,currencies:P__default.default,isValidCurrency:utils.isValidCurrency,formatNumber:x,formatMoneyAsObject:M,unformat:l,toFixed:C,formatDescription:L,prepareOptions:b};module.exports=Q;