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