Repository: MiSawa/xq (rust)

Issue:
flatten/1 on empty array emits null instead of []
The flatten filter on empty array should emit an empty array.
```sh
❯ jq -n '[] | flatten(0)'
[]

❯ xq -n '[] | flatten(0)'
null

❯ xq --version
xq 0.2.10-ad33a92d252fc4842f10730973e16f5da8434035
```

Pull request:
Fix flatten/1 on empty array
Fix #98 

Patch:
diff --git a/prelude.jq b/prelude.jq
index d7818c0..51c1424 100644
--- a/prelude.jq
+++ b/prelude.jq
@@ -70,7 +70,7 @@ def unique_by(f): def __yes_i_know_this_is_bad:.; [group_by(f)[] | .[0]];
 def unique: unique_by(.);
 
 def flatten: if type | . == "array" or . == "object" then [.[] | flatten] | reduce .[] as $v ([]; . + $v) else [.] end;
-def flatten($d): if ($d >= 0) and (type | . == "array" or . == "object") then [.[] | flatten($d-1)] | add else [.] end;
+def flatten($d): if ($d >= 0) and (type | . == "array" or . == "object") then [.[] | flatten($d-1)] | reduce .[] as $v ([]; . + $v) else [.] end;
 
 def index($s): def __yes_i_know_this_is_bad:.; indices($s)[0];
 def rindex($s): def __yes_i_know_this_is_bad:.; indices($s)[-1];


Test patch:
diff --git a/tests/hand_written/mod.rs b/tests/hand_written/mod.rs
index ea1c28c..da44901 100644
--- a/tests/hand_written/mod.rs
+++ b/tests/hand_written/mod.rs
@@ -666,3 +666,20 @@ test!(
     {"a":2,"b":3}
     "#
 );
+
+test!(
+    empty_flatten,
+    r#"
+    flatten(0, 1)
+    "#,
+    r#"
+    []
+    [[], []]
+    "#,
+    r#"
+    []
+    []
+    [[],[]]
+    []
+    "#
+);