summaryrefslogtreecommitdiff
path: root/config/string-format.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commitad18d877ddd2a44d98fa12ccd3dbbcf4d0ac4299 (patch)
tree10027f336435511475e392454359edea8e25895d /config/string-format.js
parent15477ed9af4859dacb069040b5d4de600803d3bc (diff)
downloadaura-central-ad18d877ddd2a44d98fa12ccd3dbbcf4d0ac4299.tar.gz
Add m-esr52 at 52.6.0
Diffstat (limited to 'config/string-format.js')
-rw-r--r--config/string-format.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/config/string-format.js b/config/string-format.js
new file mode 100644
index 000000000..7319eb859
--- /dev/null
+++ b/config/string-format.js
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+String.prototype.format = function string_format() {
+ // 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;
+ let d;
+
+ if (arguments.length > 1) {
+ d = arguments;
+ }
+ else
+ d = arguments[0];
+
+ function r(s, key, type) {
+ if (type == '%')
+ return '%';
+
+ 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":
+ if (v === undefined)
+ return "<undefined>";
+ return v.toString();
+ case "r":
+ return uneval(v);
+ case "i":
+ return parseInt(v);
+ case "f":
+ return Number(v);
+ default:
+ throw Error("Unexpected format character '%s'.".format(type));
+ }
+ }
+ return this.replace(/%(\([^)]+\))?(.)/g, r);
+};