Skip to content
Myr

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

3 files+31 added−6 removed

modified:docs/content/3.manual/manual.yml+7 added-1 removed
1444 unmodified lines
1445 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.14481449 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.14521453 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"']146014541461 - title: "`ascii_downcase`, `ascii_upcase`"1462 body: |14631464 Emit a copy of the input string with its alphabetic characters (a-z and A-Z)
1489 unmodified lines
modified:src/builtin.jq+4 added-1 removed
57 unmodified lines
58def values: select(. != null);59def scalars: select(. == null or . == true or . == false or type == "number" or type == "string");60def scalars_or_empty: select(. == null or . == true or . == false or type == "number" or type == "string" or ((type=="array" or type=="object") and length==0));61def leaf_paths: paths(scalars);62def join($x): reduce .[] as $i (null; (.//"") + (if . == null then $i else $x + $i end))//"";62def 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 ) // "";66def _flatten($x): reduce .[] as $i ([]; if $i | type == "array" and $x != 0 then . + ($i | _flatten($x-1)) else . + [$i] end);67def flatten($x): if $x < 0 then error("flatten depth must not be negative") else _flatten($x) end;68def flatten: _flatten(-1);69def range($x): range(0;$x);
220 unmodified lines
modified:tests/jq.test+20 added-4 removed
1257 unmodified lines
1258try -. catch .1259"very-long-string"1260"string (\"very-long-...) cannot be negated"12611262try join(",") catch .1263["1",2]1264"string (\",\") and number (2) cannot be added"1262join(",")1263["1",2,true,false,3.4]1264"1,2,true,false,3.4"12651266.[] | join(",")1267[[], [null], [null,null], [null,null,null]]1268""1269""1270","1271",,"12721273.[] | join(",")1274[["a",null], [null,"a"]]1275"a,"1276",a"12771278try 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"12811282try join(",") catch .1283["1","2",[3,4,5]]1284"string (\"1,2,\") and array ([3,4,5]) cannot be added"12851286{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}1287null1288{"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