mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-11-28 02:03:45 +00:00
Added support for collapseWhitespace function
This commit is contained in:
@@ -66,6 +66,16 @@ char[] result = chars(title);
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## collapseWhitespace(value)
|
||||||
|
|
||||||
|
Replace consecutive whitespace characters with a single space.
|
||||||
|
|
||||||
|
```java
|
||||||
|
final String input = " foo bar ";
|
||||||
|
final String result = collapseWhitespace(title);
|
||||||
|
// result = "foo bar";
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Other functions that can be added
|
## Other functions that can be added
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package strman;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,6 +11,10 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public interface Strman {
|
public interface Strman {
|
||||||
|
|
||||||
|
Predicate<String> NULL_STRING_PREDICATE = str -> str == null;
|
||||||
|
Predicate<String> EMPTY_STRING_PREDICATE = str -> str.length() == 0;
|
||||||
|
Predicate<String> NULL_AND_EMPTY_STRING_PREDICATE = NULL_STRING_PREDICATE.and(EMPTY_STRING_PREDICATE);
|
||||||
|
String EMPTY_STRING = " ";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends Strings to value
|
* Appends Strings to value
|
||||||
@@ -30,7 +35,7 @@ public interface Strman {
|
|||||||
* @return full String
|
* @return full String
|
||||||
*/
|
*/
|
||||||
static String appendArray(final String value, final String[] appends) {
|
static String appendArray(final String value, final String[] appends) {
|
||||||
validate(value, () -> "'value' should be not null.");
|
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
|
||||||
if (appends == null || appends.length == 0) {
|
if (appends == null || appends.length == 0) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -71,20 +76,14 @@ public interface Strman {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static String[] between(final String value, final String start, final String end) {
|
static String[] between(final String value, final String start, final String end) {
|
||||||
validate(value, () -> "'value' should be not null.");
|
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
|
||||||
validate(start, () -> "'start' should be not null.");
|
validate(start, NULL_STRING_PREDICATE, () -> "'start' should be not null.");
|
||||||
validate(end, () -> "'end' should be not null.");
|
validate(end, NULL_STRING_PREDICATE, () -> "'end' should be not null.");
|
||||||
|
|
||||||
String[] parts = value.split(end);
|
String[] parts = value.split(end);
|
||||||
return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new);
|
return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void validate(final String value, final Supplier<String> supplier) {
|
|
||||||
if (value == null) {
|
|
||||||
throw new IllegalArgumentException(supplier.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a String array consisting of the characters in the String.
|
* Returns a String array consisting of the characters in the String.
|
||||||
*
|
*
|
||||||
@@ -96,7 +95,7 @@ public interface Strman {
|
|||||||
* The other implementation of this could be using String's split method
|
* The other implementation of this could be using String's split method
|
||||||
* String[] chars = value.split("")
|
* String[] chars = value.split("")
|
||||||
*/
|
*/
|
||||||
validate(value, () -> "'value' should be not null.");
|
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
|
||||||
int length = value.length();
|
int length = value.length();
|
||||||
String[] result = new String[length];
|
String[] result = new String[length];
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
@@ -104,4 +103,22 @@ public interface Strman {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace consecutive whitespace characters with a single space.
|
||||||
|
*
|
||||||
|
* @param value input String
|
||||||
|
* @return collapsed String
|
||||||
|
*/
|
||||||
|
static String collapseWhitespace(final String value) {
|
||||||
|
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
|
||||||
|
return value.trim().replaceAll("\\s\\s+", " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
|
||||||
|
if (predicate.test(value)) {
|
||||||
|
throw new IllegalArgumentException(supplier.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package strman;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
@@ -12,31 +13,31 @@ import static strman.Strman.*;
|
|||||||
public class StrmanTest {
|
public class StrmanTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldAppendStringsToEndOfValue() throws Exception {
|
public void append_shouldAppendStringsToEndOfValue() throws Exception {
|
||||||
assertThat(append("f", "o", "o", "b", "a", "r"), equalTo("foobar"));
|
assertThat(append("f", "o", "o", "b", "a", "r"), equalTo("foobar"));
|
||||||
assertThat(append("foobar"), equalTo("foobar"));
|
assertThat(append("foobar"), equalTo("foobar"));
|
||||||
assertThat(append("", "foobar"), equalTo("foobar"));
|
assertThat(append("", "foobar"), equalTo("foobar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void appendShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
|
public void append_shouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
|
||||||
append(null);
|
append(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldAppendStringArrayToEndOfValue() throws Exception {
|
public void appendArray_shouldAppendStringArrayToEndOfValue() throws Exception {
|
||||||
assertThat(appendArray("f", new String[]{"o", "o", "b", "a", "r"}), equalTo("foobar"));
|
assertThat(appendArray("f", new String[]{"o", "o", "b", "a", "r"}), equalTo("foobar"));
|
||||||
assertThat(appendArray("foobar", new String[]{}), equalTo("foobar"));
|
assertThat(appendArray("foobar", new String[]{}), equalTo("foobar"));
|
||||||
assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar"));
|
assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void appendArrayShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
|
public void appendArray_ShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
|
||||||
appendArray(null, new String[]{});
|
appendArray(null, new String[]{});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFindCharacterAtIndex() throws Exception {
|
public void at_shouldFindCharacterAtIndex() throws Exception {
|
||||||
assertThat(at("foobar", 0), equalTo(Optional.of("f")));
|
assertThat(at("foobar", 0), equalTo(Optional.of("f")));
|
||||||
assertThat(at("foobar", 1), equalTo(Optional.of("o")));
|
assertThat(at("foobar", 1), equalTo(Optional.of("o")));
|
||||||
assertThat(at("foobar", -1), equalTo(Optional.of("r")));
|
assertThat(at("foobar", -1), equalTo(Optional.of("r")));
|
||||||
@@ -46,21 +47,38 @@ public class StrmanTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception {
|
public void between_shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception {
|
||||||
assertThat(between("[abc][def]", "[", "]"), arrayContaining("abc", "def"));
|
assertThat(between("[abc][def]", "[", "]"), arrayContaining("abc", "def"));
|
||||||
assertThat(between("<span>foo</span>", "<span>", "</span>"), arrayContaining("foo"));
|
assertThat(between("<span>foo</span>", "<span>", "</span>"), arrayContaining("foo"));
|
||||||
assertThat(between("<span>foo</span><span>bar</span>", "<span>", "</span>"), arrayContaining("foo", "bar"));
|
assertThat(between("<span>foo</span><span>bar</span>", "<span>", "</span>"), arrayContaining("foo", "bar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void betweenShouldReturnFullStringWhenStartAndEndDoesNotExist() throws Exception {
|
public void between_shouldReturnFullStringWhenStartAndEndDoesNotExist() throws Exception {
|
||||||
assertThat(between("[abc][def]", "{", "}"), arrayContaining("[abc][def]"));
|
assertThat(between("[abc][def]", "{", "}"), arrayContaining("[abc][def]"));
|
||||||
assertThat(between("", "{", "}"), arrayContaining(""));
|
assertThat(between("", "{", "}"), arrayContaining(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void charsShouldReturnAllCharactersInString() throws Exception {
|
public void chars_shouldReturnAllCharactersInString() throws Exception {
|
||||||
final String title = "title";
|
final String title = "title";
|
||||||
assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"}));
|
assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceWithSingleSpace() throws Exception {
|
||||||
|
String[] fixture = {
|
||||||
|
"foo bar",
|
||||||
|
" foo bar ",
|
||||||
|
" foo bar ",
|
||||||
|
" foo bar "
|
||||||
|
};
|
||||||
|
Arrays.stream(fixture).forEach(el -> assertThat(collapseWhitespace(el), equalTo("foo bar")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceBetweenMultipleStrings() throws Exception {
|
||||||
|
String input = " foo bar bazz hello world ";
|
||||||
|
assertThat(collapseWhitespace(input), equalTo("foo bar bazz hello world"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user