ray-template/js/currency.js@2.0.4-9OAR_aOO.js

8 lines
2.3 KiB
JavaScript

/*!
* currency.js - v2.0.4
* http://scurker.github.io/currency.js
*
* Copyright (c) 2021 Jason Wilson
* Released under MIT license
*/var m={symbol:"$",separator:",",decimal:".",errorOnInvalid:!1,precision:2,pattern:"!#",negativePattern:"-!#",format:y,fromCents:!1},h=function(t){return Math.round(t)},d=function(t){return Math.pow(10,t)},V=function(t,e){return h(t/e)*e},b=/(\d)(?=(\d{3})+\b)/g,_=/(\d)(?=(\d\d)+\d\b)/g;function a(i,t){var e=this;if(!(e instanceof a))return new a(i,t);var r=Object.assign({},m,t),n=d(r.precision),s=f(i,r);e.intValue=s,e.value=s/n,r.increment=r.increment||1/n,r.useVedic?r.groups=_:r.groups=b,this.s=r,this.p=n}function f(i,t){var e=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!0,r=0,n=t.decimal,s=t.errorOnInvalid,c=t.precision,l=t.fromCents,u=d(c),o=typeof i=="number",p=i instanceof a;if(p&&l)return i.intValue;if(o||p)r=p?i.value:i;else if(typeof i=="string"){var g=new RegExp("[^-\\d"+n+"]","g"),v=new RegExp("\\"+n,"g");r=i.replace(/\((.*)\)/,"-$1").replace(g,"").replace(v,"."),r=r||0}else{if(s)throw Error("Invalid Input");r=0}return l||(r*=u,r=r.toFixed(4)),e?h(r):r}function y(i,t){var e=t.pattern,r=t.negativePattern,n=t.symbol,s=t.separator,c=t.decimal,l=t.groups,u=(""+i).replace(/^-/,"").split("."),o=u[0],p=u[1];return(i.value>=0?e:r).replace("!",n).replace("#",o.replace(l,"$1"+s)+(p?c+p:""))}a.prototype={add:function(t){var e=this.intValue,r=this.s,n=this.p;return a((e+=f(t,r))/(r.fromCents?1:n),r)},subtract:function(t){var e=this.intValue,r=this.s,n=this.p;return a((e-=f(t,r))/(r.fromCents?1:n),r)},multiply:function(t){var e=this.intValue,r=this.s;return a((e*=t)/(r.fromCents?1:d(r.precision)),r)},divide:function(t){var e=this.intValue,r=this.s;return a(e/=f(t,r,!1),r)},distribute:function(t){for(var e=this.intValue,r=this.p,n=this.s,s=[],c=Math[e>=0?"floor":"ceil"](e/t),l=Math.abs(e-c*t),u=n.fromCents?1:r;t!==0;t--){var o=a(c/u,n);l-- >0&&(o=o[e>=0?"add":"subtract"](1/u)),s.push(o)}return s},dollars:function(){return~~this.value},cents:function(){var t=this.intValue,e=this.p;return~~(t%e)},format:function(t){var e=this.s;return typeof t=="function"?t(this,e):e.format(this,Object.assign({},e,t))},toString:function(){var t=this.intValue,e=this.p,r=this.s;return V(t/e,r.increment).toFixed(r.precision)},toJSON:function(){return this.value}};export{a as c};