diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index df309c4a..baf474bf 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -1446,11 +1446,17 @@ sections: running `split("foo") | join("foo")` over any input string returns said input string. + Numbers and booleans in the input are converted to strings. + Null values are treated as empty strings. Arrays and objects + in the input are not supported. + examples: - program: 'join(", ")' input: '["a","b,c,d","e"]' output: ['"a, b,c,d, e"'] - + - program: 'join(" ")' + input: '["a",1,2.3,true,null,false]' + output: ['"a 1 2.3 true false"'] - title: "`ascii_downcase`, `ascii_upcase`" body: | diff --git a/src/builtin.jq b/src/builtin.jq index b81467fe..4add6813 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -59,7 +59,10 @@ def values: select(. != null); def scalars: select(. == null or . == true or . == false or type == "number" or type == "string"); def scalars_or_empty: select(. == null or . == true or . == false or type == "number" or type == "string" or ((type=="array" or type=="object") and length==0)); def leaf_paths: paths(scalars); -def join($x): reduce .[] as $i (null; (.//"") + (if . == null then $i else $x + $i end))//""; +def join($x): reduce .[] as $i (null; + (if .==null then "" else .+$x end) + + ($i | if type=="boolean" or type=="number" then tostring else .//"" end) + ) // ""; def _flatten($x): reduce .[] as $i ([]; if $i | type == "array" and $x != 0 then . + ($i | _flatten($x-1)) else . + [$i] end); def flatten($x): if $x < 0 then error("flatten depth must not be negative") else _flatten($x) end; def flatten: _flatten(-1); diff --git a/tests/jq.test b/tests/jq.test index 46a7bc29..4ca1f66b 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1259,13 +1259,29 @@ try -. catch . "very-long-string" "string (\"very-long-...) cannot be negated" -try join(",") catch . -["1",2] -"string (\",\") and number (2) cannot be added" +join(",") +["1",2,true,false,3.4] +"1,2,true,false,3.4" + +.[] | join(",") +[[], [null], [null,null], [null,null,null]] +"" +"" +"," +",," + +.[] | join(",") +[["a",null], [null,"a"]] +"a," +",a" try join(",") catch . ["1","2",{"a":{"b":{"c":33}}}] -"string (\",\") and object ({\"a\":{\"b\":{...) cannot be added" +"string (\"1,2,\") and object ({\"a\":{\"b\":{...) cannot be added" + +try join(",") catch . +["1","2",[3,4,5]] +"string (\"1,2,\") and array ([3,4,5]) cannot be added" {if:0,and:1,or:2,then:3,else:4,elif:5,end:6,as:7,def:8,reduce:9,foreach:10,try:11,catch:12,label:13,import:14,include:15,module:16} null