-
Notifications
You must be signed in to change notification settings - Fork 6
/
coercion.cljc
34 lines (29 loc) · 972 Bytes
/
coercion.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(ns axel-f.functions.coercion
#?(:clj
(:require [clojure.edn :as edn])))
(defn excel-number [maybe-number]
(cond
(number? maybe-number)
maybe-number
(string? maybe-number)
(try
(let [n (#?(:clj edn/read-string
:cljs js/parseFloat) maybe-number)]
(if (and (number? n) #?(:cljs (not (js/isNaN n))))
n
(throw (ex-info (str "Fail to coerce `" maybe-number "` to number.")
{:type :argument-type}))))
(catch #?(:clj Throwable
:cljs js/Error) e
(throw (ex-info (str "Fail to coerce `" maybe-number "` to number.")
{:type :argument-type}))))
(boolean? maybe-number)
(if maybe-number 1 0)
:otherwise
(throw (ex-info (str "Fail to coerce `" maybe-number "` to number.")
{:type :argument-type}))))
(defn excel-str [item]
(case item
true "TRUE"
false "FALSE"
(str item)))