123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- (function(root, undefined) {
-
-
- var lib = {};
-
- lib.version = '0.4.2';
-
-
-
- lib.settings = {
- currency: {
- symbol : "$",
- format : "%s%v",
- decimal : ".",
- thousand : ",",
- precision : 2,
- grouping : 3
- },
- number: {
- precision : 0,
- grouping : 3,
- thousand : ",",
- decimal : "."
- }
- };
-
-
- var nativeMap = Array.prototype.map,
- nativeIsArray = Array.isArray,
- toString = Object.prototype.toString;
-
- function isString(obj) {
- return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
- }
-
- function isArray(obj) {
- return nativeIsArray ? nativeIsArray(obj) : toString.call(obj) === '[object Array]';
- }
-
- function isObject(obj) {
- return obj && toString.call(obj) === '[object Object]';
- }
-
- function defaults(object, defs) {
- var key;
- object = object || {};
- defs = defs || {};
-
- for (key in defs) {
- if (defs.hasOwnProperty(key)) {
-
- if (object[key] == null) object[key] = defs[key];
- }
- }
- return object;
- }
-
- function map(obj, iterator, context) {
- var results = [], i, j;
- if (!obj) return results;
-
- if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
-
- for (i = 0, j = obj.length; i < j; i++ ) {
- results[i] = iterator.call(context, obj[i], i, obj);
- }
- return results;
- }
-
- function checkPrecision(val, base) {
- val = Math.round(Math.abs(val));
- return isNaN(val)? base : val;
- }
-
- function checkCurrencyFormat(format) {
- var defaults = lib.settings.currency.format;
-
- if ( typeof format === "function" ) format = format();
-
- if ( isString( format ) && format.match("%v") ) {
-
- return {
- pos : format,
- neg : format.replace("-", "").replace("%v", "-%v"),
- zero : format
- };
-
- } else if ( !format || !format.pos || !format.pos.match("%v") ) {
-
- return ( !isString( defaults ) ) ? defaults : lib.settings.currency.format = {
- pos : defaults,
- neg : defaults.replace("%v", "-%v"),
- zero : defaults
- };
- }
-
- return format;
- }
-
-
- var unformat = lib.unformat = lib.parse = function(value, decimal) {
-
- if (isArray(value)) {
- return map(value, function(val) {
- return unformat(val, decimal);
- });
- }
-
- value = value || 0;
-
- if (typeof value === "number") return value;
-
- decimal = decimal || lib.settings.number.decimal;
-
- var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]),
- unformatted = parseFloat(
- ("" + value)
- .replace(/\((?=\d+)(.*)\)/, "-$1")
- .replace(regex, '')
- .replace(decimal, '.')
- );
-
- return !isNaN(unformatted) ? unformatted : 0;
- };
-
- var toFixed = lib.toFixed = function(value, precision) {
- precision = checkPrecision(precision, lib.settings.number.precision);
- var exponentialForm = Number(lib.unformat(value) + 'e' + precision);
- var rounded = Math.round(exponentialForm);
- var finalResult = Number(rounded + 'e-' + precision).toFixed(precision);
- return finalResult;
- };
-
- var formatNumber = lib.formatNumber = lib.format = function(number, precision, thousand, decimal) {
-
- if (isArray(number)) {
- return map(number, function(val) {
- return formatNumber(val, precision, thousand, decimal);
- });
- }
-
- number = unformat(number);
-
- var opts = defaults(
- (isObject(precision) ? precision : {
- precision : precision,
- thousand : thousand,
- decimal : decimal
- }),
- lib.settings.number
- ),
-
- usePrecision = checkPrecision(opts.precision),
-
- negative = number < 0 ? "-" : "",
- base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + "",
- mod = base.length > 3 ? base.length % 3 : 0;
-
- return negative + (mod ? base.substr(0, mod) + opts.thousand : "") + base.substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + opts.thousand) + (usePrecision ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] : "");
- };
-
- var formatMoney = lib.formatMoney = function(number, symbol, precision, thousand, decimal, format) {
-
- if (isArray(number)) {
- return map(number, function(val){
- return formatMoney(val, symbol, precision, thousand, decimal, format);
- });
- }
-
- number = unformat(number);
-
- var opts = defaults(
- (isObject(symbol) ? symbol : {
- symbol : symbol,
- precision : precision,
- thousand : thousand,
- decimal : decimal,
- format : format
- }),
- lib.settings.currency
- ),
-
- formats = checkCurrencyFormat(opts.format),
-
- useFormat = number > 0 ? formats.pos : number < 0 ? formats.neg : formats.zero;
-
- return useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(number), checkPrecision(opts.precision), opts.thousand, opts.decimal));
- };
-
- lib.formatColumn = function(list, symbol, precision, thousand, decimal, format) {
- if (!list || !isArray(list)) return [];
-
- var opts = defaults(
- (isObject(symbol) ? symbol : {
- symbol : symbol,
- precision : precision,
- thousand : thousand,
- decimal : decimal,
- format : format
- }),
- lib.settings.currency
- ),
-
- formats = checkCurrencyFormat(opts.format),
-
- padAfterSymbol = formats.pos.indexOf("%s") < formats.pos.indexOf("%v") ? true : false,
-
- maxLength = 0,
-
- formatted = map(list, function(val, i) {
- if (isArray(val)) {
-
- return lib.formatColumn(val, opts);
- } else {
-
- val = unformat(val);
-
- var useFormat = val > 0 ? formats.pos : val < 0 ? formats.neg : formats.zero,
-
- fVal = useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(val), checkPrecision(opts.precision), opts.thousand, opts.decimal));
- if (fVal.length > maxLength) maxLength = fVal.length;
- return fVal;
- }
- });
-
- return map(formatted, function(val, i) {
-
- if (isString(val) && val.length < maxLength) {
-
- return padAfterSymbol ? val.replace(opts.symbol, opts.symbol+(new Array(maxLength - val.length + 1).join(" "))) : (new Array(maxLength - val.length + 1).join(" ")) + val;
- }
- return val;
- });
- };
-
-
-
- if (typeof exports !== 'undefined') {
- if (typeof module !== 'undefined' && module.exports) {
- exports = module.exports = lib;
- }
- exports.accounting = lib;
- } else if (typeof define === 'function' && define.amd) {
-
- define([], function() {
- return lib;
- });
- } else {
-
-
-
- lib.noConflict = (function(oldAccounting) {
- return function() {
-
- root.accounting = oldAccounting;
-
- lib.noConflict = undefined;
-
- return lib;
- };
- })(root.accounting);
-
- root['accounting'] = lib;
- }
-
- }(this));
|