Skip to content

Commit 27d10cb

Browse files
committed
Fixed shekhargulati#40. Added support for toSnakeCase function
1 parent f6811a9 commit 27d10cb

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/main/java/strman/Strman.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,16 @@ public static String toKebabCase(final String value) {
10331033
return toDecamelize(value, "-");
10341034
}
10351035

1036+
/**
1037+
* Transform to snake_case.
1038+
*
1039+
* @param value The input String
1040+
* @return String in snake_case.
1041+
*/
1042+
public static String toSnakeCase(final String value) {
1043+
return toDecamelize(value, "_");
1044+
}
1045+
10361046
public static String decode(final String value, final int digits, final int radix) {
10371047
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
10381048
return Arrays

src/test/java/strman/StrmanTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,4 +776,23 @@ public void toKebabCase_shouldKebabCaseAString() throws Exception {
776776

777777
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toKebabCase(%s) should be de-camelize", el), toKebabCase(el), equalTo("de-camelize")));
778778
}
779+
780+
@Test
781+
public void toSnakeCase_shouldSnakeCaseAString() throws Exception {
782+
String[] fixture = {
783+
"deCamelize",
784+
"de-Camelize",
785+
"de camelize",
786+
"de camelize",
787+
"de Camelize",
788+
"de-camelize",
789+
"-de--camelize",
790+
"de_camelize",
791+
" de_camelize"
792+
};
793+
794+
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toSnakeCase(%s) should be de_camelize", el), toSnakeCase(el), equalTo("de_camelize")));
795+
}
796+
797+
779798
}

0 commit comments

Comments
 (0)