diff --git a/src/clj_jtwig/standard_functions.clj b/src/clj_jtwig/standard_functions.clj index 3271d97..11ac30d 100644 --- a/src/clj_jtwig/standard_functions.clj +++ b/src/clj_jtwig/standard_functions.clj @@ -43,15 +43,6 @@ (with-out-str (clojure.pprint/print-table x)))} - "nth" - {:fn (fn [sequence index & optional-not-found] - (let [values (if (map? sequence) ; map instance check to match behaviour of jtwig's first/last implementation - (-> sequence vals) - sequence)] - (if optional-not-found - (nth values index (first optional-not-found)) - (nth values index))))} - "max" {:fn (fn [& numbers] (if (coll? (first numbers)) @@ -68,6 +59,15 @@ {:fn (fn [s] (StringUtils/normalizeSpace s))} + "nth" + {:fn (fn [sequence index & optional-not-found] + (let [values (if (map? sequence) ; map instance check to match behaviour of jtwig's first/last implementation + (-> sequence vals) + sequence)] + (if optional-not-found + (nth values index (first optional-not-found)) + (nth values index))))} + "pad_left" {:fn (fn [s size & [padding-string]] (StringUtils/leftPad s size (or padding-string " ")))} diff --git a/test/clj_jtwig/functions_test.clj b/test/clj_jtwig/functions_test.clj index 33f72c8..730d0b2 100644 --- a/test/clj_jtwig/functions_test.clj +++ b/test/clj_jtwig/functions_test.clj @@ -304,16 +304,6 @@ (is (= (render "{{ t|dump_table }}", {:t [{:a 1 :b 2 :c 3} {:b 5 :a 7 :c "dog"}]}) "\n| b | c | a |\n|---+-----+---|\n| 2 | 3 | 1 |\n| 5 | dog | 7 |\n"))) - (testing "nth" - (is (= (render "{{ [1, 2, 3, 4, 5]|nth(2) }}" nil) - "3")) - (is (thrown-with-msg? - Exception - #"java.lang.IndexOutOfBoundsException" - (render "{{ [1, 2, 3, 4, 5]|nth(6) }}" nil))) - (is (= (render "{{ [1, 2, 3, 4, 5]|nth(6, \"not found\") }}" nil) - "not found"))) - (testing "max" (is (= (render "{{ [2, 1, 5, 3, 4]|max }}" nil) "5")) @@ -330,6 +320,16 @@ (is (= (render "{{ normalize_space(' hello world ') }}" nil) "hello world"))) + (testing "nth" + (is (= (render "{{ [1, 2, 3, 4, 5]|nth(2) }}" nil) + "3")) + (is (thrown-with-msg? + Exception + #"java.lang.IndexOutOfBoundsException" + (render "{{ [1, 2, 3, 4, 5]|nth(6) }}" nil))) + (is (= (render "{{ [1, 2, 3, 4, 5]|nth(6, \"not found\") }}" nil) + "not found"))) + (testing "pad_left" (is (= (render "{{ pad_left('bat', 5) }}" nil) " bat"))