diff --git a/format-min.js b/format-min.js index 5db0962..9e0f386 100644 --- a/format-min.js +++ b/format-min.js @@ -1,3 +1,2 @@ -exports.printf=function(){console.log(exports.format.apply(this,arguments))}; -exports.format=function(d){for(var f=1,i=[].slice.call(arguments),e=0,j=d.length,a="",b,g=!1,h,c=function(){return i[f++]},k=function(){for(var a="";d[e].match(/\d/);)a+=d[e++];return a.length>0?parseInt(a):null};e0?parseInt(a):null};g +// Copyright 2010 - 2013 Sami Samhuri // ISC license // -exports.printf = function(/* ... */) { - console.log(exports.format.apply(this, arguments)); -}; +;(function() { -exports.format = function(format) { + //// Export the API + var namespace; + + // CommonJS / Node module + if (typeof module !== 'undefined') { + namespace = module.exports = format; + } + + // Browsers and other environments + else { + // Get the global object. Works in ES3, ES5, and ES5 strict mode. + namespace = (function(){ return this || (1,eval)('this') }()); + } + + namespace.format = format; + namespace.vsprintf = vsprintf; + + if (typeof console !== 'undefined' && typeof console.log === 'function') { + namespace.printf = printf; + } + + function printf(/* ... */) { + console.log(format.apply(null, arguments)); + } + + function vsprintf(fmt, replacements) { + return format.apply(null, [fmt].concat(replacements)); + } + + function format(fmt) { var argIndex = 1 // skip initial format argument , args = [].slice.call(arguments) , i = 0 - , n = format.length + , n = fmt.length , result = '' , c , escaped = false @@ -22,61 +49,57 @@ exports.format = function(format) { , precision , nextArg = function() { return args[argIndex++]; } , slurpNumber = function() { - var digits = ''; - while (format[i].match(/\d/)) - digits += format[i++]; - return digits.length > 0 ? parseInt(digits) : null; - } + var digits = ''; + while (fmt[i].match(/\d/)) + digits += fmt[i++]; + return digits.length > 0 ? parseInt(digits) : null; + } ; for (; i < n; ++i) { - c = format[i]; - if (escaped) { - escaped = false; - precision = slurpNumber(); - switch (c) { - case 'b': // number in binary - result += parseInt(nextArg(), 10).toString(2); - break; - case 'c': // character - arg = nextArg(); - if (typeof arg === 'string' || arg instanceof String) - result += arg; - else - result += String.fromCharCode(parseInt(arg, 10)); - break; - case 'd': // number in decimal - result += parseInt(nextArg(), 10); - break; - case 'f': // floating point number - result += parseFloat(nextArg()).toFixed(precision || 6); - break; - case 'o': // number in octal - result += '0' + parseInt(nextArg(), 10).toString(8); - break; - case 's': // string - result += nextArg(); - break; - case 'x': // lowercase hexadecimal - result += '0x' + parseInt(nextArg(), 10).toString(16); - break; - case 'X': // uppercase hexadecimal - result += '0x' + parseInt(nextArg(), 10).toString(16).toUpperCase(); - break; - default: - result += c; - break; - } - } else if (c === '%') { - escaped = true; - } else { - result += c; + c = fmt[i]; + if (escaped) { + escaped = false; + precision = slurpNumber(); + switch (c) { + case 'b': // number in binary + result += parseInt(nextArg(), 10).toString(2); + break; + case 'c': // character + arg = nextArg(); + if (typeof arg === 'string' || arg instanceof String) + result += arg; + else + result += String.fromCharCode(parseInt(arg, 10)); + break; + case 'd': // number in decimal + result += parseInt(nextArg(), 10); + break; + case 'f': // floating point number + result += parseFloat(nextArg()).toFixed(precision || 6); + break; + case 'o': // number in octal + result += '0' + parseInt(nextArg(), 10).toString(8); + break; + case 's': // string + result += nextArg(); + break; + case 'x': // lowercase hexadecimal + result += '0x' + parseInt(nextArg(), 10).toString(16); + break; + case 'X': // uppercase hexadecimal + result += '0x' + parseInt(nextArg(), 10).toString(16).toUpperCase(); + break; + default: + result += c; + break; } + } else if (c === '%') { + escaped = true; + } else { + result += c; + } } return result; -}; + } -exports.vsprintf = function(format, replacements) { - return exports.format.apply(this, [format].concat(replacements)); -}; - -exports.sprintf = exports.format; +}());