Created
November 9, 2010 14:55
-
-
Save omarkj/669186 to your computer and use it in GitHub Desktop.
Underscore.js mixin that provides a 'printf'-like functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.mixin({ | |
format: function(string) { | |
var i = 1, a, f = string, o = [], m, p, c, x, s = ''; | |
while (f) { | |
if (m = /^[^\x25]+/.exec(f)) { | |
o.push(m[0]); | |
} | |
else if (m = /^\x25{2}/.exec(f)) { | |
o.push('%'); | |
} | |
else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) { | |
if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) { | |
throw('Too few arguments.'); | |
}; | |
if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) { | |
throw('Expecting number but found ' + typeof(a)); | |
}; | |
switch (m[7]) { | |
case 'b': a = a.toString(2); break; | |
case 'c': a = String.fromCharCode(a); break; | |
case 'd': a = parseInt(a); break; | |
case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break; | |
case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break; | |
case 'o': a = a.toString(8); break; | |
case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break; | |
case 'u': a = Math.abs(a); break; | |
case 'x': a = a.toString(16); break; | |
case 'X': a = a.toString(16).toUpperCase(); break; | |
}; | |
a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a); | |
c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' '; | |
x = m[5] - String(a).length - s.length; | |
p = m[5] ? string.str_repeat(c, x) : ''; | |
o.push(s + (m[4] ? a + p : p + a)); | |
} | |
else { throw("Error"); }; | |
f = f.substring(m[0].length); | |
}; | |
return o.join(''); | |
function str_repeat(i, m) { | |
for (var o = []; m > 0; o[--m] = i); | |
return o.join(''); | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment