Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - Clojurescript: parseInt-Function with value for radix

I am new to Clojure and Clojurescript and here is my Problem. In my webpage I have an input form where the input is string. For further computation with these inputs the input should be integer without a leading zero. So for example "09" is not allowed.

The solution in JavaScript would be:

parseInt("09", 10) // 9

But right now, I am struggling to convert that in my Clojurescript-Code. In my Clojurescript code I already use the parseInt-Function of JavaScript. This works all fine, but I don't know how to add the parameter radix to this specific code:

  (defn order
  [round-data]
  (let [form-data (atom {:round/order 0})
        input-data (inputs/make-validated-input
                    {:class-name "order-input"
                     :spec :round/order
                     :placeholder "Bestellung"
                     :transform js/parseInt
                     :on-change #(swap! form-data assoc :round/order (.-value %2))
                     :value-fn #(:round/order @form-data)
                     :invalid-msg "Bitte eine ganze Zahl eingeben."})
        submit-fn (atom #(rf/dispatch [:game/round-commit @form-data]))]

At the :transform key I tried a lot of things like

:transform #(js/parseInt (.-value %2) 10).

Can anybody please help me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's :transform #(js/parseInt % 10)

  • %2 means the second argument for an anon-fn - so use %1 or just % for the first one
  • no need to get the value again in the transform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...