forked from loafle/openapi-generator-original
Add 'endsWith' handlebars helper function (#14634)
* Add 'endsWith' handlebars helper function * add tests * fix test
This commit is contained in:
committed by
GitHub
parent
e00d8d564a
commit
f455ac166c
@@ -10,7 +10,7 @@ import java.util.Locale;
|
||||
public enum StringHelpers implements Helper<Object> {
|
||||
|
||||
/**
|
||||
* Indicates the string starts with the defined value.
|
||||
* Indicates whether the string starts with the given value.
|
||||
* For example:
|
||||
*
|
||||
* <pre>
|
||||
@@ -39,8 +39,6 @@ public enum StringHelpers implements Helper<Object> {
|
||||
* <pre>
|
||||
* {{startsWith a b yes='y' no='n'}}
|
||||
* </pre>
|
||||
*
|
||||
* If value is "handlebars.java", the output will be "Handlebars.java".
|
||||
*/
|
||||
startsWith {
|
||||
@Override
|
||||
@@ -57,6 +55,60 @@ public enum StringHelpers implements Helper<Object> {
|
||||
return result ? options.fn() : options.inverse();
|
||||
}
|
||||
|
||||
return result
|
||||
? options.hash("yes", true)
|
||||
: options.hash("no", false);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Indicates whether the string ends with the given value.
|
||||
* For example:
|
||||
*
|
||||
* <pre>
|
||||
* {{endsWith a b ["insensitive"]}}
|
||||
* </pre>
|
||||
*
|
||||
* <pre>
|
||||
* {{endsWith a text='b' [insensitive=true]}}
|
||||
* </pre>
|
||||
*
|
||||
* Render 'yes' or 'no':
|
||||
* <pre>
|
||||
* {{#endsWith a b}}
|
||||
* yes
|
||||
* {{else}}
|
||||
* no
|
||||
* {{/endsWith}}
|
||||
* </pre>
|
||||
*
|
||||
* Render 'true' or 'false':
|
||||
* <pre>
|
||||
* {{endsWith a b}}
|
||||
* </pre>
|
||||
*
|
||||
* Render 'y' or 'n':
|
||||
* <pre>
|
||||
* {{endsWith a b yes='y' no='n'}}
|
||||
* </pre>
|
||||
*/
|
||||
endsWith {
|
||||
@Override
|
||||
public Object apply(Object value, Options options) throws IOException {
|
||||
String match = options.param(0, options.hash("text", ""));
|
||||
if (match.length() < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean caseInsensitive = options.hash("insensitive", false);
|
||||
boolean result = caseInsensitive
|
||||
? value.toString().toLowerCase(Locale.ROOT).endsWith(match.toLowerCase(Locale.ROOT))
|
||||
: value.toString().endsWith(match);
|
||||
|
||||
if (options.tagType == TagType.SECTION) {
|
||||
return result ? options.fn() : options.inverse();
|
||||
}
|
||||
|
||||
return result
|
||||
? options.hash("yes", true)
|
||||
: options.hash("no", false);
|
||||
|
||||
@@ -83,4 +83,53 @@ public class StringHelpersTest {
|
||||
template = "{{startsWith asdf b yes='y' no='n'}}";
|
||||
evaluate(data, template, "n");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "Handlebars StringHelpers.endsWith, section")
|
||||
public void endsWithSectionalTest() throws IOException {
|
||||
HashMap<String, Object> data = new HashMap<String, Object>() {{
|
||||
put("asdf", "asdf");
|
||||
put("df", "df");
|
||||
put("b", "b");
|
||||
}};
|
||||
|
||||
String template = "{{~#endsWith asdf df ~}}yes{{~else~}}no{{~/endsWith~}}";
|
||||
evaluate(data, template, "yes");
|
||||
|
||||
template = "{{~#endsWith asdf b ~}}yes{{~else~}}no{{~/endsWith~}}";
|
||||
evaluate(data, template, "no");
|
||||
}
|
||||
|
||||
@Test(description = "Handlebars StringHelpers.endsWith")
|
||||
public void endsWithTest() throws IOException {
|
||||
HashMap<String, Object> data = new HashMap<String, Object>() {{
|
||||
put("asdf", "asdf");
|
||||
put("ASDF", "ASDF");
|
||||
put("f", "f");
|
||||
put("b", "b");
|
||||
}};
|
||||
evaluate(data, "{{endsWith asdf f}}", "true");
|
||||
evaluate(data, "{{endsWith asdf b}}", "false");
|
||||
evaluate(data, "{{endsWith ASDF f insensitive=true }}", "true");
|
||||
evaluate(data, "{{endsWith ASDF f insensitive=false }}", "false");
|
||||
evaluate(data, "{{endsWith ASDF 'f' insensitive=true }}", "true");
|
||||
evaluate(data, "{{endsWith ASDF b insensitive=true }}", "false");
|
||||
evaluate(data, "{{endsWith ASDF 'b' insensitive=true }}", "false");
|
||||
evaluate(data, "{{endsWith ASDF insensitive=true text='f'}}", "true");
|
||||
evaluate(data, "{{endsWith ASDF insensitive=true text='f' yes='✓' no='✘'}}", "✓");
|
||||
evaluate(data, "{{endsWith ASDF insensitive=false text='f' yes='✓' no='✘'}}", "✘");
|
||||
}
|
||||
|
||||
@Test(description = "Handlebars StringHelpers.endsWith, yes/no override")
|
||||
public void endsWithYesOverrideTest() throws IOException {
|
||||
HashMap<String, Object> data = new HashMap<String, Object>() {{
|
||||
put("asdf", "asdf");
|
||||
put("f", "f");
|
||||
put("b", "b");
|
||||
}};
|
||||
String template = "{{endsWith asdf f yes='y' no='n'}}";
|
||||
evaluate(data, template, "y");
|
||||
|
||||
template = "{{endsWith asdf b yes='y' no='n'}}";
|
||||
evaluate(data, template, "n");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user