Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 2806ecd

Browse files
authored
Fix different behaviour in coercion to string for JVM and JS runt… (#97)
1 parent 4b9e698 commit 2806ecd

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

src/axel_f/excel/coerce.cljc

+18-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
(defn excel-type [x]
55
(cond
6+
#?@(:clj [(ratio? x) :ratio])
67
(number? x) :number
78
(string? x) :string
89
(boolean? x) :boolean
@@ -17,6 +18,9 @@
1718

1819
(defmethod excel-number :number [n] n)
1920

21+
#?(:clj
22+
(defmethod excel-number :ratio [r] r))
23+
2024
(defmethod excel-number :string [s]
2125
(try
2226
(let [n (when (not-empty s)
@@ -36,11 +40,20 @@
3640

3741
(defmethod excel-number :null [_] 0)
3842

39-
(defn excel-str [item]
40-
(case item
41-
true "TRUE"
42-
false "FALSE"
43-
(str item)))
43+
(defmulti excel-str excel-type)
44+
45+
(defmethod excel-str :default [x]
46+
(str x))
47+
48+
(defmethod excel-str :boolean [b]
49+
(if b "TRUE" "FALSE"))
50+
51+
(defmethod excel-str :null [_]
52+
"NULL")
53+
54+
#?(:clj
55+
(defmethod excel-str :ratio [r]
56+
(str (double r))))
4457

4558
(defn to-string*
4659
"Tries to coerce given value to a string type. Returns null for empty value."

src/axel_f/excel/jws.cljc

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727

2828
(def env
2929
{"JWS" {"EXTRACT" jws-extract
30-
"VERIFY" jws-verify}})
30+
"VERIFY" jws-verify
31+
"SIGN" jws-sign}})

src/axel_f/excel/text.cljc

+6-8
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
(defn CONCATENATE*
7474
"Appends strings to one another."
7575
[st1 & stx]
76-
(apply TEXTJOIN "" false st1 stx))
76+
(apply TEXTJOIN "" true st1 stx))
7777

7878
(def CONCATENATE #'CONCATENATE*)
7979

@@ -350,13 +350,11 @@
350350
(defn TEXTJOIN*
351351
"Combines the text from multiple strings and/or arrays, with a specifiable delimiter separating the different texts."
352352
[delimeter ignore-empty & items]
353-
(->> items
354-
flatten
355-
(map coerce/excel-str)
356-
(filter (if ignore-empty
357-
not-empty
358-
identity))
359-
(string/join delimeter)))
353+
(let [items (flatten items)
354+
items (if ignore-empty (keep identity items) items)]
355+
(->> items
356+
(map coerce/excel-str)
357+
(string/join delimeter))))
360358

361359
(def TEXTJOIN #'TEXTJOIN*)
362360

test/axel_f/functions_test.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@
577577
(t/is (= "foo, baz"
578578
((af/compile "TEXTJOIN(\", \", TRUE, _)") ["foo" nil "baz"])))
579579

580-
(t/is (= "foo, , baz"
580+
(t/is (= "foo, NULL, baz"
581581
((af/compile "TEXTJOIN(\", \", FALSE, _)") ["foo" nil "baz"]))))
582582

583583
(t/deftest complex-example

test/axel_f/issue_96_test.cljc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(ns axel-f.issue-96-test
2+
(:require #?(:clj [clojure.test :as t]
3+
:cljs [cljs.test :as t :include-macros true])
4+
[axel-f.excel :as af]))
5+
6+
(t/deftest coercion-to-string-in-text-functions
7+
8+
(t/testing "ratio"
9+
10+
(t/is (= "310.3333333333333333" ((af/compile "CONCATENATE(.x, .y, .y / .x)") {"x" 3 "y" 1})))))

test/axel_f/jws_test.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:cljs [cljs.test :as t :include-macros true])))
55

66
(t/deftest jws-hs256
7-
#_(t/testing "Sign payload using HS256"
7+
(t/testing "Sign payload using HS256"
88
(t/is (= "eyJhbGciOiJIUzI1NiJ9.eyJmb28iOjEsImJhciI6WzQsNSwicXdlIl19.HU45XthYzICLPj8RvTeVQum2FLPdynx0MTsSCs5l-O0"
99
((af/compile "JWS.SIGN('HS256', JSON.ENCODE(OBJECT.NEW({{\"foo\", 1}, {\"bar\", {4, 5, 'qwe'}}})), 'password')")))))
1010

0 commit comments

Comments
 (0)