Skip to content

Commit 91d770a

Browse files
committed
Add support for the drop().
1 parent 49d21f3 commit 91d770a

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jdk:
77
- oraclejdk8
88

99
script:
10-
- mvn clean package jacoco:report coveralls:jacoco
10+
- mvn clean install jacoco:report coveralls:jacoco
1111
- mvn -f math-plugin/pom.xml clean package jacoco:report
1212
- mvn -f string-plugin/pom.xml clean package jacoco:report
1313
- mvn -f lodash-plugin/pom.xml clean package jacoco:report

lodash-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
<dependency>
203203
<groupId>com.github.javadev</groupId>
204204
<artifactId>underscore</artifactId>
205-
<version>1.3</version>
205+
<version>1.4-SNAPSHOT</version>
206206
</dependency>
207207
<dependency>
208208
<groupId>junit</groupId>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public Chain(final List<T> list) {
4646
public Chain<List<List<T>>> chunk(final Integer size) {
4747
return new Chain<List<List<T>>>($.chunk(value(), size));
4848
}
49+
50+
public Chain<List<T>> drop() {
51+
return new Chain<List<T>>($.drop(value()));
52+
}
53+
54+
public Chain<List<T>> drop(final Integer n) {
55+
return new Chain<List<T>>($.drop(value(), n));
56+
}
4957
}
5058

5159
public static Chain chain(final String item) {
@@ -79,6 +87,22 @@ public List<List<T>> chunk(final Integer size) {
7987
return chunk(getIterable(), size);
8088
}
8189

90+
public static <T> List<T> drop(final Iterable<T> list) {
91+
return rest(newArrayList(list));
92+
}
93+
94+
public List<T> drop() {
95+
return drop(getIterable());
96+
}
97+
98+
public static <T> List<T> drop(final Iterable<T> list, final Integer n) {
99+
return rest(newArrayList(list), n);
100+
}
101+
102+
public List<T> drop(final Integer n) {
103+
return drop(getIterable(), n);
104+
}
105+
82106
public static void main(String ... args) {
83107
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
84108
+ "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
@@ -50,6 +50,31 @@ public void chunk() {
5050
assertEquals("[[a, b, c], [d]]", $.chunk(asList("a", "b", "c", "d"), 3).toString());
5151
}
5252

53+
/*
54+
_.drop([1, 2, 3]);
55+
// → [2, 3]
56+
57+
_.drop([1, 2, 3], 2);
58+
// → [3]
59+
60+
_.drop([1, 2, 3], 5);
61+
// → []
62+
63+
_.drop([1, 2, 3], 0);
64+
// → [1, 2, 3]
65+
*/
66+
@Test
67+
public void drop() {
68+
assertEquals("[2, 3]", $.drop(asList(1, 2, 3)).toString());
69+
assertEquals("[2, 3]", new $(asList(1, 2, 3)).drop().toString());
70+
assertEquals("[2, 3]", $.chain(asList(1, 2, 3)).drop().toString());
71+
assertEquals("[3]", $.drop(asList(1, 2, 3), 2).toString());
72+
assertEquals("[3]", new $(asList(1, 2, 3)).drop(2).toString());
73+
assertEquals("[3]", $.chain(asList(1, 2, 3)).drop(2).toString());
74+
assertEquals("[]", $.drop(asList(1, 2, 3), 5).toString());
75+
assertEquals("[1, 2, 3]", $.drop(asList(1, 2, 3), 0).toString());
76+
}
77+
5378
@Test
5479
public void main() {
5580
$.main(new String[] {});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ public static <E> List<E> rest(final List<E> list) {
665665
}
666666

667667
public static <E> List<E> rest(final List<E> list, int n) {
668-
return list.subList(n, list.size());
668+
return list.subList(Math.min(n, list.size()), list.size());
669669
}
670670

671671
public static <E> E[] rest(final E[] array) {

0 commit comments

Comments
 (0)