[sushi] util: add format.js from gnome-shell for a printf-style string format



commit 3ccf70cf7598f16c3172b884fa1ff41871d84718
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue May 10 13:27:56 2011 -0400

    util: add format.js from gnome-shell for a printf-style string format

 src/Makefile-js.am    |    3 +-
 src/js/ui/main.js     |    2 +
 src/js/util/format.js |   60 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 1 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 803ea97..784062a 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -18,7 +18,8 @@ dist_jsviewers_DATA = \
 
 jsutildir = $(pkgdatadir)/js/util
 dist_jsutil_DATA = \
-    js/util/constants.js
+    js/util/constants.js \
+    js/util/format.js
 
 jsutil_built_sources = \
     $(srcdir)/js/util/features.js \
diff --git a/src/js/ui/main.js b/src/js/ui/main.js
index e230757..9944692 100644
--- a/src/js/ui/main.js
+++ b/src/js/ui/main.js
@@ -5,11 +5,13 @@ const Gtk = imports.gi.Gtk;
 
 const Application = imports.ui.application;
 const Path = imports.util.path;
+const Format = imports.util.format;
 const Utils = imports.ui.utils;
 const Tweener = imports.ui.tweener;
 
 function run() {
     Gettext.bindtextdomain("sushi", Path.LOCALE_DIR);
+    String.prototype.format = Format.format;
 
     GLib.set_application_name("Sushi");
 
diff --git a/src/js/util/format.js b/src/js/util/format.js
new file mode 100644
index 0000000..dcebf7e
--- /dev/null
+++ b/src/js/util/format.js
@@ -0,0 +1,60 @@
+/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
+
+/*
+ * This function is intended to extend the String object and provide
+ * an String.format API for string formatting.
+ * It has to be set up using String.prototype.format = Format.format;
+ * Usage:
+ * "somestring %s %d".format('hello', 5);
+ * It supports %s, %d, %x and %f, for %f it also support precisions like
+ * "%.2f".format(1.526). All specifiers can be prefixed with a minimum
+ * field width, e.g. "%5s".format("foo"). Unless the width is prefixed
+ * with '0', the formatted string will be padded with spaces.
+ */
+
+function format() {
+    let str = this;
+    let i = 0;
+    let args = arguments;
+
+    return str.replace(/%([0-9]+)?(?:\.([0-9]+))?(.)/g, function (str, widthGroup, precisionGroup, genericGroup) {
+
+                    if (precisionGroup != '' && genericGroup != 'f')
+                        throw new Error("Precision can only be specified for 'f'");
+
+                    let fillChar = (widthGroup[0] == '0') ? '0' : ' ';
+                    let width = parseInt(widthGroup, 10) || 0;
+
+                    function fillWidth(s, c, w) {
+                        let fill = '';
+                        for (let i = 0; i < w; i++)
+                            fill += c;
+                        return fill.substr(s.length) + s;
+                    }
+
+                    let s = '';
+                    switch (genericGroup) {
+                        case '%':
+                            return '%';
+                            break;
+                        case 's':
+                            s = args[i++].toString();
+                            break;
+                        case 'd':
+                            s = parseInt(args[i++]).toString();
+                            break;
+                        case 'x':
+                            s = parseInt(args[i++]).toString(16);
+                            break;
+                        case 'f':
+                            if (precisionGroup == '')
+                                s = parseFloat(args[i++]).toString();
+                            else
+                                s = parseFloat(args[i++]).toFixed(parseInt(precisionGroup));
+                            break;
+                        default:
+                            throw new Error('Unsupported conversion character %' + genericGroup);
+                    }
+                    return fillWidth(s, fillChar, width);
+                });
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]