Support numbers and boolean in join (fix #930)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Tolnay · committed by David Tolnay · parent 8eb1367 · Browse files · 3 files·+31 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−6 removed
- tests:+20 added−4 removed
- other:+11 added−2 removed
Provenance
- Commit
e17ccf2- Tree
4ad441a- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
8eb1367
3 files changed+31 added−6 removed
modified:docs/content/3.manual/manual.yml+7 added-1 removed
1444 unmodified lines1445 argument as separator. It is the inverse of `split`: that is,1446 running `split("foo") | join("foo")` over any input string1447 returns said input string.1448 1449+ Numbers and booleans in the input are converted to strings.1450+ Null values are treated as empty strings. Arrays and objects1451+ in the input are not supported.1452+1453 examples:1454 - program: 'join(", ")'1455 input: '["a","b,c,d","e"]'1456 output: ['"a, b,c,d, e"']1457+ - program: 'join(" ")'1458+ input: '["a",1,2.3,true,null,false]'1459+ output: ['"a 1 2.3 true false"']1460 1454-1461 - title: "`ascii_downcase`, `ascii_upcase`"1462 body: |1463 1464 Emit a copy of the input string with its alphabetic characters (a-z and A-Z)1489 unmodified linesmodified:src/builtin.jq+4 added-1 removed
57 unmodified lines58 def values: select(. != null);59 def scalars: select(. == null or . == true or . == false or type == "number" or type == "string");60 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));61 def leaf_paths: paths(scalars);62-def join($x): reduce .[] as $i (null; (.//"") + (if . == null then $i else $x + $i end))//"";62+def join($x): reduce .[] as $i (null;63+ (if .==null then "" else .+$x end) +64+ ($i | if type=="boolean" or type=="number" then tostring else .//"" end)65+ ) // "";66 def _flatten($x): reduce .[] as $i ([]; if $i | type == "array" and $x != 0 then . + ($i | _flatten($x-1)) else . + [$i] end);67 def flatten($x): if $x < 0 then error("flatten depth must not be negative") else _flatten($x) end;68 def flatten: _flatten(-1);69 def range($x): range(0;$x);220 unmodified linesmodified:tests/jq.test+20 added-4 removed
1257 unmodified lines1258 try -. catch .1259 "very-long-string"1260 "string (\"very-long-...) cannot be negated"1261 1262-try join(",") catch .1263-["1",2]1264-"string (\",\") and number (2) cannot be added"1262+join(",")1263+["1",2,true,false,3.4]1264+"1,2,true,false,3.4"1265 1266+.[] | join(",")1267+[[], [null], [null,null], [null,null,null]]1268+""1269+""1270+","1271+",,"1272+1273+.[] | join(",")1274+[["a",null], [null,"a"]]1275+"a,"1276+",a"1277+1278 try join(",") catch .1279 ["1","2",{"a":{"b":{"c":33}}}]1268-"string (\",\") and object ({\"a\":{\"b\":{...) cannot be added"1280+"string (\"1,2,\") and object ({\"a\":{\"b\":{...) cannot be added"1281 1282+try join(",") catch .1283+["1","2",[3,4,5]]1284+"string (\"1,2,\") and array ([3,4,5]) cannot be added"1285+1286 {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}1287 null1288 {"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}1289 28 unmodified lines