Skip to content

Commit 55f8977

Browse files
committed
Add support for the pull().
1 parent 096a375 commit 55f8977

File tree

2 files changed

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

2 files changed

+39
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public Chain<List<Object>> fill(Object value, Integer start, Integer end) {
8383
public Chain<List<?>> flattenDeep() {
8484
return new Chain<List<?>>($.flattenDeep((List<?>) value()));
8585
}
86+
87+
public Chain<List<Object>> pull(Object ... values) {
88+
return new Chain<List<Object>>($.pull((List<Object>) value(), values));
89+
}
8690
}
8791

8892
public static Chain chain(final String item) {
@@ -194,6 +198,21 @@ public List<T> flattenDeep() {
194198
return flattenDeep((List<?>) getIterable());
195199
}
196200

201+
public static List<Object> pull(final List<Object> list, Object ... values) {
202+
final List<Object> valuesList = Arrays.asList(values);
203+
for (Iterator<Object> iterator = list.iterator(); iterator.hasNext(); ) {
204+
final Object object = iterator.next();
205+
if (valuesList.contains(object)) {
206+
iterator.remove();
207+
}
208+
}
209+
return list;
210+
}
211+
212+
public List<Object> pull(Object ... values) {
213+
return pull((List<Object>) getIterable(), values);
214+
}
215+
197216
public static void main(String ... args) {
198217
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
199218
+ "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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ public void flattenDeep() {
201201
assertEquals("[1, 2, 3, 4]", resultChain.toString());
202202
}
203203

204+
/*
205+
var array = [1, 2, 3, 1, 2, 3];
206+
207+
_.pull(array, 2, 3);
208+
console.log(array);
209+
// → [1, 1]
210+
*/
211+
@Test
212+
public void pull() {
213+
List<Object> array = new ArrayList<Object>(asList(1, 2, 3, 1, 2, 3));
214+
$.pull(array, 2, 3);
215+
assertEquals("[1, 1]", array.toString());
216+
array = new ArrayList<Object>(asList(1, 2, 3, 1, 2, 3));
217+
new $(array).pull(2, 3);
218+
assertEquals("[1, 1]", array.toString());
219+
array = new ArrayList<Object>(asList(1, 2, 3, 1, 2, 3));
220+
$.chain(array).pull(2, 3);
221+
assertEquals("[1, 1]", array.toString());
222+
}
223+
204224
@Test
205225
public void main() {
206226
$.main(new String[] {});

0 commit comments

Comments
 (0)