Skip to content

Commit

Permalink
feat: add joinAnd function
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Oct 15, 2021
1 parent f6c218f commit cc6c7e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This is a short description of the functions implemented in Stencil:
- [html](#html)
- [integer](#integer)
- [join](#join)
- [joinAnd](#joinAnd)
- [length](#length)
- [list](#list)
- [lowercase](#lowercase)
Expand Down Expand Up @@ -102,6 +103,13 @@ Joins a list of items with an optional separator.

**Example:** call join(xs) to just concatenate the items.

### JoinAnd

Joins a list of items using two separators. The first separator is used to join the items except for the last item. The second separator is used to join the last item. When two items are supplied, then
only the second separator is used.

**Example:** call <code>join(xs, ", ", " and ")</code> to get <code>"1, 2, 3 and 4"</code>.

### Date

Formats a date value according to a given [format string](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html).
Expand Down
11 changes: 9 additions & 2 deletions src/stencil/functions.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns stencil.functions
"Function definitions"
(:require [stencil.types :refer [->HideTableColumnMarker ->HideTableRowMarker]]
(:require [clojure.string]
[stencil.types :refer [->HideTableColumnMarker ->HideTableRowMarker]]
[stencil.util :refer [fail find-first]]))

(set! *warn-on-reflection* true)
Expand All @@ -20,7 +21,7 @@
(defmethod call-fn "decimal" [_ f] (some-> f bigdec))

;; The format() function calls java.lang.String.format()
;; but it predicts the argumet types from the format string and
;; but it predicts the argument types from the format string and
;; converts the argument values to the correct types to prevent runtime errors.
(let [fs-pattern #"%(?:(\d+)\$)?([-#+ 0,(\<]*)?(\d+)?(\.\d+)?([tT])?([a-zA-Z%])"
get-types (fn [pattern-str]
Expand Down Expand Up @@ -101,3 +102,9 @@
(keep (partial lookup p) elems))))
data
(.split column "\\.")))

(defmethod call-fn "joinAnd" [_ elements ^String separator1 ^String separator2]
(case (count elements)
0 ""
1 (str (first elements))
(str (clojure.string/join separator1 (butlast elements)) separator2 (last elements))))
8 changes: 8 additions & 0 deletions test/stencil/functions_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@
(is (thrown? ExceptionInfo (call-fn "map" "x" {:x 1 :y 2})))
(is (thrown? ExceptionInfo (call-fn "map" 1 [])))))

(deftest test-join-and
(are [expect param] (= expect (call-fn "joinAnd" param ", " " and "))
"" nil
"" []
"1" [1]
"1 and 2" [1 2]
"1, 2 and 3" [1 2 3]))

(import '[stencil.types ReplaceImage])
(def data-uri "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==")

Expand Down

0 comments on commit cc6c7e2

Please sign in to comment.