Skip to content

Commit 45d3096

Browse files
committed
cut on empty string returns unexpected result fix #714
1 parent 56e53f3 commit 45d3096

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,15 @@ protected CharSequence safeApply(final Object value, final Options options) {
103103
* If value is "String with spaces", the output will be "Stringwithspaces".
104104
*/
105105
cut {
106+
@Override public Object apply(Object context, Options options) throws IOException {
107+
return safeApply(context, options);
108+
}
109+
106110
@Override
107111
protected CharSequence safeApply(final Object value, final Options options) {
112+
if (options.isFalsy(value)) {
113+
return "";
114+
}
108115
String strip = options.param(0, " ");
109116
return value.toString().replace(strip, "");
110117
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.jknack.handlebars;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Test;
6+
7+
import com.github.jknack.handlebars.helper.ConditionalHelpers;
8+
import com.github.jknack.handlebars.helper.StringHelpers;
9+
10+
public class Issue714 extends v4Test {
11+
12+
@Override protected void configure(Handlebars handlebars) {
13+
handlebars.registerHelpers(StringHelpers.class);
14+
}
15+
16+
@Test
17+
public void shouldIgnoreEmptyString() throws IOException {
18+
shouldCompileTo("{{cut value \"-\"}}", $("hash", $("value", "")), "");
19+
20+
shouldCompileTo("{{cut value \"-\"}}", $("hash", $("value", "2019-12-30")), "20191230");
21+
}
22+
}

0 commit comments

Comments
 (0)