Permalink
Browse files

work in browsers out of the box

  • Loading branch information...
1 parent 4a0f641 commit 9fe2d578650c608d44c2de6abd67d69ab1757724 @samsonjs committed Mar 8, 2013
Showing with 83 additions and 61 deletions.
  1. +2 −3 format-min.js
  2. +81 −58 format.js
View
@@ -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};e<j;++e)if(b=d[e],g)switch(g=!1,h=k(),b){case "b":a+=parseInt(c(),10).toString(2);break;case "c":b=c();a+=typeof b==="string"||b instanceof String?b:String.fromCharCode(parseInt(b,10));break;case "d":a+=parseInt(c(),10);break;case "f":a+=parseFloat(c()).toFixed(h||6);break;case "o":a+="0"+
-parseInt(c(),10).toString(8);break;case "s":a+=c();break;case "x":a+="0x"+parseInt(c(),10).toString(16);break;case "X":a+="0x"+parseInt(c(),10).toString(16).toUpperCase();break;default:a+=b}else b==="%"?g=!0:a+=b;return a};exports.vsprintf=function(d,f){return exports.format.apply(this,[d].concat(f))};exports.sprintf=exports.format;
+(function(){function f(){console.log(e.apply(null,arguments))}function e(h){for(var e=1,d=[].slice.call(arguments),g=0,f=h.length,a="",b,i=!1,j,c=function(){return d[e++]},k=function(){for(var a="";h[g].match(/\d/);)a+=h[g++];return a.length>0?parseInt(a):null};g<f;++g)if(b=h[g],i)switch(i=!1,j=k(),b){case "b":a+=parseInt(c(),10).toString(2);break;case "c":b=c();a+=typeof b==="string"||b instanceof String?b:String.fromCharCode(parseInt(b,10));break;case "d":a+=parseInt(c(),10);break;case "f":a+=parseFloat(c()).toFixed(j||
+6);break;case "o":a+="0"+parseInt(c(),10).toString(8);break;case "s":a+=c();break;case "x":a+="0x"+parseInt(c(),10).toString(16);break;case "X":a+="0x"+parseInt(c(),10).toString(16).toUpperCase();break;default:a+=b}else b==="%"?i=!0:a+=b;return a}var d;d=typeof module!=="undefined"?module.exports=e:function(){return this||(0,eval)("this")}();d.format=e;d.vsprintf=function(d,f){return e.apply(null,[d].concat(f))};if(typeof console!=="undefined"&&typeof console.log==="function")d.printf=f})();
View
@@ -2,81 +2,104 @@
// format, printf-like string formatting for JavaScript
// github.com/samsonjs/format
//
-// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
+// Copyright 2010 - 2013 Sami Samhuri <sami@samhuri.net>
// 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
, arg
, 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;
+}());

0 comments on commit 9fe2d57

Please sign in to comment.