Skip to content

Commit

Permalink
Merge pull request #528 from exadel-inc/feature/EAK-527
Browse files Browse the repository at this point in the history
[EAK-527] Enhanced the string transformation routine
  • Loading branch information
smiakchilo authored Jun 4, 2024
2 parents a506104 + ddcb325 commit 716a048
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,49 @@
package com.exadel.aem.toolkit.api.annotations.meta;

import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

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

/**
* Enumerates transformations of a string value that can be applied when rendering as an attribute of a Granite UI entity
*/
public enum StringTransformation {

/**
* No transformation is applied (default value)
*/
NONE(null),

/**
* The string is converted to uppercase
*/
UPPERCASE(String::toUpperCase),

/**
* The string is converted to lowercase
*/
LOWERCASE(String::toLowerCase),
CAMELCASE(string -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, string.toLowerCase())),
CAPITALIZE(string -> WordUtils.capitalizeFully(string, ' ', '-'));

/**
* The string is split into words delimited by a space, a hyphen, or an underscore. The words are then merged
* into a {@code camelCase} string
*/
CAMELCASE(StringTransformation::toCamelCase),

/**
* The string is split into words delimited by a space, a hyphen, or an underscore. The first letter of each
* word is capitalized. The words are then merged with a space between them
*/
CAPITALIZE(StringTransformation::capitalize);

private static final char CHAR_SPACE = ' ';
private static final char CHAR_HYPHEN = '-';
private static final char CHAR_UNDERSCORE = '_';

private final UnaryOperator<String> transformation;

Expand All @@ -52,4 +80,34 @@ public String apply(String value) {
return transformation.apply(value);
}

/* ---------------
Transformations
--------------- */

/**
* Converts a string to camelCase
* @param value The string to be transformed
* @return Resulting string value
*/
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));
}

/**
* Capitalizes the first letter of each word in the provided string
* @param value The string to be transformed
* @return Resulting string value
*/
private static String capitalize(String value) {
if (StringUtils.isBlank(value)) {
return value;
}
String[] words = StringUtils.split(value, " -_");
return Stream.of(words).map(StringUtils::capitalize).collect(Collectors.joining(StringUtils.SPACE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exadel.aem.toolkit.core;
package com.exadel.aem.toolkit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import com.exadel.aem.toolkit.api.annotations.meta.StringTransformationTest;
import com.exadel.aem.toolkit.core.injectors.ChildInjectorTest;
import com.exadel.aem.toolkit.core.injectors.ChildrenInjectorTest;
import com.exadel.aem.toolkit.core.injectors.EToolboxListInjectorTest;
Expand Down Expand Up @@ -45,6 +46,8 @@
*/
@RunWith(Suite.class)
@SuiteClasses({
StringTransformationTest.class,

ListHelperTest.class,
ListPageUtilTest.class,
ListResourceUtilTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exadel.aem.toolkit.api.annotations.meta;

import org.junit.Assert;
import org.junit.Test;

public class StringTransformationTest {

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";

@Test
public void shouldConvertToLowerCase() {
Assert.assertEquals("l0rem ipsum! do1or sit amet", StringTransformation.LOWERCASE.apply(SAMPLE1));
}

@Test
public void shouldConvertToUpperCase() {
Assert.assertEquals("L0REM IPSUM! DO1OR SIT AMET", StringTransformation.UPPERCASE.apply(SAMPLE1));
}

@Test
public void shouldConvertToCamelCase() {
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("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));
}
}

0 comments on commit 716a048

Please sign in to comment.