diff --git a/rescript/bsconfig.json b/rescript/bsconfig.json index 0b355495..ba1378cf 100644 --- a/rescript/bsconfig.json +++ b/rescript/bsconfig.json @@ -6,7 +6,7 @@ "subdirs" : true }, "package-specs": { - "module": "es6", + "module": "commonjs", "in-source": true }, "suffix": ".bs.js", diff --git a/rescript/src/Demo.bs.js b/rescript/src/Demo.bs.js deleted file mode 100644 index 957c5c98..00000000 --- a/rescript/src/Demo.bs.js +++ /dev/null @@ -1,9 +0,0 @@ -// Generated by ReScript, PLEASE EDIT WITH CARE - - -console.log("Hello, ReScript"); - -export { - -} -/* Not a pure module */ diff --git a/rescript/src/Demo.res b/rescript/src/Demo.res deleted file mode 100644 index fcea153b..00000000 --- a/rescript/src/Demo.res +++ /dev/null @@ -1,2 +0,0 @@ - -Js.log("Hello, ReScript") \ No newline at end of file diff --git a/rescript/src/Demo_test.bs.js b/rescript/src/Demo_test.bs.js deleted file mode 100644 index f150754e..00000000 --- a/rescript/src/Demo_test.bs.js +++ /dev/null @@ -1,12 +0,0 @@ -// Generated by ReScript, PLEASE EDIT WITH CARE - -import * as Jest from "@glennsl/rescript-jest/src/jest.bs.js"; - -Jest.test("hey", (function (param) { - return Jest.Expect.toBe(Jest.Expect.expect(1), 1); - })); - -export { - -} -/* Not a pure module */ diff --git a/rescript/src/Demo_test.res b/rescript/src/Demo_test.res deleted file mode 100644 index 3fba7fb8..00000000 --- a/rescript/src/Demo_test.res +++ /dev/null @@ -1,6 +0,0 @@ -open Jest -open Expect - -test("hey", () => { - 1->expect->toBe(1) -}) diff --git a/rescript/src/GildedRose.bs.js b/rescript/src/GildedRose.bs.js new file mode 100644 index 00000000..f6446add --- /dev/null +++ b/rescript/src/GildedRose.bs.js @@ -0,0 +1,94 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + + +function make(name, sellIn, quality) { + return { + name: name, + sellIn: sellIn, + quality: quality + }; +} + +var Item = { + make: make +}; + +function updateQuality(items) { + return items.map(function (item) { + var newItem = item; + if (item.name !== "Aged Brie" && item.name !== "Backstage passes to a TAFKAL80ETC concert") { + if (item.quality > 0 && item.name !== "Sulfuras, Hand of Ragnaros") { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: item.quality - 1 | 0 + }; + } + + } else if (item.quality < 50) { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: item.quality + 1 | 0 + }; + if (item.name === "Backstage passes to a TAFKAL80ETC concert") { + if (item.sellIn < 11 && item.quality < 50) { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: item.quality + 1 | 0 + }; + } + if (item.sellIn < 6 && item.quality < 50) { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: item.quality + 1 | 0 + }; + } + + } + + } + if (item.name !== "Sulfuras, Hand of Ragnaros") { + newItem = { + name: item.name, + sellIn: item.sellIn - 1 | 0, + quality: item.quality + }; + } + if (item.sellIn < 0) { + if (item.name !== "Aged Brie") { + if (item.name !== "Backstage passes to a TAFKAL80ETC concert") { + if (item.quality > 0 && item.name !== "Sulfuras, Hand of Ragnaros") { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: item.quality - 1 | 0 + }; + } + + } else { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: 0 + }; + } + } else if (item.quality < 50) { + newItem = { + name: item.name, + sellIn: item.sellIn, + quality: item.quality + 1 | 0 + }; + } + + } + return newItem; + }); +} + +exports.Item = Item; +exports.updateQuality = updateQuality; +/* No side effect */ diff --git a/rescript/src/GildedRose.res b/rescript/src/GildedRose.res new file mode 100644 index 00000000..4af4758d --- /dev/null +++ b/rescript/src/GildedRose.res @@ -0,0 +1,65 @@ +module Item = { + type t = { + name: string, + sellIn: int, + quality: int, + } + + let make = (~name, ~sellIn, ~quality): t => { + name, + sellIn, + quality, + } +} + +let updateQuality = (items: array) => { + items->Js.Array2.map(item => { + let newItem = ref(item) + + if item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert" { + if item.quality > 0 { + if item.name != "Sulfuras, Hand of Ragnaros" { + newItem := {...item, quality: item.quality - 1} + } + } + } else if item.quality < 50 { + newItem := {...item, quality: item.quality + 1} + + if item.name == "Backstage passes to a TAFKAL80ETC concert" { + if item.sellIn < 11 { + if item.quality < 50 { + newItem := {...item, quality: item.quality + 1} + } + } + + if item.sellIn < 6 { + if item.quality < 50 { + newItem := {...item, quality: item.quality + 1} + } + } + } + } + + if item.name != "Sulfuras, Hand of Ragnaros" { + newItem := {...item, sellIn: item.sellIn - 1} + } + + if item.sellIn < 0 { + if item.name != "Aged Brie" { + if item.name != "Backstage passes to a TAFKAL80ETC concert" { + if item.quality > 0 { + if item.name != "Sulfuras, Hand of Ragnaros" { + newItem := {...item, quality: item.quality - 1} + } + } + } else { + newItem := {...item, quality: 0} + } + } else if item.quality < 50 { + newItem := {...item, quality: item.quality + 1} + } + } + + newItem.contents + }) +} diff --git a/rescript/src/GildedRose_test.bs.js b/rescript/src/GildedRose_test.bs.js new file mode 100644 index 00000000..522b02ac --- /dev/null +++ b/rescript/src/GildedRose_test.bs.js @@ -0,0 +1,20 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + +var Jest = require("@glennsl/rescript-jest/src/jest.bs.js"); +var Caml_array = require("rescript/lib/js/caml_array.js"); +var GildedRose = require("./GildedRose.bs.js"); + +Jest.describe("Gilded Rose", (function (param) { + Jest.test("should foo", (function (param) { + var items = [{ + name: "foo", + sellIn: 0, + quality: 0 + }]; + var updatedItems = GildedRose.updateQuality(items); + return Jest.Expect.toBe(Jest.Expect.expect(Caml_array.get(updatedItems, 0).name), "fixme"); + })); + })); + +/* Not a pure module */ diff --git a/rescript/src/GildedRose_test.res b/rescript/src/GildedRose_test.res new file mode 100644 index 00000000..6cf7c024 --- /dev/null +++ b/rescript/src/GildedRose_test.res @@ -0,0 +1,11 @@ +open Jest +open Expect +open GildedRose + +describe("Gilded Rose", () => { + test("should foo", () => { + let items: array = [{name: "foo", sellIn: 0, quality: 0}] + let updatedItems = updateQuality(items) + expect(updatedItems[0].name)->toBe("fixme") + }) +}) diff --git a/rescript/src/TextTest.bs.js b/rescript/src/TextTest.bs.js new file mode 100644 index 00000000..1f393649 --- /dev/null +++ b/rescript/src/TextTest.bs.js @@ -0,0 +1,42 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + +var Process = require("process"); +var Belt_Array = require("rescript/lib/js/belt_Array.js"); +var Caml_array = require("rescript/lib/js/caml_array.js"); +var GildedRose = require("./GildedRose.bs.js"); +var Belt_Option = require("rescript/lib/js/belt_Option.js"); +var Caml_format = require("rescript/lib/js/caml_format.js"); + +console.log("OMGHAI!"); + +var items = { + contents: [ + GildedRose.Item.make("+5 Dexterity Vest", 10, 20), + GildedRose.Item.make("Aged Brie", 2, 0), + GildedRose.Item.make("Elixir of the Mongoose", 5, 7), + GildedRose.Item.make("Sulfuras, Hand of Ragnaros", 0, 80), + GildedRose.Item.make("Sulfuras, Hand of Ragnaros", -1, 80), + GildedRose.Item.make("Backstage passes to a TAFKAL80ETC concert", 15, 20), + GildedRose.Item.make("Backstage passes to a TAFKAL80ETC concert", 10, 49), + GildedRose.Item.make("Backstage passes to a TAFKAL80ETC concert", 5, 49), + GildedRose.Item.make("Conjured Mana Cake", 3, 6) + ] +}; + +var days = Belt_Option.mapWithDefault(Belt_Array.get(Process.argv, 2), 31, Caml_format.int_of_string); + +for(var i = 0; i <= days; ++i){ + console.log("-------- day " + String(i) + " --------"); + console.log("name, sellIn, quality"); + for(var j = 0 ,j_finish = items.contents.length; j < j_finish; ++j){ + var item = Caml_array.get(items.contents, j); + console.log(item.name + ", " + String(item.sellIn) + ", " + String(item.quality)); + } + console.log(""); + items.contents = GildedRose.updateQuality(items.contents); +} + +exports.items = items; +exports.days = days; +/* Not a pure module */ diff --git a/rescript/src/TextTest.res b/rescript/src/TextTest.res new file mode 100644 index 00000000..b8f208cf --- /dev/null +++ b/rescript/src/TextTest.res @@ -0,0 +1,28 @@ +open GildedRose + +Js.log("OMGHAI!") + +let items: ref> = ref([ + Item.make(~name="+5 Dexterity Vest", ~sellIn=10, ~quality=20), + Item.make(~name="Aged Brie", ~sellIn=2, ~quality=0), + Item.make(~name="Elixir of the Mongoose", ~sellIn=5, ~quality=7), + Item.make(~name="Sulfuras, Hand of Ragnaros", ~sellIn=0, ~quality=80), + Item.make(~name="Sulfuras, Hand of Ragnaros", ~sellIn=-1, ~quality=80), + Item.make(~name="Backstage passes to a TAFKAL80ETC concert", ~sellIn=15, ~quality=20), + Item.make(~name="Backstage passes to a TAFKAL80ETC concert", ~sellIn=10, ~quality=49), + Item.make(~name="Backstage passes to a TAFKAL80ETC concert", ~sellIn=5, ~quality=49), + Item.make(~name="Conjured Mana Cake", ~sellIn=3, ~quality=6), +]) + +let days = Node.Process.argv->Belt.Array.get(2)->Belt.Option.mapWithDefault(31, int_of_string) + +for i in 0 to days { + Js.log("-------- day " ++ string_of_int(i) ++ " --------") + Js.log("name, sellIn, quality") + for j in 0 to Js.Array2.length(items.contents) - 1 { + let item = items.contents[j] + Js.log(item.name ++ ", " ++ string_of_int(item.sellIn) ++ ", " ++ string_of_int(item.quality)) + } + Js.log("") + items := updateQuality(items.contents) +}