Skip to content

Commit f79c7c3

Browse files
committed
Add support for the object method contains().
1 parent ef72e29 commit f79c7c3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ public Boolean apply(E e) {
371371
});
372372
}
373373

374+
public boolean contains(final T elem) {
375+
return contains(iterable, elem);
376+
}
377+
374378
public static <E> boolean contains(final Iterable<E> iterable, final E elem, final int fromIndex) {
375379
final List<E> list = newArrayList(iterable);
376380
return contains(list.subList(fromIndex, list.size()), elem);

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,44 @@ public Boolean apply(Integer item) {
554554
assertFalse(result2obj);
555555
}
556556

557+
/*
558+
_.some([1, 2, 3, 4], function(num) { return num % 2 === 0; }); // true
559+
_.some([1, 2, 3, 4], function(num) { return num === 5; }); // false
560+
*/
561+
@Test
562+
public void some() {
563+
final boolean result1 = $.some(asList(1, 2, 3, 4),
564+
new Predicate<Integer>() {
565+
public Boolean apply(Integer item) {
566+
return item % 2 == 0;
567+
}
568+
});
569+
final boolean result1obj = new $(asList(1, 2, 3, 4))
570+
.some(
571+
new Predicate<Integer>() {
572+
public Boolean apply(Integer item) {
573+
return item % 2 == 0;
574+
}
575+
});
576+
final boolean result2 = $.some(asList(1, 2, 3, 4),
577+
new Predicate<Integer>() {
578+
public Boolean apply(Integer item) {
579+
return item == 5;
580+
}
581+
});
582+
final boolean result2obj = new $(asList(1, 2, 3, 4))
583+
.some(
584+
new Predicate<Integer>() {
585+
public Boolean apply(Integer item) {
586+
return item == 5;
587+
}
588+
});
589+
assertTrue(result1);
590+
assertTrue(result1obj);
591+
assertFalse(result2);
592+
assertFalse(result2obj);
593+
}
594+
557595
/*
558596
_.include([1, 2, 3], 3); // true
559597
*/
@@ -571,6 +609,8 @@ public void include() {
571609
public void contains() {
572610
final boolean result = $.contains(asList(1, 2, 3), 3);
573611
assertTrue(result);
612+
final boolean resultObj = new $(asList(1, 2, 3)).contains(3);
613+
assertTrue(resultObj);
574614
final boolean resultChain = (Boolean) $.chain(asList(1, 2, 3)).contains(3).item();
575615
assertTrue(resultChain);
576616
final boolean result2 = $.contains(asList(1, 2, 3), 3, 1);

0 commit comments

Comments
 (0)