All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openapitools.codegen.templating.handlebars.StringHelpers Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
package org.openapitools.codegen.templating.handlebars;

import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.TagType;

import java.io.IOException;
import java.util.Locale;

public enum StringHelpers implements Helper {

    /**
     * Indicates whether the string starts with the given value.
     * For example:
     *
     * 
     * {{startsWith a b ["insensitive"]}}
     * 
* *
     * {{startsWith a text='b' [insensitive=true]}}
     * 
* * Render 'yes' or 'no': *
     *   {{#startsWith a b}}
     *     yes
     *   {{else}}
     *     no
     *   {{/startsWith}}
     * 
* * Render 'true' or 'false': *
     *   {{startsWith a b}}
     * 
* * Render 'y' or 'n': *
     *   {{startsWith a b yes='y' no='n'}}
     * 
*/ startsWith { @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).startsWith(match.toLowerCase(Locale.ROOT)) : value.toString().startsWith(match); if (options.tagType == TagType.SECTION) { 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: * *
     * {{endsWith a b ["insensitive"]}}
     * 
* *
     * {{endsWith a text='b' [insensitive=true]}}
     * 
* * Render 'yes' or 'no': *
     *   {{#endsWith a b}}
     *     yes
     *   {{else}}
     *     no
     *   {{/endsWith}}
     * 
* * Render 'true' or 'false': *
     *   {{endsWith a b}}
     * 
* * Render 'y' or 'n': *
     *   {{endsWith a b yes='y' no='n'}}
     * 
*/ 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); } } }