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);
}
}

View file

@ -35,6 +35,18 @@ class TestRenderUtils {
.isEqualTo(TestCase.SAMPLE_TEXT);
}
@Test
void testCapitalize() {
assertThat(RenderUtils.capitalize("a")).isEqualTo("A");
assertThat(RenderUtils.capitalize("")).as("empty").isEqualTo("");
}
@Test
void testHtmlEntities() {
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN))
.isEqualTo("&#77;&#246;&#99;&#104;&#116;&#101;&#110;&#32;&#83;&#105;&#101;&#32;&#101;&#105;&#110;&#32;&#112;&#97;&#97;&#114;&#32;&#196;&#112;&#102;&#101;&#108;&#63;");
}
@Test
void testMask() {
var foo = "4342256562440179";
@ -54,21 +66,21 @@ class TestRenderUtils {
assertThat(RenderUtils.normalize(SAMPLE_GERMAN)).isEqualTo("mochten-sie-ein-paar-apfel");
}
@Test
void testSwapCase() {
assertThat(RenderUtils.swapCase(SAMPLE_GERMAN)).isEqualTo("mÖCHTEN sIE EIN PAAR äPFEL?");
}
@Test
void testHtmlEntities() {
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN))
.isEqualTo("&#77;&#246;&#99;&#104;&#116;&#101;&#110;&#32;&#83;&#105;&#101;&#32;&#101;&#105;&#110;&#32;&#112;&#97;&#97;&#114;&#32;&#196;&#112;&#102;&#101;&#108;&#63;");
}
@Test
void testRot13() {
var encoded = "Zöpugra Fvr rva cnne Äcsry?";
assertThat(RenderUtils.rot13(SAMPLE_GERMAN)).as("encode").isEqualTo(encoded);
assertThat(RenderUtils.rot13(encoded)).as("decode").isEqualTo(SAMPLE_GERMAN);
}
@Test
void testSwapCase() {
assertThat(RenderUtils.swapCase(SAMPLE_GERMAN)).isEqualTo("mÖCHTEN sIE EIN PAAR äPFEL?");
}
@Test
void testUcapitalize() {
assertThat(RenderUtils.uncapitalize("A")).isEqualTo("a");
assertThat(RenderUtils.uncapitalize("")).as("empty").isEqualTo("");
}
}