Skip to content

Commit ba27aff

Browse files
committed
Add support for the dropRightWhile().
1 parent f43b3b4 commit ba27aff

File tree

2 files changed

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

2 files changed

+38
-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
@@ -67,6 +67,10 @@ public Chain<List<T>> dropRight(final Integer n) {
6767
public Chain<List<T>> dropWhile(final Predicate<T> pred) {
6868
return new Chain<List<T>>($.dropWhile(value(), pred));
6969
}
70+
71+
public Chain<List<T>> dropRightWhile(final Predicate<T> pred) {
72+
return new Chain<List<T>>($.dropRightWhile(value(), pred));
73+
}
7074
}
7175

7276
public static Chain chain(final String item) {
@@ -140,6 +144,15 @@ public List<T> dropWhile(final Predicate<T> pred) {
140144
return dropWhile(getIterable(), pred);
141145
}
142146

147+
public static <T> List<T> dropRightWhile(final Iterable<T> iterable, final Predicate<T> pred) {
148+
final List<T> list = reverse(iterable);
149+
return rest(list, findIndex(list, negate(pred)));
150+
}
151+
152+
public List<T> dropRightWhile(final Predicate<T> pred) {
153+
return dropRightWhile(getIterable(), pred);
154+
}
155+
143156
public static void main(String ... args) {
144157
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
145158
+ "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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,31 @@ public Boolean apply(Integer n) {
126126
}).toString());
127127
}
128128

129+
/*
130+
_.dropRightWhile([1, 2, 3], function(n) {
131+
return n > 1;
132+
});
133+
// → [1]
134+
*/
135+
@Test
136+
public void dropRightWhile() {
137+
assertEquals("[1]", $.dropRightWhile(asList(1, 2, 3), new Predicate<Integer>() {
138+
public Boolean apply(Integer n) {
139+
return n > 1;
140+
}
141+
}).toString());
142+
assertEquals("[1]", new $(asList(1, 2, 3)).dropRightWhile(new Predicate<Integer>() {
143+
public Boolean apply(Integer n) {
144+
return n > 1;
145+
}
146+
}).toString());
147+
assertEquals("[1]", $.chain(asList(1, 2, 3)).dropRightWhile(new Predicate<Integer>() {
148+
public Boolean apply(Integer n) {
149+
return n > 1;
150+
}
151+
}).toString());
152+
}
153+
129154
@Test
130155
public void main() {
131156
$.main(new String[] {});

0 commit comments

Comments
 (0)