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.9.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 the string starts with the defined 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'}}
     * 
* * If value is "handlebars.java", the output will be "Handlebars.java". */ 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); } } }