Created
March 26, 2018 05:30
-
-
Save NeedPainkiller/ff8bb3ad29acde39a907b08f65a161e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static String nonWordsReg = "[\\W\\_]+"; | |
| private static String non = ""; | |
| private static String spaceReg = "\\s"; | |
| private static String space = " "; | |
| private static String upper = "(\\p{Ll})(\\p{Lu})"; | |
| private static String upperGroup = "$1 $2"; | |
| public Observable<String> camelCase(String source) { | |
| return lowerCase(source) | |
| .map(string -> replaceSpaceWithUpperCase(new StringBuilder(string)).toString()) | |
| .map(string -> string.replaceAll(spaceReg, non)); | |
| } | |
| public Observable<String> kebabCase(String source) { | |
| return Observable.just(source) | |
| .map(string -> string.replaceAll(upper, upperGroup)) | |
| .map(string -> string.replaceAll(nonWordsReg, space)) | |
| .map(String::trim) | |
| .map(string -> replaceSpaceWithLowerCase(new StringBuilder(string)).toString()) | |
| .map(string -> string.replaceAll(spaceReg, "-")) | |
| .map(string -> string.substring(0, 1).toUpperCase().concat(string.substring(1))) | |
| .map(String::toLowerCase); | |
| } | |
| public Observable<String> snakeCase(String source) { | |
| return Observable.just(source) | |
| .map(string -> string.replaceAll(upper, upperGroup)) | |
| .map(string -> string.replaceAll(nonWordsReg, space)) | |
| .map(String::trim) | |
| .map(string -> replaceSpaceWithLowerCase(new StringBuilder(string)).toString()) | |
| .map(string -> string.replaceAll(spaceReg, "_")) | |
| .map(string -> string.substring(0, 1).toUpperCase().concat(string.substring(1))) | |
| .map(String::toLowerCase); | |
| } | |
| public Observable<String> startCase(String source) { | |
| return Observable.just(source) | |
| .map(string -> string.replaceAll(upper, upperGroup)) | |
| .map(string -> string.replaceAll(nonWordsReg, space)) | |
| .map(String::trim) | |
| .map(string -> replaceSpaceWithUpperCase(new StringBuilder(string)).toString()) | |
| .map(string -> string.substring(0, 1).toUpperCase() + string.substring(1)); | |
| } | |
| private static StringBuilder replaceSpaceWithUpperCase(StringBuilder source) { | |
| for (int i = -1; (i = source.indexOf(space, i + 1)) != -1; i++) { | |
| source.setCharAt(i + 1, Character.toUpperCase(source.charAt(i + 1))); | |
| } | |
| return source; | |
| } | |
| private static StringBuilder replaceSpaceWithLowerCase(StringBuilder source) { | |
| for (int i = -1; (i = source.indexOf(space, i + 1)) != -1; i++) { | |
| source.setCharAt(i + 1, Character.toLowerCase(source.charAt(i + 1))); | |
| } | |
| return source; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment