Last active
March 1, 2022 11:54
-
-
Save DeepSnowNeeL/ae37e9c2d8bc0bbaa3b299f330ee0b2e to your computer and use it in GitHub Desktop.
GildedRose kata (https://github.com/emilybache/GildedRose-Refactoring-Kata) solutions
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
class Item { | |
constructor(name, sellIn, quality) { | |
this.name = name; | |
this.sellIn = sellIn; | |
this.quality = quality; | |
} | |
} | |
class Shop { | |
legendaries = ["Sulfuras, Hand of Ragnaros"]; | |
ageing = ["Aged Brie"]; | |
ageing_and_expiring = ["Backstage passes to a TAFKAL80ETC concert"]; | |
conjured = "Conjured"; | |
constructor(items = []) { | |
this.items = items; | |
} | |
//There is another solution which involves setting deltas for sellIn and quality and handling each category of items | |
//This has the advantage of putting the legendary item in the same indent level/conditions as the others | |
updateQuality() { | |
this.items.forEach((i) => { | |
if (this.legendaries.includes(i.name)) { | |
return; //It does not age nor lose quality | |
} | |
i.sellIn--; | |
if (this.ageing.includes(i.name)) { | |
if (i.sellIn <= 0) i.quality += 2; | |
} else if (this.ageing_and_expiring.includes(i.name)) { | |
if (i.sellIn <= 0) i.quality = 0; | |
else if (i.sellIn <= 5) i.quality += 3; | |
else if (i.sellIn < 10) i.quality += 2; | |
else i.quality++; | |
//La qualité augmente de 2 quand il reste 10 jours ou moins et de 3 quand il reste 5 jours ou moins, mais la qualité tombe à 0 après le concert. | |
} else if (i.name.includes(this.conjured)) { | |
i.quality -= 2; | |
} else if (i.sellIn > 0) { | |
i.quality--; | |
} else { | |
//if (i.sellIn <= 0) | |
i.quality -= 2; | |
} | |
if (i.quality < 0) i.quality = 0; | |
if (i.quality > 50) i.quality = 50; | |
}); | |
return this.items; | |
} | |
} | |
module.exports = { | |
Item, | |
Shop, | |
}; |
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
class Item { | |
constructor(name, sellIn, quality) { | |
this.name = name; | |
this.sellIn = sellIn; | |
this.quality = quality; | |
} | |
} | |
class ItemStrategy { | |
handle(item) { | |
return false; | |
} | |
updateQuality(item) {} | |
} | |
class DefaultItemStrategy extends ItemStrategy { | |
handle(item) { | |
return true; | |
} | |
updateQuality(item) { | |
item.sellIn--; | |
if (item.sellIn > 0) { | |
item.quality--; | |
} else { | |
item.quality -= 2; | |
} | |
if (item.quality < 0) item.quality = 0; | |
if (item.quality > 50) item.quality = 50; | |
} | |
} | |
class LegendaryItemStrategy extends ItemStrategy { | |
handle(item) { | |
return item.name == "Sulfuras, Hand of Ragnaros"; | |
} | |
} | |
class AgeingStrategy extends ItemStrategy { | |
handle(item) { | |
return item.name === "Aged Brie"; | |
} | |
updateQuality(item) { | |
item.sellIn--; | |
if (item.sellIn > 0) item.quality++; | |
if (item.sellIn <= 0) item.quality += 2; | |
if (item.quality < 0) item.quality = 0; | |
if (item.quality > 50) item.quality = 50; | |
} | |
} | |
class AgeingAndExpiringStrategy extends ItemStrategy { | |
handle(item) { | |
return item.name === "Backstage passes to a TAFKAL80ETC concert"; | |
} | |
updateQuality(item) { | |
item.sellIn--; | |
if (item.sellIn <= 0) item.quality = 0; | |
else if (item.sellIn <= 5) item.quality += 3; | |
else if (item.sellIn < 10) item.quality += 2; | |
else item.quality++; | |
if (item.quality < 0) item.quality = 0; | |
if (item.quality > 50) item.quality = 50; | |
} | |
} | |
class ConjuredStrategy extends ItemStrategy { | |
handle(item) { | |
return item.name.includes("Conjured"); | |
} | |
updateQuality(item) { | |
item.sellIn--; | |
item.quality -= 2; | |
if (item.quality < 0) item.quality = 0; | |
if (item.quality > 50) item.quality = 50; | |
} | |
} | |
class Shop { | |
strategies = [ | |
new LegendaryItemStrategy(), | |
new AgeingStrategy(), | |
new AgeingAndExpiringStrategy(), | |
new ConjuredStrategy(), | |
new DefaultItemStrategy(), | |
]; | |
constructor(items = []) { | |
this.items = items; | |
} | |
updateQuality() { | |
this.items.forEach((i) => { | |
for (let s of this.strategies) | |
if (s.handle(i)) { | |
s.updateQuality(i); | |
break; | |
} | |
}); | |
return this.items; | |
} | |
} | |
module.exports = { | |
Item, | |
Shop, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment