Browse files

CLJS-324: implement cljs.core/format as wrapper for goog.string.format

Also adds printf (copy & paste from Clojure).
  • Loading branch information...
1 parent 8365d2a commit 8f518760a3df8b351208e97bb70270856623bb0a @michalmarczyk michalmarczyk committed with David Nolen Jun 28, 2012
Showing with 14 additions and 0 deletions.
  1. +11 −0 src/cljs/cljs/core.cljs
  2. +3 −0 test/cljs/cljs/core_test.cljs
View
11 src/cljs/cljs/core.cljs
@@ -9,6 +9,7 @@
(ns cljs.core
(:require [goog.string :as gstring]
[goog.string.StringBuffer :as gstringbuf]
+ [goog.string.format]
[goog.object :as gobject]
[goog.array :as garray]))
@@ -1442,6 +1443,11 @@ reduces them without incurring seq initialization"
([s start] (.substring s start))
([s start end] (.substring s start end)))
+(defn format
+ "Formats a string using goog.string.format."
+ [fmt & args]
+ (apply gstring/format fmt args))
+
(defn symbol
"Returns a Symbol with the given namespace and name."
([name] (cond (symbol? name) name
@@ -6230,6 +6236,11 @@ reduces them without incurring seq initialization"
(pr-with-opts objs (pr-opts))
(newline (pr-opts)))
+(defn printf
+ "Prints formatted output, as per format"
+ [fmt & args]
+ (print (apply format fmt args)))
+
(extend-protocol IPrintable
boolean
(-pr-seq [bool opts] (list (str bool)))
View
3 test/cljs/cljs/core_test.cljs
@@ -196,6 +196,9 @@
(assert (= "hello:world" (str "hello" :world)))
(assert (= ":helloworld" (str :hello 'world)))
+ ;; format
+ (assert (= "01: 2.000000" (format "%02d: %.6f" 1 2)))
+
(assert (= {:a :b} (get {[1 2 3] {:a :b}, 4 5} [1 2 3])))
(assert (= :a (nth [:a :b :c :d] 0)))
(assert (= :a (nth [:a :b :c :d] 0.1)) )

0 comments on commit 8f51876

Please sign in to comment.