Skip to content

Commit 143074e

Browse files
committed
Add support for the take().
1 parent 8bc163a commit 143074e

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
@@ -96,6 +96,14 @@ public Chain<List<T>> remove(final Predicate<T> pred) {
9696
return new Chain<List<T>>($.remove(value(), pred));
9797
}
9898

99+
public Chain<List<T>> take() {
100+
return new Chain<List<T>>($.take(value()));
101+
}
102+
103+
public Chain<List<T>> take(final Integer n) {
104+
return new Chain<List<T>>($.take(value(), n));
105+
}
106+
99107
public Chain<List<T>> xor(final List<T> list) {
100108
return new Chain<List<T>>($.xor(value(), list));
101109
}
@@ -260,6 +268,22 @@ public List<T> remove(final Predicate<T> pred) {
260268
return remove((List<T>) getIterable(), pred);
261269
}
262270

271+
public static <T> List<T> take(final Iterable<T> iterable) {
272+
return first(newArrayList(iterable), 1);
273+
}
274+
275+
public List<T> take() {
276+
return take(getIterable());
277+
}
278+
279+
public static <T> List<T> take(final Iterable<T> iterable, final Integer n) {
280+
return first(newArrayList(iterable), n);
281+
}
282+
283+
public List<T> take(final Integer n) {
284+
return take(getIterable(), n);
285+
}
286+
263287
public static <T> List<T> xor(final List<T> ... lists) {
264288
int index = -1;
265289
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
@@ -287,6 +287,31 @@ public Boolean apply(final Integer n) {
287287
assertEquals("[2, 4]", evens.toString());
288288
}
289289

290+
/*
291+
_.take([1, 2, 3]);
292+
// → [1]
293+
294+
_.take([1, 2, 3], 2);
295+
// → [1, 2]
296+
297+
_.take([1, 2, 3], 5);
298+
// → [1, 2, 3]
299+
300+
_.take([1, 2, 3], 0);
301+
// → []
302+
*/
303+
@Test
304+
public void take() {
305+
assertEquals("[1]", $.take(asList(1, 2, 3)).toString());
306+
assertEquals("[1]", new $(asList(1, 2, 3)).take().toString());
307+
assertEquals("[1]", $.chain(asList(1, 2, 3)).take().value().toString());
308+
assertEquals("[1, 2]", $.take(asList(1, 2, 3), 2).toString());
309+
assertEquals("[1, 2]", new $(asList(1, 2, 3)).take(2).toString());
310+
assertEquals("[1, 2]", $.chain(asList(1, 2, 3)).take(2).value().toString());
311+
assertEquals("[1, 2, 3]", $.take(asList(1, 2, 3), 5).toString());
312+
assertEquals("[]", $.take(asList(1, 2, 3), 0).toString());
313+
}
314+
290315
/*
291316
_.xor([1, 2], [4, 2]);
292317
// → [1, 4]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ public static <E> E first(final E[] array) {
585585
}
586586

587587
public static <E> List<E> first(final List<E> list, final int n) {
588-
return list.subList(0, n);
588+
return list.subList(0, Math.min(n, list.size()));
589589
}
590590

591591
public T first() {

0 commit comments

Comments
 (0)