aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 14:48:34 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 14:48:34 +0100
commitf64c97341cedf668c6e7ae7cad818bae6fdb90bd (patch)
treef01d9007b84b00600d638274e45883a83d037003
parentd38575d2b97f7a86cfe6dd93df7de31612e19890 (diff)
downloadunja-f64c97341cedf668c6e7ae7cad818bae6fdb90bd.tar.gz
unja-f64c97341cedf668c6e7ae7cad818bae6fdb90bd.zip
add filter: lower
-rw-r--r--src/template.c21
-rw-r--r--tests/test_template.c9
2 files changed, 24 insertions, 6 deletions
diff --git a/src/template.c b/src/template.c
index 1c1945e..9555206 100644
--- a/src/template.c
+++ b/src/template.c
@@ -4,6 +4,8 @@
#include <unistd.h>
#include <dirent.h>
#include <string.h>
+#include <assert.h>
+
#include "vendor/mpc.h"
#include "template.h"
@@ -634,16 +636,25 @@ char *render_ast(mpc_ast_t *ast, struct context *ctx) {
return buf.string;
}
-struct unja_object *filter_trim(struct unja_object *in) {
- /* TODO: Keep original pointer */
- in->string = trim_leading_whitespace(in->string);
- trim_trailing_whitespace(in->string);
- return in;
+struct unja_object *filter_trim(struct unja_object *obj) {
+ assert(obj->type == OBJ_STRING);
+ obj->string = trim_leading_whitespace(obj->string);
+ trim_trailing_whitespace(obj->string);
+ return obj;
+}
+
+struct unja_object *filter_lower(struct unja_object *obj) {
+ assert(obj->type == OBJ_STRING);
+ for (int i=0; i < strlen(obj->string); i++) {
+ obj->string[i] = tolower(obj->string[i]);
+ }
+ return obj;
}
struct hashmap *default_filters() {
struct hashmap *filters = hashmap_new();
hashmap_insert(filters, "trim", filter_trim);
+ hashmap_insert(filters, "lower", filter_lower);
return filters;
}
diff --git a/tests/test_template.c b/tests/test_template.c
index b0f03ff..51c8fad 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -454,7 +454,7 @@ TEST(inheritance_depth_2) {
env_free(env);
}
-TEST(filter) {
+TEST(filter_trim) {
char *input = "{{ text | trim }}";
struct hashmap *ctx = hashmap_new();
char *text = "\nHello world\n";
@@ -465,4 +465,11 @@ TEST(filter) {
free(output);
}
+TEST(filter_lower) {
+ char *input = "{{ \"Hello World\" | lower }}";
+ char *output = template_string(input, NULL);
+ assert_str(output, "hello world");
+ free(output);
+}
+
END_TESTS \ No newline at end of file