Created
November 8, 2021 23:07
-
-
Save kulicuu/170867e27e72d8542b3e072c41912d19 to your computer and use it in GitHub Desktop.
Assessment Kata from GildedRose Refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const itemQualityFunctionIndex = { | |
"Aged Brie": function(item) { | |
item.quality++; | |
return item | |
}, | |
"Backstage passes": function (item) { | |
if (item.sellIn <= 10 && item.sellIn > 5) { | |
item.quality += 2; | |
} else if (item.sellIn <= 5 && item.sellIn > -1) { | |
item.quality += 3; | |
} else if (item.sellIn < 0) { | |
item.quality = 0; | |
} | |
return item | |
}, | |
"Sulfuras": function (item) { | |
return item; | |
}, | |
"normal": function (item) { | |
if (item.conjured == true) { | |
if (item.sellIn < 0) { | |
item.quality -= 4; | |
} else { | |
item.quality -= 2; | |
} | |
} else { | |
if (item.sellIn < 0) { | |
item.quality -= 2; | |
} else { | |
item.quality --; | |
} | |
} | |
return item | |
} | |
} | |
class Item { | |
constructor(name, sellIn, quality, conjured){ | |
this.name = name; | |
this.sellIn = sellIn; | |
this.quality = quality; | |
this.conjured = conjured; | |
} | |
} | |
class Shop { | |
constructor(items=[]){ | |
this.items = items; | |
} | |
updateQuality() { | |
for (var i = 0; i < this.items.length; i++) { | |
let item = this.items[i]; | |
item = updateItemQuality(item); | |
item.sellIn = item.sellIn --; | |
if ((item.quality > 50) && (item.name != "Sulfuras")) { | |
item.quality = 50; | |
} | |
if (item.quality < 0) { | |
item.quality = 0; | |
} | |
} // end for loop | |
return this.items; | |
} // end updateQuality | |
} // end class Sho | |
module.exports = { | |
Item, | |
Shop | |
} | |
function updateItemQuality(item) { | |
if (Object.keys(itemQualityFunctionIndex).indexOf(item.name) > -1) { | |
return itemQualityFunctionIndex[item.name](item) | |
} else { | |
return itemQualityFunctionIndex.normal(item) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var {expect} = require('chai'); | |
var {Shop, Item} = require('../src/gilded_rose_new.js'); | |
var c = console.log.bind(console); | |
describe("Gilded Rose", function() { | |
it("Should work", function() { | |
const gildedRose = new Shop([ | |
new Item("Aged Brie", 5, 30, false), | |
new Item("Backstage passes", 8, 20, false), | |
new Item("Stamps", 10, 14, true), | |
new Item("Backstage passes", 2, 90, false), | |
new Item("Backstage passes", 2, 30, false), | |
new Item("Stamps", -3, 14, true), | |
]); | |
const items = gildedRose.updateQuality(); | |
expect(items[0].quality).to.equal(31); | |
expect(items[1].quality).to.equal(22); | |
expect(items[2].quality).to.equal(12); | |
expect(items[3].quality).to.equal(50); | |
expect(items[4].quality).to.equal(33); | |
expect(items[5].quality).to.equal(10); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment