Support numbers and boolean in join (fix #930)

This commit is contained in:
David Tolnay committed 2015-10-25 12:40:15 -07:00
1 parent 8eb1367ca4
commit e17ccf2297
3 files changed
+31 -6

No files matched your search

+7 -1
View File
@@ -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: |
+4 -1
View File
@@ -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);
+20 -4
View File
@@ -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