rearrange nth function alphabetically

This commit is contained in:
Gered 2014-03-23 11:32:16 -04:00
parent d807f45091
commit fd73645574
2 changed files with 19 additions and 19 deletions

View file

@ -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 " ")))}

View file

@ -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"))