From 84050d67a3702e9ec3784ff8f2315ce3fe54e342 Mon Sep 17 00:00:00 2001 From: rolux Date: Fri, 25 May 2012 13:32:57 +0200 Subject: [PATCH] rename vars --- source/Ox/js/Format.js | 141 +++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 70 deletions(-) diff --git a/source/Ox/js/Format.js b/source/Ox/js/Format.js index e9326848..3e2345d0 100644 --- a/source/Ox/js/Format.js +++ b/source/Ox/js/Format.js @@ -468,13 +468,13 @@ Ox.formatDegrees = function(degrees, mode) { var days = 0, seconds = Math.round(Math.abs(degrees) * 3600), sign = degrees < 0 ? '-' : '', - split = Ox.formatDuration(seconds).split(':'); - if (split.length == 4) { - days = parseInt(split.shift(), 10); + array = Ox.formatDuration(seconds).split(':'); + if (array.length == 4) { + days = parseInt(array.shift(), 10); } - split[0] = days * 24 + parseInt(split[0], 10); + array[0] = days * 24 + parseInt(array[0], 10); return (!mode ? sign : '') - + split[0] + '\u00B0' + split[1] + "'" + split[2] + '"' + + array[0] + '\u00B0' + array[1] + "'" + array[2] + '"' + ( mode == 'lat' ? (degrees < 0 ? 'S' : 'N') : mode == 'lng' ? (degrees < 0 ? 'W' : 'E') @@ -520,45 +520,45 @@ Ox.formatDuration Formats a duration as a string > Ox.formatDuration(0, 'long') '' @*/ -Ox.formatDuration = function(/*sec, dec, format*/) { - var format = Ox.isString(arguments[arguments.length - 1]) - ? arguments[arguments.length - 1] : '', - dec = Ox.isNumber(arguments[1]) ? arguments[1] : 0, - sec = Ox.round(arguments[0], dec), - val = [ - Math.floor(sec / 31536000), - Math.floor(sec % 31536000 / 86400), - Math.floor(sec % 86400 / 3600), - Math.floor(sec % 3600 / 60), - Ox.formatNumber(sec % 60, dec) +Ox.formatDuration = function(seconds/*, decimals, format*/) { + var last = Ox.last(arguments), + format = last == 'short' || last == 'long' ? last : 'none', + decimals = Ox.isNumber(arguments[1]) ? arguments[1] : 0, + seconds = Ox.round(seconds, decimals), + values = [ + Math.floor(seconds / 31536000), + Math.floor(seconds % 31536000 / 86400), + Math.floor(seconds % 86400 / 3600), + Math.floor(seconds % 3600 / 60), + Ox.formatNumber(seconds % 60, decimals) ], - str = !format ? [] - : format == 'short' ? ['y', 'd', 'h', 'm', 's'] - : ['year', 'day', 'hour', 'minute', 'second'], + string = format == 'short' ? ['y', 'd', 'h', 'm', 's'] + : format == 'long' ? ['year', 'day', 'hour', 'minute', 'second'] + : [], pad = [ - val[0].toString().length, - val[0] ? 3 : 1, + values[0].toString().length, + values[0] ? 3 : 1, 2, 2, - dec ? dec + 3 : 2 + decimals ? decimals + 3 : 2 ]; - while (!val[0] && val.length > (!format ? 3 : 1)) { - val.shift(); - str.shift(); + while (!values[0] && values.length > (format == 'none' ? 3 : 1)) { + values.shift(); + string.shift(); pad.shift(); } - return Ox.filter(Ox.map(val, function(v, i) { + return Ox.filter(Ox.map(values, function(value, index) { var ret; - if (!format) { - ret = Ox.pad(v, pad[i]); - } else if (Ox.isNumber(v) ? v : parseFloat(v)) { - ret = v + (format == 'long' ? ' ' : '') + str[i] - + (format == 'long' && v != 1 ? 's' : ''); + if (format == 'none') { + ret = Ox.pad(value, pad[index]); + } else if (Ox.isNumber(value) ? value : parseFloat(value)) { + ret = value + (format == 'long' ? ' ' : '') + string[index] + + (format == 'long' && value != 1 ? 's' : ''); } else { ret = ''; } return ret; - })).join(!format ? ':' : ' '); + })).join(format == 'none' ? ':' : ' '); }; /*@ @@ -570,20 +570,19 @@ Ox.formatNumber Formats a number with thousands separators "123,456,789.000" > Ox.formatNumber(-2000000 / 3, 3) "-666,666.667" - > Ox.formatNumber(666666.666, 0) + > Ox.formatNumber(666666.666) "666,667" @*/ -Ox.formatNumber = function(num, dec) { - var arr = [], - abs = Math.abs(num), - str = Ox.isUndefined(dec) ? abs.toString() : abs.toFixed(dec), - spl = str.split('.'); - while (spl[0]) { - arr.unshift(spl[0].slice(-3)); - spl[0] = spl[0].slice(0, -3); +Ox.formatNumber = function(number, decimals) { + var array = [], + abs = Math.abs(number), + split = abs.toFixed(decimals).split('.'); + while (split[0]) { + array.unshift(split[0].slice(-3)); + split[0] = split[0].slice(0, -3); } - spl[0] = arr.join(','); - return (num < 0 ? '-' : '') + spl.join('.'); + split[0] = array.join(','); + return (number < 0 ? '-' : '') + split.join('.'); }; /*@ @@ -603,21 +602,21 @@ Ox.formatOrdinal Formats a number as an ordinal > Ox.formatOrdinal(13) "13th" @*/ -Ox.formatOrdinal = function(num) { - var str = num.toString(), - len = str.length, - end = str[len - 1], - ten = len > 1 && str[len - 2] == '1'; +Ox.formatOrdinal = function(number) { + var string = number.toString(), + length = string.length, + end = string[length - 1], + ten = length > 1 && string[length - 2] == '1'; if (end == '1' && !ten) { - str += 'st'; + string += 'st'; } else if (end == '2' && !ten) { - str += 'nd'; + string += 'nd'; } else if (end == '3' && !ten) { - str += 'rd'; + string += 'rd'; } else { - str += 'th'; + string += 'th'; } - return str; + return string; }; /*@ @@ -625,8 +624,8 @@ Ox.formatPercent Formats the relation of two numbers as a percentage > Ox.formatPercent(1, 1000, 2) "0.10%" @*/ -Ox.formatPercent = function(num, total, dec) { - return Ox.formatNumber(num / total * 100, dec) + '%' +Ox.formatPercent = function(number, total, decimals) { + return Ox.formatNumber(number / total * 100, decimals) + '%' }; /*@ @@ -636,10 +635,9 @@ Ox.formatString Basic string formatting > Ox.formatString('{a}{b}', {a: 'foo', b: 'bar'}) 'foobar' @*/ - -Ox.formatString = function (str, obj) { - return str.replace(/\{([^}]+)\}/g, function(str, match) { - return obj[match]; +Ox.formatString = function (string, collection) { + return string.replace(/\{([^}]+)\}/g, function(string, match) { + return collection[match]; }); }; @@ -648,10 +646,12 @@ Ox.formatUnit Formats a number with a unit > Ox.formatUnit(0.333333, '%', 2, 100) "33.33%" @*/ -Ox.formatUnit = function(num, str, dec, factor) { - dec = Ox.isUndefined(dec) ? 3 : dec; +// FIXME: why factor?? +Ox.formatUnit = function(number, string, decimals, factor) { + decimals = Ox.isUndefined(decimals) ? 3 : decimals; factor = Ox.isUndefined(factor) ? 1 : factor; - return Ox.formatNumber(num * factor, dec) + (str == '%' ? '' : ' ') + str; + return Ox.formatNumber(number * factor, decimals) + + (string == '%' ? '' : ' ') + string; }; /*@ @@ -664,18 +664,19 @@ Ox.formatValue Formats a numerical value "1.15 GiB" @*/ // fixme: is this the best name? -Ox.formatValue = function(num, str, bin) { +Ox.formatValue = function(number, string, bin) { var base = bin ? 1024 : 1000, - len = Ox.PREFIXES.length, - val; - Ox.forEach(Ox.PREFIXES, function(chr, i) { - if (num < Math.pow(base, i + 1) || i == len - 1) { - val = Ox.formatNumber(num / Math.pow(base, i), i ? i - 1 : 0) - + ' ' + chr + (chr && bin ? 'i' : '') + str; + length = Ox.PREFIXES.length, + ret; + Ox.forEach(Ox.PREFIXES, function(prefix, index) { + if (number < Math.pow(base, index + 1) || index == length - 1) { + ret = Ox.formatNumber( + number / Math.pow(base, index), index ? index - 1 : 0 + ) + ' ' + prefix + (prefix && bin ? 'i' : '') + string; Ox.Break(); } }); - return val; + return ret; }; /*@