1 String.prototype.format = function string_format() {
2 // there are two modes of operation... unnamed indices are read in order;
3 // named indices using %(name)s. The two styles cannot be mixed.
4 // Unnamed indices can be passed as either a single argument to this function,
5 // multiple arguments to this function, or as a single array argument
9 if (arguments.length > 1) {
15 function r(s, key, type) {
22 throw Error("Cannot mix named and positional indices in string formatting.");
24 if (curindex == 0 && (!(d instanceof Object) || !(0 in d))) {
27 else if (!(curindex in d))
28 throw Error("Insufficient number of items in format, requesting item %i".format(curindex));
36 key = key.slice(1, -1);
38 throw Error("Cannot mix named and positional indices in string formatting.");
42 throw Error("Key '%s' not present during string substitution.".format(key));
57 throw Error("Unexpected format character '%s'.".format(type));
60 return this.replace(/%(\([^)]+\))?(.)/g, r);