mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-11-06 06:44:43 +00:00
Added underscored method with tests and documentation
This commit is contained in:
@@ -631,6 +631,16 @@ isBlank("test")
|
|||||||
// result => false)
|
// result => false)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## underscored
|
||||||
|
|
||||||
|
underscored returns a string that represents the passed in string in all lowercase and underscores
|
||||||
|
between words.
|
||||||
|
|
||||||
|
```java
|
||||||
|
underscored("MozTransform")
|
||||||
|
// result => "moz_transform")
|
||||||
|
```
|
||||||
|
|
||||||
## Inspiration
|
## Inspiration
|
||||||
|
|
||||||
This library is inspired by [dleitee/strman](https://github.com/dleitee/strman).
|
This library is inspired by [dleitee/strman](https://github.com/dleitee/strman).
|
||||||
|
|||||||
@@ -1310,12 +1310,38 @@ public abstract class Strman {
|
|||||||
* Checks if string is empty. This is a null safe check and will return true when string is null.
|
* Checks if string is empty. This is a null safe check and will return true when string is null.
|
||||||
*
|
*
|
||||||
* @param input The input string
|
* @param input The input string
|
||||||
* @return true if string is null or empty
|
* @return true if input string is null or empty
|
||||||
*/
|
*/
|
||||||
public static boolean isBlank(String input) {
|
public static boolean isBlank(String input) {
|
||||||
return input == null || input.isEmpty();
|
return input == null || input.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes passed in string to all lower case and adds underscore between words.
|
||||||
|
*
|
||||||
|
* @param input The input string
|
||||||
|
* @return the input string in all lower case with underscores between words
|
||||||
|
*/
|
||||||
|
public static String underscored(String input) {
|
||||||
|
String result = null;
|
||||||
|
if (input != null) {
|
||||||
|
StringBuffer resultBuffer = new StringBuffer();
|
||||||
|
char[] inputArray = input.toCharArray();
|
||||||
|
for(int i = 0; i < inputArray.length; i++) {
|
||||||
|
char nextChar = inputArray[i];
|
||||||
|
if (Character.isUpperCase(nextChar)) {
|
||||||
|
//start new word
|
||||||
|
if (resultBuffer.length() != 0) {
|
||||||
|
resultBuffer.append("_");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resultBuffer.append(nextChar);
|
||||||
|
}
|
||||||
|
result = resultBuffer.toString().toLowerCase();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
|
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
|
||||||
if (predicate.test(value)) {
|
if (predicate.test(value)) {
|
||||||
throw new IllegalArgumentException(supplier.get());
|
throw new IllegalArgumentException(supplier.get());
|
||||||
|
|||||||
@@ -1040,4 +1040,20 @@ public class StrmanTest {
|
|||||||
public void isBlank_shouldReturnFalseIfNotEmpty() {
|
public void isBlank_shouldReturnFalseIfNotEmpty() {
|
||||||
assertFalse(isBlank("ac"));
|
assertFalse(isBlank("ac"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void underscored_shouldReturnUnderscoredString() {
|
||||||
|
assertThat(underscored("MozTransform"), equalTo("moz_transform"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void underscored_shouldReturnEmptyStringIfEmptyStringPassedIn() {
|
||||||
|
assertThat(underscored(""), equalTo(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void underscored_shouldReturnNullIfNullPassedIn() {
|
||||||
|
assertThat(underscored(null), equalTo(null));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user