From 9a2736d267405e50f4b63a7fdabfd9c0e85c3787 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Tue, 7 Feb 2023 08:35:39 +0000 Subject: [PATCH] use `foreach` for extra indentation --- jq/gilded-rose.jq | 85 ++++++++++++++++++++++-------------------- jq/test-gilded-rose.sh | 2 +- jq/texttest_fixture.jq | 2 +- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/jq/gilded-rose.jq b/jq/gilded-rose.jq index 32689e2f..e4096d57 100644 --- a/jq/gilded-rose.jq +++ b/jq/gilded-rose.jq @@ -1,49 +1,54 @@ def update_quality: - if .name != "Aged Brie" and .name != "Backstage passes to a TAFKAL80ETC concert" then - if .quality > 0 then - if .name != "Sulfuras, Hand of Ragnaros" then - .quality = .quality - 1 - else . end - else . end - else - if .quality < 50 then - .quality = .quality + 1 - | - if .name == "Backstage passes to a TAFKAL80ETC concert" then - if .sell_in < 11 then - if .quality < 50 then - .quality = .quality + 1 - else . end - else . end - | - if .sell_in < 6 then - if .quality < 50 then - .quality = .quality + 1 - else . end - else . end - else . end - else . end - end - | - if .name != "Sulfuras, Hand of Ragnaros" then - .sell_in = .sell_in - 1 - else . end - | - if .sell_in < 0 then - if .name != "Aged Brie" then - if .name != "Backstage passes to a TAFKAL80ETC concert" then + [ + foreach .[] as $item ( + null; + $item | + if .name != "Aged Brie" and .name != "Backstage passes to a TAFKAL80ETC concert" then if .quality > 0 then if .name != "Sulfuras, Hand of Ragnaros" then .quality = .quality - 1 else . end else . end else - .quality = .quality - .quality + if .quality < 50 then + .quality = .quality + 1 + | + if .name == "Backstage passes to a TAFKAL80ETC concert" then + if .sell_in < 11 then + if .quality < 50 then + .quality = .quality + 1 + else . end + else . end + | + if .sell_in < 6 then + if .quality < 50 then + .quality = .quality + 1 + else . end + else . end + else . end + else . end end - else - if .quality < 50 then - .quality = .quality + 1 + | + if .name != "Sulfuras, Hand of Ragnaros" then + .sell_in = .sell_in - 1 else . end - end - else . end - ; + | + if .sell_in < 0 then + if .name != "Aged Brie" then + if .name != "Backstage passes to a TAFKAL80ETC concert" then + if .quality > 0 then + if .name != "Sulfuras, Hand of Ragnaros" then + .quality = .quality - 1 + else . end + else . end + else + .quality = .quality - .quality + end + else + if .quality < 50 then + .quality = .quality + 1 + else . end + end + else . end + ) + ]; diff --git a/jq/test-gilded-rose.sh b/jq/test-gilded-rose.sh index 2e32d49f..62d91240 100755 --- a/jq/test-gilded-rose.sh +++ b/jq/test-gilded-rose.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -jaq -en "$(cat gilded-rose.jq)"'{name:"foo",sell_in:0,quality:0} | update_quality | .name == "fixme"' +jaq -en "$(cat gilded-rose.jq)"'[{name:"foo",sell_in:0,quality:0}] | update_quality | .[].name == "fixme"' diff --git a/jq/texttest_fixture.jq b/jq/texttest_fixture.jq index 28fcd69e..477147bb 100644 --- a/jq/texttest_fixture.jq +++ b/jq/texttest_fixture.jq @@ -12,7 +12,7 @@ { name: "Conjured Mana Cake", sell_in: 3, quality: 6} # <-- :O ] | { items: ., day: 0 } | - recurse(.day += 1 | .items = [.items[] | update_quality]; .day < ($ARGS.named.days // 2 | tonumber)) | + recurse(.day += 1 | .items = (.items | update_quality); .day < ($ARGS.named.days // 2 | tonumber)) | ( "", (["-------- day ", (.day | tostring), " --------"] | add),