Skip to content

Commit

Permalink
release 0.2.7: added join() string function
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Erdos committed Jan 17, 2019
1 parent 294ea31 commit 85a08d1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions java-src/io/github/erdos/stencil/functions/StringFunctions.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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<Object> items = (Collection<Object>) 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<Object> items = (Collection<Object>) 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(), ","));
}
}
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 85a08d1

Please sign in to comment.