mirror of
https://github.com/samsonjs/format.git
synced 2026-03-25 08:45:53 +00:00
work in browsers out of the box
This commit is contained in:
parent
4a0f6413bb
commit
9fe2d57865
2 changed files with 83 additions and 61 deletions
5
format-min.js
vendored
5
format-min.js
vendored
|
|
@ -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})();
|
||||
|
|
|
|||
53
format.js
53
format.js
|
|
@ -2,19 +2,46 @@
|
|||
// 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
|
||||
|
|
@ -23,13 +50,13 @@ exports.format = function(format) {
|
|||
, nextArg = function() { return args[argIndex++]; }
|
||||
, slurpNumber = function() {
|
||||
var digits = '';
|
||||
while (format[i].match(/\d/))
|
||||
digits += format[i++];
|
||||
while (fmt[i].match(/\d/))
|
||||
digits += fmt[i++];
|
||||
return digits.length > 0 ? parseInt(digits) : null;
|
||||
}
|
||||
;
|
||||
for (; i < n; ++i) {
|
||||
c = format[i];
|
||||
c = fmt[i];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
precision = slurpNumber();
|
||||
|
|
@ -73,10 +100,6 @@ exports.format = function(format) {
|
|||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
exports.vsprintf = function(format, replacements) {
|
||||
return exports.format.apply(this, [format].concat(replacements));
|
||||
};
|
||||
|
||||
exports.sprintf = exports.format;
|
||||
}());
|
||||
|
|
|
|||
Loading…
Reference in a new issue