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
) {
19 throw Error("Cannot mix named and positional indices in string formatting.");
21 if (curindex
== 0 && (!(d
instanceof Object
) || !(0 in d
))) {
24 else if (!(curindex
in d
))
25 throw Error("Insufficient number of items in format, requesting item %i".format(curindex
));
33 key
= key
.slice(1, -1);
35 throw Error("Cannot mix named and positional indices in string formatting.");
39 throw Error("Key '%s' not present during string substitution.".format(key
));
56 throw Error("Unexpected format character '%s'.".format(type
));
59 return this.replace(/%(\([^)]+\))?(.)/g, r
);