aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 14:55:50 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 14:55:50 +0100
commitc5469ba6723eaecc5797acc842d090e1b85afb60 (patch)
treea44366a08174b5eede7a1d1177f02ffa66856b0a
parentef00aa15f8eef283dadc28aba7f36ea918d61b15 (diff)
downloadunja-c5469ba6723eaecc5797acc842d090e1b85afb60.tar.gz
unja-c5469ba6723eaecc5797acc842d090e1b85afb60.zip
add filter: length
-rw-r--r--src/template.c9
-rw-r--r--tests/test_template.c7
2 files changed, 16 insertions, 0 deletions
diff --git a/src/template.c b/src/template.c
index f85db25..cf021c1 100644
--- a/src/template.c
+++ b/src/template.c
@@ -666,11 +666,20 @@ struct unja_object *filter_wordcount(struct unja_object *obj) {
return make_int_object(word_count);
}
+
+struct unja_object *filter_length(struct unja_object *obj) {
+ assert(obj->type == OBJ_STRING);
+ int len = strlen(obj->string);
+ object_free(obj);
+ return make_int_object(len);
+}
+
struct hashmap *default_filters() {
struct hashmap *filters = hashmap_new();
hashmap_insert(filters, "trim", filter_trim);
hashmap_insert(filters, "lower", filter_lower);
hashmap_insert(filters, "wordcount", filter_wordcount);
+ hashmap_insert(filters, "length", filter_length);
return filters;
}
diff --git a/tests/test_template.c b/tests/test_template.c
index 6b2ca12..1ad131e 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -484,4 +484,11 @@ TEST(filter_wordcount) {
free(output);
}
+TEST(filter_title) {
+ char *input = "{{ \"Hello world\" | length }}";
+ char *output = template_string(input, NULL);
+ assert_str(output, "11");
+ free(output);
+}
+
END_TESTS \ No newline at end of file