diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index eabf7d8..96c6d86 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -660,6 +660,19 @@ public boolean contains(final T elem) { return contains(iterable, elem); } + public static boolean containsWith(final Iterable iterable, final E elem) { + return some(iterable, new Predicate() { + @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 boolean contains(final Iterable iterable, final E elem, final int fromIndex) { final List list = newArrayList(iterable); return contains(list.subList(fromIndex, list.size()), elem); @@ -2957,6 +2970,10 @@ public Chain contains(final T elem) { return new Chain(U.contains(list, elem)); } + public Chain containsWith(final T elem) { + return new Chain(U.containsWith(list, elem)); + } + public Chain invoke(final String methodName, final List args) { return new Chain(U.invoke(list, methodName, args)); } diff --git a/src/main/java/com/github/underscore/lodash/U.java b/src/main/java/com/github/underscore/lodash/U.java index 92210e0..29d3cb4 100644 --- a/src/main/java/com/github/underscore/lodash/U.java +++ b/src/main/java/com/github/underscore/lodash/U.java @@ -315,6 +315,10 @@ public Chain contains(final T elem) { return new Chain(U.contains(value(), elem)); } + public Chain containsWith(final T elem) { + return new Chain(U.containsWith(value(), elem)); + } + public Chain invoke(final String methodName, final List args) { return new Chain(U.invoke(value(), methodName, args)); } diff --git a/src/test/java/com/github/underscore/CollectionsTest.java b/src/test/java/com/github/underscore/CollectionsTest.java index 6500c38..fd04b0e 100644 --- a/src/test/java/com/github/underscore/CollectionsTest.java +++ b/src/test/java/com/github/underscore/CollectionsTest.java @@ -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(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); diff --git a/src/test/java/com/github/underscore/lodash/LodashTest.java b/src/test/java/com/github/underscore/lodash/LodashTest.java index b3a9ef8..437dea6 100644 --- a/src/test/java/com/github/underscore/lodash/LodashTest.java +++ b/src/test/java/com/github/underscore/lodash/LodashTest.java @@ -934,6 +934,7 @@ public void accept(String str) { U.chain(new String[] {""}).count(new Predicate() { 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");