String Formatting in JavaScript
Tuesday, July 15th, 2008I am a relative newcomer to python, and have been blown away by the flexibility of some operations in Python. The string-formatting operator, %, is really wonderful and flexible. This is my attempt at implementing something similar in JavaScript.
Obviously, you can’t create a new operator in JavaScript, and in addition you can’t use % as a JavaScript identifier. So I went for the next-best thing:
String.prototype.format = function string_format(d) {
// there are two modes of operation... unnamed indices are read in order;
// named indices using %(name)s. The two styles cannot be mixed.
// Unnamed indices can be passed as either a single argument to this function,
// multiple arguments to this function, or as a single array argument
let curindex = 0;
if (arguments.length > 1)
d = arguments;
function r(s, key, type) {
let v;
if (key == "") {
if (curindex == -1)
throw Error("Cannot mix named and positional indices in string formatting.");
if (curindex == 0 && (!(d instanceof Object) || !(0 in d)))
v = d;
else if (!(curindex in d))
throw Error("Insufficient number of items in format, requesting item %i".format(curindex));
else
v = d[curindex];
++curindex;
}
else {
key = key.slice(1, -1);
if (curindex > 0)
throw Error("Cannot mix named and positional indices in string formatting.");
curindex = -1;
if (!(key in d))
throw Error("Key '%s' not present during string substitution.".format(key));
v = d[key];
}
switch (type) {
case "s":
return v.toString();
case "r":
return v.toSource();
case "i":
return parseInt(v);
case "f":
return Number(v);
case "%":
return "%";
default:
throw Error("Unexpected format character '%s'.".format(type));
}
}
return this.replace(/%(\([^)]+\))?(.)/g, r);
};
String.prototype.ø = String.prototype.format;
If you are at all familiar with the python string-formatting operator, this should be very similar:
"%s %s".ø("angry", "monkeys"); == "angry monkeys";
"%(key)s: %(value)s".ø({key: 'bananas', value: 'tasty'}) == "bananas: tasty";
"%i - %i".ø([1, 3]) == "1 - 3";
"%r".ø(['α', 'ω'] == '["α", "ω"]';
I know that there are many sprintf-like libraries out there for JavaScript: I just happen to like mine best. Caution: this code requires JavaScript 1.7… if you replace some “let”s with “var”s it may work in older browsers, but I haven’t tested it.
Being able to pass a function as a replacement in String.replace is a very powerful feature!