From 85a08d1f8078e6572643109d960e8b5d89df9ee2 Mon Sep 17 00:00:00 2001 From: Janos Erdos Date: Thu, 17 Jan 2019 12:56:35 +0100 Subject: [PATCH] release 0.2.7: added join() string function --- docs/Functions.md | 8 ++++ .../stencil/functions/StringFunctions.java | 38 +++++++++++++++++++ .../functions/StringFunctionsTest.java | 17 +++++++++ project.clj | 2 +- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/docs/Functions.md b/docs/Functions.md index de2995ee..bc655f85 100644 --- a/docs/Functions.md +++ b/docs/Functions.md @@ -35,6 +35,14 @@ of an array or hide the whole paragraph when the array is empty. These functions deal with textual data. +## Join + +Joins a list of items with an optional separator. + +**Example:** call join(xs, ",") to join them with a comma. + +**Example:** call join(xs) to just concatenate the items. + ## Format Calls [String.format](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html) function. diff --git a/java-src/io/github/erdos/stencil/functions/StringFunctions.java b/java-src/io/github/erdos/stencil/functions/StringFunctions.java index 62a65877..376d00b5 100644 --- a/java-src/io/github/erdos/stencil/functions/StringFunctions.java +++ b/java-src/io/github/erdos/stencil/functions/StringFunctions.java @@ -1,7 +1,9 @@ package io.github.erdos.stencil.functions; import java.util.Arrays; +import java.util.Collection; import java.util.IllegalFormatException; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -32,6 +34,42 @@ public Object call(Object... arguments) { } }, + JOIN { + @SuppressWarnings("unchecked") + @Override + public Object call(Object... arguments) { + if (arguments.length == 0) { + throw new IllegalArgumentException("At least one arg is expected!"); + } else if (arguments.length == 1 || (arguments.length == 2 && arguments[1] == null)) { + if (arguments[0] == null) { + return ""; + } else if (arguments[0] instanceof Collection) { + final Collection items = (Collection) arguments[0]; + return items.stream() + .filter(Objects::nonNull) + .map(Objects::toString) + .collect(Collectors.joining()); + } else { + throw new IllegalArgumentException("First parameter must be a Collection!"); + } + } else if (arguments.length == 2) { + if (!(arguments[0] instanceof Collection)) { + throw new IllegalArgumentException("First parameter must be a Collection!"); + } else if (!(arguments[1] instanceof String)) { + throw new IllegalArgumentException("Second parameter must be a String!"); + } else { + final Collection items = (Collection) arguments[0]; + return items.stream() + .filter(Objects::nonNull) + .map(Objects::toString) + .collect(Collectors.joining((String) (arguments[1]))); + } + } else { + throw new IllegalArgumentException("Join function expects 1 or 2 arguments!"); + } + } + }, + /** * Converts parameters to strings and concatenates the result. * Returns empty string on empty or null arguments. diff --git a/java-test/io/github/erdos/stencil/functions/StringFunctionsTest.java b/java-test/io/github/erdos/stencil/functions/StringFunctionsTest.java index 4bd6b87e..3697b5c0 100644 --- a/java-test/io/github/erdos/stencil/functions/StringFunctionsTest.java +++ b/java-test/io/github/erdos/stencil/functions/StringFunctionsTest.java @@ -3,7 +3,12 @@ import org.junit.Assert; import org.junit.Test; +import java.util.Collections; + import static io.github.erdos.stencil.functions.StringFunctions.FORMAT; +import static io.github.erdos.stencil.functions.StringFunctions.JOIN; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; import static org.junit.Assert.assertEquals; public class StringFunctionsTest { @@ -27,4 +32,16 @@ public void testFormat() { // direkt ures! } } + + @Test + public void testJoin() { + assertEquals("", JOIN.call(emptyList())); + + assertEquals("123", JOIN.call(asList(1, 2, 3))); + assertEquals("123", JOIN.call(asList(1, 2, 3), null)); + assertEquals("1,2,3", JOIN.call(asList(1, 2, 3), ",")); + + assertEquals("", JOIN.call(emptyList(), null)); + assertEquals("", JOIN.call(emptyList(), ",")); + } } \ No newline at end of file diff --git a/project.clj b/project.clj index 45b7909c..b738b2a9 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject io.github.erdos/stencil-core "0.2.6" +(defproject io.github.erdos/stencil-core "0.2.7" :url "https://github.com/erdos/stencil" :description "Templating engine for office documents." :license {:name "Eclipse Public License - v 2.0"