From d5b430fde96245fedab5a012643552234026ed81 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Fri, 5 Jan 2024 09:21:12 +0100 Subject: [PATCH] - r jq: jaq -> jq, rm foreach --- jq/README.md | 8 ++-- jq/gilded-rose.jq | 92 +++++++++++++++++++++--------------------- jq/test-gilded-rose.sh | 2 +- jq/texttest_fixture.jq | 2 +- 4 files changed, 51 insertions(+), 53 deletions(-) diff --git a/jq/README.md b/jq/README.md index a4dd4d82..1e823968 100644 --- a/jq/README.md +++ b/jq/README.md @@ -1,6 +1,6 @@ -# Installs +# Requirements -[install jaq](https://github.com/01mf02/jaq#installation) (latest development version) +jq # Failing Unit Test @@ -11,11 +11,11 @@ # TextTest Fixture ```shell -jaq -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)" +jq -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)" ``` Specify days (e.g. 10 days): ```shell -jaq --arg days 10 -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)" +jq --arg days 10 -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)" ``` diff --git a/jq/gilded-rose.jq b/jq/gilded-rose.jq index e4096d57..e9c7d941 100644 --- a/jq/gilded-rose.jq +++ b/jq/gilded-rose.jq @@ -1,54 +1,52 @@ def update_quality: [ - 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 + .[] + | + 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 + end + 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 + end + end + | + if .sell_in < 6 then + if .quality < 50 then + .quality = .quality + 1 + end + end + end + end + end + | + if .name != "Sulfuras, Hand of Ragnaros" then + .sell_in = .sell_in - 1 + 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 + end + end + else + .quality = .quality - .quality + 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 - 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 - ) + end + end ]; diff --git a/jq/test-gilded-rose.sh b/jq/test-gilded-rose.sh index 62d91240..b667d26f 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"' +jq -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 12c08dc3..9f153790 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), ("name, sellIn, quality"),