1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 String.prototype.format = function string_format() {
6 // there are two modes of operation... unnamed indices are read in order;
7 // named indices using %(name)s. The two styles cannot be mixed.
8 // Unnamed indices can be passed as either a single argument to this function,
9 // multiple arguments to this function, or as a single array argument
13 if (arguments.length > 1) {
19 function r(s, key, type) {
26 throw Error("Cannot mix named and positional indices in string formatting.");
28 if (curindex == 0 && (!(d instanceof Object) || !(0 in d))) {
31 else if (!(curindex in d))
32 throw Error("Insufficient number of items in format, requesting item %i".format(curindex));
40 key = key.slice(1, -1);
42 throw Error("Cannot mix named and positional indices in string formatting.");
46 throw Error("Key '%s' not present during string substitution.".format(key));
61 throw Error("Unexpected format character '%s'.".format(type));
64 return this.replace(/%(\([^)]+\))?(.)/g, r);