From 6c31850e86bfe482bdaff8bc858864be2c7f4c75 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 17 Oct 2015 21:15:24 -0700 Subject: [PATCH] If-then-else documentation for forking conditional (fix #942) --- docs/content/3.manual/manual.yml | 6 +++--- docs/content/3.manual/v1.3/manual.yml | 6 +++--- docs/content/3.manual/v1.4/manual.yml | 6 +++--- docs/content/3.manual/v1.5/manual.yml | 6 +++--- tests/jq.test | 15 ++++++++++++++- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index d9dd3b18..f7fd9381 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -1863,9 +1863,9 @@ sections: need something more like `if (.name | length) > 0 then A else B end` instead. - If the condition A produces multiple results, it is - considered "true" if any of those results is not false or - null. If it produces zero results, it's considered false. + If the condition `A` produces multiple results, then `B` is evaluated + once for each result that is not false or null, and `C` is evaluated + once for each false or null. More cases can be added to an if using `elif A then B` syntax. diff --git a/docs/content/3.manual/v1.3/manual.yml b/docs/content/3.manual/v1.3/manual.yml index aaedf377..08603207 100644 --- a/docs/content/3.manual/v1.3/manual.yml +++ b/docs/content/3.manual/v1.3/manual.yml @@ -913,9 +913,9 @@ sections: need something more like `if (.name | count) > 0 then A else B end` instead. - If the condition A produces multiple results, it is - considered "true" if any of those results is not false or - null. If it produces zero results, it's considered false. + If the condition `A` produces multiple results, then `B` is evaluated + once for each result that is not false or null, and `C` is evaluated + once for each false or null. More cases can be added to an if using `elif A then B` syntax. diff --git a/docs/content/3.manual/v1.4/manual.yml b/docs/content/3.manual/v1.4/manual.yml index 841764ac..41745888 100644 --- a/docs/content/3.manual/v1.4/manual.yml +++ b/docs/content/3.manual/v1.4/manual.yml @@ -1315,9 +1315,9 @@ sections: need something more like `if (.name | length) > 0 then A else B end` instead. - If the condition A produces multiple results, it is - considered "true" if any of those results is not false or - null. If it produces zero results, it's considered false. + If the condition `A` produces multiple results, then `B` is evaluated + once for each result that is not false or null, and `C` is evaluated + once for each false or null. More cases can be added to an if using `elif A then B` syntax. diff --git a/docs/content/3.manual/v1.5/manual.yml b/docs/content/3.manual/v1.5/manual.yml index f676e52d..1b37e799 100644 --- a/docs/content/3.manual/v1.5/manual.yml +++ b/docs/content/3.manual/v1.5/manual.yml @@ -1794,9 +1794,9 @@ sections: need something more like `if (.name | length) > 0 then A else B end` instead. - If the condition A produces multiple results, it is - considered "true" if any of those results is not false or - null. If it produces zero results, it's considered false. + If the condition `A` produces multiple results, then `B` is evaluated + once for each result that is not false or null, and `C` is evaluated + once for each false or null. More cases can be added to an if using `elif A then B` syntax. diff --git a/tests/jq.test b/tests/jq.test index 879de162..46a7bc29 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -867,8 +867,21 @@ try (def x: reverse; x=10) catch . [{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}] ["yep","yep","yep","yep","nope","nope","yep","nope"] +[if 1,null,2 then 3 else 4 end] +null +[3,4,3] -# FIXME: define/test behaviour of 'if (.foo,.bar) then A else B end' +[if empty then 3 else 4 end] +null +[] + +[if 1 then 3,4 else 5 end] +null +[3,4] + +[if null then 3 else 5,6 end] +null +[5,6] [.[] | [.foo[] // .bar]] [{"foo":[1,2], "bar": 42}, {"foo":[1], "bar": null}, {"foo":[null,false,3], "bar": 18}, {"foo":[], "bar":42}, {"foo": [null,false,null], "bar": 41}]