Skip to content

Commit

Permalink
[EAK-527] Reduced an external dependency inside StringTransformation
Browse files Browse the repository at this point in the history
  • Loading branch information
smiakchilo committed Jun 7, 2024
1 parent 764c8d4 commit a294bd8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import com.google.common.base.CaseFormat;

import com.exadel.aem.toolkit.core.CoreConstants;

Expand Down Expand Up @@ -93,9 +92,14 @@ private static String toCamelCase(String value) {
if (StringUtils.isBlank(value)) {
return value;
}
return value.contains(CoreConstants.SEPARATOR_HYPHEN)
? CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, value.toLowerCase().replace(CHAR_SPACE, CHAR_HYPHEN))
: CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, value.toLowerCase().replace(CHAR_SPACE, CHAR_UNDERSCORE));
String[] words = value.contains(CoreConstants.SEPARATOR_HYPHEN)
? StringUtils.split(value, " -")
: StringUtils.split(value, " _");
return words[0].toLowerCase() + Stream.of(words)
.skip(1)
.map(StringUtils::lowerCase)
.map(StringUtils::capitalize)
.collect(Collectors.joining());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
*/
package com.exadel.aem.toolkit.api.annotations.meta;

import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;

public class StringTransformationTest {

private static final String SAMPLE0 = "LoReM";
private static final String SAMPLE1 = "L0rem IPSum! Do1or Sit Amet";
private static final String SAMPLE2 = "Lorem IPSum dolor-sit-Amet";
private static final String SAMPLE3 = "Lorem_IPSum__dolor_Sit_Amet";
Expand All @@ -34,13 +36,17 @@ public void shouldConvertToUpperCase() {

@Test
public void shouldConvertToCamelCase() {
Assert.assertEquals(StringUtils.EMPTY, StringTransformation.CAMELCASE.apply(StringUtils.EMPTY));
Assert.assertEquals("lorem", StringTransformation.CAMELCASE.apply(SAMPLE0));
Assert.assertEquals("l0remIpsum!Do1orSitAmet", StringTransformation.CAMELCASE.apply(SAMPLE1));
Assert.assertEquals("loremIpsumDolorSitAmet", StringTransformation.CAMELCASE.apply(SAMPLE2));
Assert.assertEquals("loremIpsumDolorSitAmet", StringTransformation.CAMELCASE.apply(SAMPLE3));
}

@Test
public void shouldCapitalize() {
Assert.assertEquals(StringUtils.EMPTY, StringTransformation.CAPITALIZE.apply(StringUtils.EMPTY));
Assert.assertEquals("Lorem", StringTransformation.CAPITALIZE.apply(SAMPLE0));
Assert.assertEquals("L0rem Ipsum! Do1or Sit Amet", StringTransformation.CAPITALIZE.apply(SAMPLE1));
Assert.assertEquals("Lorem Ipsum Dolor Sit Amet", StringTransformation.CAPITALIZE.apply(SAMPLE2));
Assert.assertEquals("Lorem Ipsum Dolor Sit Amet", StringTransformation.CAPITALIZE.apply(SAMPLE3));
Expand Down

0 comments on commit a294bd8

Please sign in to comment.