ClojureScript format string with goog.string.format does not replace

I am trying to format the color in hexadecimal format for use in HTML using ClojureScript in a browser.

Here is my "format" function.

(defn gen-format [& args] (apply gstring/format args) )

in the "string" namespace, where I need a goog.string library with:

(:require [goog.string :as gstring] [goog.string.format :as gformat])

But when I try to call it from javascript:

document.write(mypackage.strings.gen_format("#%x%x%x",0,0,0));

he just returns #%x%x%x

This is not a failure. But the goog format function does not seem to substitute values. Am I doing something wrong here?

+4
source share
1 answer

What does it do %x?

, s, f, d, i u

var formatRe = /%([0\-\ \+]*)(\d+)?(\.(\d+))?([%sfdiu])/g;

, :

mypackage.strings.gen_format("#%d%d%d", 0, 0, 0)

. , :

(defn hex-color [& args]
  (apply str "#" (map #(.toString % 16) args))

(defn hex-color [r g b]
  (str "#" (.toString r 16) (.toString g 16) (.toString b 16))
+5

Source: https://habr.com/ru/post/1547612/


All Articles