Skip to content

Commit b24a5eb

Browse files
committed
Added support for containsAny function
1 parent d3c0cac commit b24a5eb

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,16 @@ Verifies that all needles are contained in value.
103103
// result = false
104104
```
105105

106+
## containsAny(value, needles, caseSensitive)
106107

108+
Verifies that one or more of needles are contained in value.
107109

110+
```java
111+
String title = "foo bar";
112+
String needles = ["foo", "bar", "test"];
113+
String result = containsAny(title, needles, true);
114+
// result = true
115+
```
108116

109117
## Other functions that can be added
110118

src/main/java/strman/Strman.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ static boolean contains(final String value, final String needle, final boolean c
151151
/**
152152
* Verifies that all needles are contained in value. The search is case insensitive
153153
*
154-
* @param value input String to search
155-
* @param needles needles to find
154+
* @param value input String to search
155+
* @param needles needles to find
156156
* @return true if all needles are found else false.
157157
*/
158-
static boolean containsAll(String value, String[] needles) {
158+
static boolean containsAll(final String value, final String[] needles) {
159159
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
160160
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false));
161161
}
@@ -168,8 +168,17 @@ static boolean containsAll(String value, String[] needles) {
168168
* @param caseSensitive true or false
169169
* @return true if all needles are found else false.
170170
*/
171-
static boolean containsAll(String value, String[] needles, boolean caseSensitive) {
171+
static boolean containsAll(final String value, final String[] needles, final boolean caseSensitive) {
172172
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
173173
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, caseSensitive));
174174
}
175+
176+
static boolean containsAny(final String value, final String[] needles) {
177+
return containsAny(value, needles, false);
178+
}
179+
180+
static boolean containsAny(final String value, final String[] needles, final boolean caseSensitive) {
181+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
182+
return Arrays.stream(needles).anyMatch(needle -> contains(value, needle, caseSensitive));
183+
}
175184
}

src/test/java/strman/StrmanTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,26 @@ public void containsAll_shouldReturnFalseOnlyWhenAllNeedlesAreNotContainedInValu
124124
"bar foo",
125125
"foobar",
126126
};
127-
128127
Arrays.stream(fixture).forEach(el -> assertFalse(containsAll(el, new String[]{"FOO", "bar"}, true)));
129128
}
130129

130+
@Test
131+
public void containsAny_shouldReturnTrueWhenAnyOfSearchNeedleExistInInputValue() throws Exception {
132+
String[] fixture = {
133+
"foo bar",
134+
"bar foo",
135+
"foobar",
136+
};
137+
Arrays.stream(fixture).forEach(el -> assertTrue(containsAny(el, new String[]{"foo", "bar", "test"})));
138+
}
139+
140+
@Test
141+
public void containsAny_shouldReturnFalseWhenNoneOfSearchNeedleExistInInputValue() throws Exception {
142+
String[] fixture = {
143+
"foo bar",
144+
"bar foo",
145+
"foobar",
146+
};
147+
Arrays.stream(fixture).forEach(el -> assertFalse(containsAny(el, new String[]{"FOO", "BAR", "Test"}, true)));
148+
}
131149
}

0 commit comments

Comments
 (0)