Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,11 @@ The interpretation of `reduce`/`foreach` in jaq has the following advantages ove
When there is no more input value left,
in jq, `input` yields an error, whereas in jaq, it yields no output value.
* Joining:
When given an array `[x0, x1, ..., xn]`,
in jq, `join(x)` converts all elements of the input array to strings and intersperses them with `x`, whereas
in jaq, `join(x)` simply calculates `x0 + x + x1 + x + ... + xn`.
When all elements of the input array and `x` are strings, jq and jaq yield the same output.
When giving an array `[x1, ..., xn]` to `join($sep)`, jaq returns
`""` if the array is empty, otherwise `"\(x1)" + $sep + ... + $sep + "\(xn)"`;
that is, it concatenates the string representations of the array values interspersed with `$sep`.
Unlike jq, jaq does not map `null` values in the array to `""`,
nor does it reject array or object values in the array.



Expand Down
2 changes: 1 addition & 1 deletion jaq-std/src/defs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def map(f): [.[] | f];
def map_values(f): .[] |= f;
def add(f): reduce f as $x (null; . + $x);
def add: add(.[]);
def join(x): .[:-1][] += x | add;
def join($x): .[] |= tostring | .[:-1][] += $x | add + "";
def min_by(f): reduce min_by_or_empty(f) as $x (null; $x);
def max_by(f): reduce max_by_or_empty(f) as $x (null; $x);
def min: min_by(.);
Expand Down
5 changes: 2 additions & 3 deletions jaq-std/tests/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@ yields!(
false
);

yields!(join_empty, r#"[] | join(" ")"#, json!(null));
yields!(join_empty, r#"[] | join(" ")"#, "");
yields!(
join_strs,
r#"["Hello", "world"] | join(" ")"#,
"Hello world"
);
// 2 + 1 + 3 + 1 + 4 + 1 + 5
yields!(join_nums, r#"[2, 3, 4, 5] | join(1)"#, 17);
yields!(join_nums, r#"[2, 3, 4, 5] | join(",")"#, "2,3,4,5");

yields!(map, "[1, 2] | map(.+1)", [2, 3]);

Expand Down