Skip to content

Commit 096a375

Browse files
committed
Add support for the flattenDeep().
1 parent 4611e44 commit 096a375

File tree

2 files changed

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

2 files changed

+26
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public Chain<List<Object>> fill(Object value) {
7979
public Chain<List<Object>> fill(Object value, Integer start, Integer end) {
8080
return new Chain<List<Object>>($.fill((List<Object>) value(), value, start, end));
8181
}
82+
83+
public Chain<List<?>> flattenDeep() {
84+
return new Chain<List<?>>($.flattenDeep((List<?>) value()));
85+
}
8286
}
8387

8488
public static Chain chain(final String item) {
@@ -182,6 +186,14 @@ public List<Object> fill(Object value, Integer start, Integer end) {
182186
return fill((List<Object>) getIterable(), value, start, end);
183187
}
184188

189+
public static <E> List<E> flattenDeep(final List<?> list) {
190+
return flatten(list, false);
191+
}
192+
193+
public List<T> flattenDeep() {
194+
return flattenDeep((List<?>) getIterable());
195+
}
196+
185197
public static void main(String ... args) {
186198
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
187199
+ "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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ public void fill() {
187187
assertEquals("[4, *, 8]", array.toString());
188188
}
189189

190+
/*
191+
_.flattenDeep([1, [2, 3, [4]]]);
192+
// → [1, 2, 3, 4]
193+
*/
194+
@Test
195+
public void flattenDeep() {
196+
final List<Integer> result = $.flattenDeep(asList(1, asList(2, 3, asList(asList(4)))));
197+
assertEquals("[1, 2, 3, 4]", result.toString());
198+
final List<Integer> result2 = new $(asList(1, asList(2, 3, asList(asList(4))))).flattenDeep();
199+
assertEquals("[1, 2, 3, 4]", result2.toString());
200+
final List<Integer> resultChain = $.chain(asList(1, asList(2, 3, asList(asList(4))))).flattenDeep().value();
201+
assertEquals("[1, 2, 3, 4]", resultChain.toString());
202+
}
203+
190204
@Test
191205
public void main() {
192206
$.main(new String[] {});

0 commit comments

Comments
 (0)