Skip to content

Commit d65bb14

Browse files
committed
Add support for the dropRight().
1 parent 91d770a commit d65bb14

File tree

3 files changed

+57
-8
lines changed
  • lodash-plugin/src
  • src/main/java/com/github/underscore

3 files changed

+57
-8
lines changed

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public Chain<List<T>> drop() {
5454
public Chain<List<T>> drop(final Integer n) {
5555
return new Chain<List<T>>($.drop(value(), n));
5656
}
57+
58+
public Chain<List<T>> dropRight() {
59+
return new Chain<List<T>>($.dropRight(value()));
60+
}
61+
62+
public Chain<List<T>> dropRight(final Integer n) {
63+
return new Chain<List<T>>($.dropRight(value(), n));
64+
}
5765
}
5866

5967
public static Chain chain(final String item) {
@@ -72,12 +80,12 @@ public static <T> Chain chain(final T ... list) {
7280
return new $.Chain<T>(Arrays.asList(list));
7381
}
7482

75-
public static <T> List<List<T>> chunk(final Iterable<T> list, final Integer size) {
83+
public static <T> List<List<T>> chunk(final Iterable<T> iterable, final Integer size) {
7684
int index = 0;
77-
int length = size(list);
85+
int length = size(iterable);
7886
final List<List<T>> result = new ArrayList<List<T>>(length / size);
7987
while (index < length) {
80-
result.add(newArrayList(list).subList(index, Math.min(length, index + size)));
88+
result.add(newArrayList(iterable).subList(index, Math.min(length, index + size)));
8189
index += size;
8290
}
8391
return result;
@@ -87,22 +95,38 @@ public List<List<T>> chunk(final Integer size) {
8795
return chunk(getIterable(), size);
8896
}
8997

90-
public static <T> List<T> drop(final Iterable<T> list) {
91-
return rest(newArrayList(list));
98+
public static <T> List<T> drop(final Iterable<T> iterable) {
99+
return rest(newArrayList(iterable));
92100
}
93101

94102
public List<T> drop() {
95103
return drop(getIterable());
96104
}
97105

98-
public static <T> List<T> drop(final Iterable<T> list, final Integer n) {
99-
return rest(newArrayList(list), n);
106+
public static <T> List<T> drop(final Iterable<T> iterable, final Integer n) {
107+
return rest(newArrayList(iterable), n);
100108
}
101109

102110
public List<T> drop(final Integer n) {
103111
return drop(getIterable(), n);
104112
}
105113

114+
public static <T> List<T> dropRight(final Iterable<T> iterable) {
115+
return initial(newArrayList(iterable));
116+
}
117+
118+
public List<T> dropRight() {
119+
return dropRight(getIterable());
120+
}
121+
122+
public static <T> List<T> dropRight(final Iterable<T> iterable, final Integer n) {
123+
return initial(newArrayList(iterable), n);
124+
}
125+
126+
public List<T> dropRight(final Integer n) {
127+
return dropRight(getIterable(), n);
128+
}
129+
106130
public static void main(String ... args) {
107131
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
108132
+ "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
@@ -75,6 +75,31 @@ public void drop() {
7575
assertEquals("[1, 2, 3]", $.drop(asList(1, 2, 3), 0).toString());
7676
}
7777

78+
/*
79+
_.dropRight([1, 2, 3]);
80+
// → [1, 2]
81+
82+
_.dropRight([1, 2, 3], 2);
83+
// → [1]
84+
85+
_.dropRight([1, 2, 3], 5);
86+
// → []
87+
88+
_.dropRight([1, 2, 3], 0);
89+
// → [1, 2, 3]
90+
*/
91+
@Test
92+
public void dropRight() {
93+
assertEquals("[1, 2]", $.dropRight(asList(1, 2, 3)).toString());
94+
assertEquals("[1, 2]", new $(asList(1, 2, 3)).dropRight().toString());
95+
assertEquals("[1, 2]", $.chain(asList(1, 2, 3)).dropRight().toString());
96+
assertEquals("[1]", $.dropRight(asList(1, 2, 3), 2).toString());
97+
assertEquals("[1]", new $(asList(1, 2, 3)).dropRight(2).toString());
98+
assertEquals("[1]", $.chain(asList(1, 2, 3)).dropRight(2).toString());
99+
assertEquals("[]", $.dropRight(asList(1, 2, 3), 5).toString());
100+
assertEquals("[1, 2, 3]", $.dropRight(asList(1, 2, 3), 0).toString());
101+
}
102+
78103
@Test
79104
public void main() {
80105
$.main(new String[] {});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ public static <E> List<E> initial(final List<E> list) {
621621
}
622622

623623
public static <E> List<E> initial(final List<E> list, final int n) {
624-
return list.subList(0, list.size() - n);
624+
return list.subList(0, Math.max(0, list.size() - n));
625625
}
626626

627627
public static <E> E[] initial(final E[] array) {

0 commit comments

Comments
 (0)