Skip to content

Better camelization: removed duplication, replace also spaces #5693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ public static String camelize(String word) {

private static Pattern camelizeSlashPattern = Pattern.compile("\\/(.?)");
private static Pattern camelizeUppercasePattern = Pattern.compile("(\\.?)(\\w)([^\\.]*)$");
private static Pattern camelizeUnderscorePattern = Pattern.compile("(_)(.)");
private static Pattern camelizeHyphenPattern = Pattern.compile("(-)(.)");
private static Pattern camelizeCharsPattern = Pattern.compile("([_ -])(.)");
private static Pattern camelizeDollarPattern = Pattern.compile("\\$");
private static Pattern camelizeSimpleUnderscorePattern = Pattern.compile("_");

Expand Down Expand Up @@ -167,7 +166,8 @@ public static String camelize(final String inputWord, boolean lowercaseFirstLett
}

// Remove all underscores (underscore_case to camelCase)
m = camelizeUnderscorePattern.matcher(word);
// Remove all hyphens (hyphen-case to camelCase)
m = camelizeCharsPattern.matcher(word);
while (m.find()) {
String original = m.group(2);
String upperCase = original.toUpperCase(Locale.ROOT);
Expand All @@ -176,14 +176,7 @@ public static String camelize(final String inputWord, boolean lowercaseFirstLett
} else {
word = m.replaceFirst(upperCase);
}
m = camelizeUnderscorePattern.matcher(word);
}

// Remove all hyphens (hyphen-case to camelCase)
m = camelizeHyphenPattern.matcher(word);
while (m.find()) {
word = m.replaceFirst(m.group(2).toUpperCase(Locale.ROOT));
m = camelizeHyphenPattern.matcher(word);
m = camelizeCharsPattern.matcher(word);
}

if (lowerFirstLetter && word.length() > 0) {
Expand Down Expand Up @@ -266,9 +259,9 @@ public static String escape(final String name, final Map<String, String> replace
return character;
}
}).reduce( (c1, c2) -> "" + c1 + c2).orElse(null);

if (result != null) return result;
throw new RuntimeException("Word '" + name + "' could not be escaped.");
throw new RuntimeException("Word '" + name + "' could not be escaped.");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ public void testCamelize() throws Exception {
Assert.assertEquals(camelize("some-value"), "SomeValue");
Assert.assertEquals(camelize("some_value"), "SomeValue");
Assert.assertEquals(camelize("$type"), "$Type");
Assert.assertEquals(camelize("Upper lower"), "UpperLower");
Assert.assertEquals(camelize("Upper Upper"), "UpperUpper");
Assert.assertEquals(camelize("lower Upper"), "LowerUpper");

Assert.assertEquals(camelize("abcd", true), "abcd");
Assert.assertEquals(camelize("some-value", true), "someValue");
Assert.assertEquals(camelize("some_value", true), "someValue");
Assert.assertEquals(camelize("Abcd", true), "abcd");
Assert.assertEquals(camelize("$type", true), "$type");
Assert.assertEquals(camelize("Upper lower", true), "upperLower");
Assert.assertEquals(camelize("Upper Upper", true), "upperUpper");
Assert.assertEquals(camelize("lower Upper", true), "lowerUpper");

Assert.assertEquals(camelize("123", true), "123");
Assert.assertEquals(camelize("$123", true), "$123");
Expand Down