Skip to content

Commit

Permalink
Sync with underscore-java.
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Mar 31, 2020
1 parent 0241a4c commit 489aebd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,19 @@ public boolean contains(final T elem) {
return contains(iterable, elem);
}

public static <E> boolean containsWith(final Iterable<E> iterable, final E elem) {
return some(iterable, new Predicate<E>() {
@Override
public boolean test(E e) {
return elem == null ? e == null : String.valueOf(e).contains(String.valueOf(elem));
}
});
}

public boolean containsWith(final T elem) {
return containsWith(iterable, elem);
}

public static <E> boolean contains(final Iterable<E> iterable, final E elem, final int fromIndex) {
final List<E> list = newArrayList(iterable);
return contains(list.subList(fromIndex, list.size()), elem);
Expand Down Expand Up @@ -2957,6 +2970,10 @@ public Chain<Boolean> contains(final T elem) {
return new Chain<Boolean>(U.contains(list, elem));
}

public Chain<Boolean> containsWith(final T elem) {
return new Chain<Boolean>(U.containsWith(list, elem));
}

public Chain<T> invoke(final String methodName, final List<Object> args) {
return new Chain<T>(U.invoke(list, methodName, args));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/github/underscore/lodash/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ public Chain<Boolean> contains(final T elem) {
return new Chain<Boolean>(U.contains(value(), elem));
}

public Chain<Boolean> containsWith(final T elem) {
return new Chain<Boolean>(U.containsWith(value(), elem));
}

public Chain<T> invoke(final String methodName, final List<Object> args) {
return new Chain<T>(U.invoke(value(), methodName, args));
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/github/underscore/CollectionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,24 @@ public void contains() {
assertTrue(result4);
}

/*
_.containsWith(["abc", "bcd", "cde"], "bc");
=> true
*/
@Test
public void containsWith() {
final boolean result = U.containsWith(asList(1, 2, 3), 3);
assertTrue(result);
final boolean resultObj = new U<Integer>(asList(1, 2, 3)).containsWith(3);
assertTrue(resultObj);
final boolean resultChain = U.chain(asList(1, 2, 3)).containsWith(3).item();
assertTrue(resultChain);
final boolean result2 = U.containsWith(asList(1, 2, null), null);
assertTrue(result2);
final boolean result3 = U.containsWith(asList("abc", "bcd", "cde"), "bc");
assertTrue(result3);
}

@Test
public void containsAtLeast() {
final boolean result = U.containsAtLeast(asList(1, 2, 2), 2, 2);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/github/underscore/lodash/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ public void accept(String str) {
U.chain(new String[] {""}).count(new Predicate<String>() {
public boolean test(String str) { return true; } });
U.chain(new String[] {""}).contains("");
U.chain(new String[] {""}).containsWith("");
U.chain(new String[] {""}).invoke("toString", Collections.emptyList());
U.chain(new String[] {""}).invoke("toString");
U.chain(new String[] {""}).pluck("toString");
Expand Down

0 comments on commit 489aebd

Please sign in to comment.