Skip to content

Commit 00ffd4b

Browse files
committed
Add support for the takeRight().
1 parent 143074e commit 00ffd4b

File tree

3 files changed

+50
-1
lines changed
  • lodash-plugin/src
  • src/main/java/com/github/underscore

3 files changed

+50
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,18 @@ public Chain<List<T>> take() {
100100
return new Chain<List<T>>($.take(value()));
101101
}
102102

103+
public Chain<List<T>> takeRight() {
104+
return new Chain<List<T>>($.takeRight(value()));
105+
}
106+
103107
public Chain<List<T>> take(final Integer n) {
104108
return new Chain<List<T>>($.take(value(), n));
105109
}
106110

111+
public Chain<List<T>> takeRight(final Integer n) {
112+
return new Chain<List<T>>($.takeRight(value(), n));
113+
}
114+
107115
public Chain<List<T>> xor(final List<T> list) {
108116
return new Chain<List<T>>($.xor(value(), list));
109117
}
@@ -276,6 +284,14 @@ public List<T> take() {
276284
return take(getIterable());
277285
}
278286

287+
public static <T> List<T> takeRight(final Iterable<T> iterable) {
288+
return last(newArrayList(iterable), 1);
289+
}
290+
291+
public List<T> takeRight() {
292+
return takeRight(getIterable());
293+
}
294+
279295
public static <T> List<T> take(final Iterable<T> iterable, final Integer n) {
280296
return first(newArrayList(iterable), n);
281297
}
@@ -284,6 +300,14 @@ public List<T> take(final Integer n) {
284300
return take(getIterable(), n);
285301
}
286302

303+
public static <T> List<T> takeRight(final Iterable<T> iterable, final Integer n) {
304+
return last(newArrayList(iterable), n);
305+
}
306+
307+
public List<T> takeRight(final Integer n) {
308+
return takeRight(getIterable(), n);
309+
}
310+
287311
public static <T> List<T> xor(final List<T> ... lists) {
288312
int index = -1;
289313
int length = lists.length;

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
@@ -312,6 +312,31 @@ public void take() {
312312
assertEquals("[]", $.take(asList(1, 2, 3), 0).toString());
313313
}
314314

315+
/*
316+
_.takeRight([1, 2, 3]);
317+
// → [3]
318+
319+
_.takeRight([1, 2, 3], 2);
320+
// → [2, 3]
321+
322+
_.takeRight([1, 2, 3], 5);
323+
// → [1, 2, 3]
324+
325+
_.takeRight([1, 2, 3], 0);
326+
// → []
327+
*/
328+
@Test
329+
public void takeRight() {
330+
assertEquals("[3]", $.takeRight(asList(1, 2, 3)).toString());
331+
assertEquals("[3]", new $(asList(1, 2, 3)).takeRight().toString());
332+
assertEquals("[3]", $.chain(asList(1, 2, 3)).takeRight().value().toString());
333+
assertEquals("[2, 3]", $.takeRight(asList(1, 2, 3), 2).toString());
334+
assertEquals("[2, 3]", new $(asList(1, 2, 3)).takeRight(2).toString());
335+
assertEquals("[2, 3]", $.chain(asList(1, 2, 3)).takeRight(2).value().toString());
336+
assertEquals("[1, 2, 3]", $.takeRight(asList(1, 2, 3), 5).toString());
337+
assertEquals("[]", $.takeRight(asList(1, 2, 3), 0).toString());
338+
}
339+
315340
/*
316341
_.xor([1, 2], [4, 2]);
317342
// → [1, 4]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ public static <E> E last(final List<E> list) {
649649
}
650650

651651
public static <E> List<E> last(final List<E> list, final int n) {
652-
return list.subList(list.size() - n, list.size());
652+
return list.subList(Math.max(0, list.size() - n), list.size());
653653
}
654654

655655
public T last() {

0 commit comments

Comments
 (0)