Skip to content

Commit f43b3b4

Browse files
committed
Add support for the dropWhile().
1 parent d65bb14 commit f43b3b4

File tree

2 files changed

+39
-0
lines changed
  • lodash-plugin/src

2 files changed

+39
-0
lines changed

lodash-plugin/src/main/java/com/github/underscore/lodash/$.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.github.underscore.lodash;
2525

2626
import java.util.*;
27+
import com.github.underscore.Predicate;
2728

2829
public class $<T> extends com.github.underscore.$<T> {
2930

@@ -62,6 +63,10 @@ public Chain<List<T>> dropRight() {
6263
public Chain<List<T>> dropRight(final Integer n) {
6364
return new Chain<List<T>>($.dropRight(value(), n));
6465
}
66+
67+
public Chain<List<T>> dropWhile(final Predicate<T> pred) {
68+
return new Chain<List<T>>($.dropWhile(value(), pred));
69+
}
6570
}
6671

6772
public static Chain chain(final String item) {
@@ -127,6 +132,14 @@ public List<T> dropRight(final Integer n) {
127132
return dropRight(getIterable(), n);
128133
}
129134

135+
public static <T> List<T> dropWhile(final Iterable<T> iterable, final Predicate<T> pred) {
136+
return rest(newArrayList(iterable), findIndex(newArrayList(iterable), negate(pred)));
137+
}
138+
139+
public List<T> dropWhile(final Predicate<T> pred) {
140+
return dropWhile(getIterable(), pred);
141+
}
142+
130143
public static void main(String ... args) {
131144
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
132145
+ "For docs, license, tests, and downloads, see: http://javadev.github.io/underscore-java";

lodash-plugin/src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.*;
2727
import org.junit.Test;
28+
import com.github.underscore.Predicate;
2829
import static java.util.Arrays.asList;
2930
import static org.junit.Assert.assertEquals;
3031

@@ -100,6 +101,31 @@ public void dropRight() {
100101
assertEquals("[1, 2, 3]", $.dropRight(asList(1, 2, 3), 0).toString());
101102
}
102103

104+
/*
105+
_.dropWhile([1, 2, 3], function(n) {
106+
return n < 3;
107+
});
108+
// → [3]
109+
*/
110+
@Test
111+
public void dropWhile() {
112+
assertEquals("[3]", $.dropWhile(asList(1, 2, 3), new Predicate<Integer>() {
113+
public Boolean apply(Integer n) {
114+
return n < 3;
115+
}
116+
}).toString());
117+
assertEquals("[3]", new $(asList(1, 2, 3)).dropWhile(new Predicate<Integer>() {
118+
public Boolean apply(Integer n) {
119+
return n < 3;
120+
}
121+
}).toString());
122+
assertEquals("[3]", $.chain(asList(1, 2, 3)).dropWhile(new Predicate<Integer>() {
123+
public Boolean apply(Integer n) {
124+
return n < 3;
125+
}
126+
}).toString());
127+
}
128+
103129
@Test
104130
public void main() {
105131
$.main(new String[] {});

0 commit comments

Comments
 (0)