summaryrefslogtreecommitdiffstats
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/src/com/vaadin/shared/util/SharedUtil.java30
-rw-r--r--shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java42
2 files changed, 70 insertions, 2 deletions
diff --git a/shared/src/com/vaadin/shared/util/SharedUtil.java b/shared/src/com/vaadin/shared/util/SharedUtil.java
index bc5d87b9f5..d363d767be 100644
--- a/shared/src/com/vaadin/shared/util/SharedUtil.java
+++ b/shared/src/com/vaadin/shared/util/SharedUtil.java
@@ -149,11 +149,12 @@ public class SharedUtil implements Serializable {
sb.append(parts[i]);
sb.append(separator);
}
- return sb.substring(0, sb.length() - 1);
+ return sb.substring(0, sb.length() - separator.length());
}
/**
- * Capitalizes the first character in the given string
+ * Capitalizes the first character in the given string in a way suitable for
+ * use in code (methods, properties etc)
*
* @since 7.4
* @param string
@@ -239,4 +240,29 @@ public class SharedUtil implements Serializable {
return uri;
}
+ /**
+ * Converts a dash ("-") separated string into camelCase.
+ * <p>
+ * Examples:
+ * <p>
+ * {@literal foo} becomes {@literal foo} {@literal foo-bar} becomes
+ * {@literal fooBar} {@literal foo--bar} becomes {@literal fooBar}
+ *
+ * @since
+ * @param dashSeparated
+ * The dash separated string to convert
+ * @return a camelCase version of the input string
+ */
+ public static String dashSeparatedToCamelCase(String dashSeparated) {
+ if (dashSeparated == null) {
+ return null;
+ }
+ String[] parts = dashSeparated.split("-");
+ for (int i = 1; i < parts.length; i++) {
+ parts[i] = capitalize(parts[i]);
+ }
+
+ return join(parts, "");
+ }
+
}
diff --git a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java
index 208d4ca7c7..e77501a446 100644
--- a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java
+++ b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java
@@ -3,6 +3,8 @@ package com.vaadin.shared.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import java.util.Locale;
+
import org.junit.Assert;
import org.junit.Test;
@@ -76,4 +78,44 @@ public class SharedUtilTests {
String[] splitParts = SharedUtil.splitCamelCase(camelCaseString);
Assert.assertArrayEquals(parts, splitParts);
}
+
+ @Test
+ public void join() {
+ String s1 = "foo-bar-baz";
+ String s2 = "foo--bar";
+
+ Assert.assertEquals("foobarbaz", SharedUtil.join(s1.split("-"), ""));
+ Assert.assertEquals("foo!bar!baz", SharedUtil.join(s1.split("-"), "!"));
+ Assert.assertEquals("foo!!bar!!baz",
+ SharedUtil.join(s1.split("-"), "!!"));
+
+ Assert.assertEquals("foo##bar", SharedUtil.join(s2.split("-"), "#"));
+ }
+
+ @Test
+ public void dashSeparatedToCamelCase() {
+ Assert.assertEquals(null, SharedUtil.dashSeparatedToCamelCase(null));
+ Assert.assertEquals("", SharedUtil.dashSeparatedToCamelCase(""));
+ Assert.assertEquals("foo", SharedUtil.dashSeparatedToCamelCase("foo"));
+ Assert.assertEquals("fooBar",
+ SharedUtil.dashSeparatedToCamelCase("foo-bar"));
+ Assert.assertEquals("fooBar",
+ SharedUtil.dashSeparatedToCamelCase("foo--bar"));
+ Assert.assertEquals("fooBarBaz",
+ SharedUtil.dashSeparatedToCamelCase("foo-bar-baz"));
+ Assert.assertEquals("fooBarBaz",
+ SharedUtil.dashSeparatedToCamelCase("foo-Bar-Baz"));
+ }
+
+ @Test
+ public void methodUppercaseWithTurkishLocale() {
+ Locale defaultLocale = Locale.getDefault();
+ try {
+ Locale.setDefault(new Locale("tr", "TR"));
+ Assert.assertEquals("Integer", SharedUtil.capitalize("integer"));
+ } finally {
+ Locale.setDefault(defaultLocale);
+ }
+ }
+
}