10 lines
3.3 KiB
JavaScript
10 lines
3.3 KiB
JavaScript
/*!
|
|
* accounting.js v0.3.2
|
|
* Copyright 2011, Joss Crowcroft
|
|
*
|
|
* Freely distributable under the MIT license.
|
|
* Portions of accounting.js are inspired or borrowed from underscore.js
|
|
*
|
|
* Full details and documentation:
|
|
* http://josscrowcroft.github.com/accounting.js/
|
|
*/(function(e,t){function o(e){return!!(e===""||e&&e.charCodeAt&&e.substr)}function u(e){return i?i(e):s.call(e)==="[object Array]"}function a(e){return e&&s.call(e)==="[object Object]"}function f(e,t){var n;e=e||{};t=t||{};for(n in t)t.hasOwnProperty(n)&&e[n]==null&&(e[n]=t[n]);return e}function l(e,t,n){var i=[],s,o;if(!e)return i;if(r&&e.map===r)return e.map(t,n);for(s=0,o=e.length;s<o;s++)i[s]=t.call(n,e[s],s,e);return i}function c(e,t){e=Math.round(Math.abs(e));return isNaN(e)?t:e}function h(e){var t=n.settings.currency.format;typeof e=="function"&&(e=e());return o(e)&&e.match("%v")?{pos:e,neg:e.replace("-","").replace("%v","-%v"),zero:e}:!e||!e.pos||!e.pos.match("%v")?o(t)?n.settings.currency.format={pos:t,neg:t.replace("%v","-%v"),zero:t}:t:e}var n={};n.version="0.3.2";n.settings={currency:{symbol:"$",format:"%s%v",decimal:".",thousand:",",precision:2,grouping:3},number:{precision:0,grouping:3,thousand:",",decimal:"."}};var r=Array.prototype.map,i=Array.isArray,s=Object.prototype.toString,p=n.unformat=n.parse=function(e,t){if(u(e))return l(e,function(e){return p(e,t)});e=e||0;if(typeof e=="number")return e;t=t||n.settings.number.decimal;var r=new RegExp("[^0-9-"+t+"]",["g"]),i=parseFloat((""+e).replace(/\((.*)\)/,"-$1").replace(r,"").replace(t,"."));return isNaN(i)?0:i},d=n.toFixed=function(e,t){t=c(t,n.settings.number.precision);var r=Math.pow(10,t);return(Math.round(n.unformat(e)*r)/r).toFixed(t)},v=n.formatNumber=function(e,t,r,i){if(u(e))return l(e,function(e){return v(e,t,r,i)});e=p(e);var s=f(a(t)?t:{precision:t,thousand:r,decimal:i},n.settings.number),o=c(s.precision),h=e<0?"-":"",m=parseInt(d(Math.abs(e||0),o),10)+"",g=m.length>3?m.length%3:0;return h+(g?m.substr(0,g)+s.thousand:"")+m.substr(g).replace(/(\d{3})(?=\d)/g,"$1"+s.thousand)+(o?s.decimal+d(Math.abs(e),o).split(".")[1]:"")},m=n.formatMoney=function(e,t,r,i,s,o){if(u(e))return l(e,function(e){return m(e,t,r,i,s,o)});e=p(e);var d=f(a(t)?t:{symbol:t,precision:r,thousand:i,decimal:s,format:o},n.settings.currency),g=h(d.format),y=e>0?g.pos:e<0?g.neg:g.zero;return y.replace("%s",d.symbol).replace("%v",v(Math.abs(e),c(d.precision),d.thousand,d.decimal))};n.formatColumn=function(e,t,r,i,s,d){if(!e)return[];var m=f(a(t)?t:{symbol:t,precision:r,thousand:i,decimal:s,format:d},n.settings.currency),g=h(m.format),y=g.pos.indexOf("%s")<g.pos.indexOf("%v")?!0:!1,b=0,w=l(e,function(e,t){if(u(e))return n.formatColumn(e,m);e=p(e);var r=e>0?g.pos:e<0?g.neg:g.zero,i=r.replace("%s",m.symbol).replace("%v",v(Math.abs(e),c(m.precision),m.thousand,m.decimal));i.length>b&&(b=i.length);return i});return l(w,function(e,t){return o(e)&&e.length<b?y?e.replace(m.symbol,m.symbol+(new Array(b-e.length+1)).join(" ")):(new Array(b-e.length+1)).join(" ")+e:e})};if(typeof exports!="undefined"){typeof module!="undefined"&&module.exports&&(exports=module.exports=n);exports.accounting=n}else if(typeof define=="function"&&define.amd)define([],function(){return n});else{n.noConflict=function(r){return function(){e.accounting=r;n.noConflict=t;return n}}(e.accounting);e.accounting=n}})(this); |