Moved un/capitlize to RenderUtils

This commit is contained in:
Erik C. Thauvin 2023-03-21 23:12:03 -07:00
parent c55c8d803d
commit 9ff60a6f6b
5 changed files with 57 additions and 31 deletions

View file

@ -19,7 +19,6 @@ package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.Localization;
/**
* <p>Capitalizes a template value.</p>
@ -41,10 +40,6 @@ public class Capitalize implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
var value = template.getValueOrAttribute(differentiator);
if (value == null || value.isBlank()) {
return value;
}
return value.substring(0, 1).toUpperCase(Localization.getLocale()) + value.substring(1);
return RenderUtils.capitalize(template.getValueOrAttribute(differentiator));
}
}

View file

@ -59,6 +59,11 @@ public final class RenderUtils {
*/
public static final DateTimeFormatter RFC_2822_FORMATTER =
DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz").withLocale(Localization.getLocale());
/**
* Year formatter.
*/
static public final DateTimeFormatter YEAR_FORMATTER =
DateTimeFormatter.ofPattern("yyyy").withLocale(Localization.getLocale());
private static final String DEFAULT_USER_AGENT =
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
@ -101,6 +106,19 @@ public final class RenderUtils {
return String.format("@%03d", beats);
}
/**
* Capitalizes a String.
*
* @param src the source String
* @return the capitalized String
*/
public static String capitalize(String src) {
if (src == null || src.isBlank()) {
return src;
}
return src.substring(0, 1).toUpperCase(Localization.getLocale()) + src.substring(1);
}
/**
* Encodes a String to JavaScript/ECMAScript.
*
@ -402,6 +420,19 @@ public final class RenderUtils {
return new String(buff, 0, offset);
}
/**
* Un-capitalizes a String.
*
* @param src the source String
* @return the capitalized String
*/
public static String uncapitalize(String src) {
if (src == null || src.isBlank()) {
return src;
}
return src.substring(0, 1).toLowerCase(Localization.getLocale()) + src.substring(1);
}
/**
* Returns the formatted server uptime.
*

View file

@ -19,7 +19,6 @@ package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.Localization;
/**
* <p>Un-capitalizes a template value.</p>
@ -41,10 +40,6 @@ public class Uncapitalize implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
var value = template.getValueOrAttribute(differentiator);
if (value == null || value.isBlank()) {
return value;
}
return value.substring(0, 1).toLowerCase(Localization.getLocale()) + value.substring(1);
return RenderUtils.uncapitalize(template.getValueOrAttribute(differentiator));
}
}

View file

@ -19,10 +19,8 @@ package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.Localization;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* <p>Renders the current year.</p>
@ -39,17 +37,12 @@ import java.time.format.DateTimeFormatter;
* @since 1.0
*/
public class Year implements ValueRenderer {
/**
* Year formatter.
*/
static public final DateTimeFormatter yearFormatter =
DateTimeFormatter.ofPattern("yyyy").withLocale(Localization.getLocale());
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
return ZonedDateTime.now().format(yearFormatter);
return ZonedDateTime.now().format(RenderUtils.YEAR_FORMATTER);
}
}