Skip to content

Instantly share code, notes, and snippets.

@monlovesmango
Created December 9, 2023 04:03
Show Gist options
  • Save monlovesmango/aa5309a15575b5f6e49bafaf84451c07 to your computer and use it in GitHub Desktop.
Save monlovesmango/aa5309a15575b5f6e49bafaf84451c07 to your computer and use it in GitHub Desktop.
GildedRose-Refactoring-Kata
export class Item {
name: string;
sellIn: number;
quality: number;
constructor(name, sellIn, quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}
export class GildedRose {
items: Array<Item>;
constructor(items = [] as Array<Item>) {
this.items = items;
}
updateQuality() {
for (let i = 0; i < this.items.length; i++) {
this.items[i].sellIn += this.sellInDelta(this.items[i]);
this.items[i].quality = this.newQuality(this.items[i].quality, this.qualityDelta(this.items[i]));
}
return this.items;
}
sellInDelta(item: Item) { return item.name === 'Sulfuras, Hand of Ragnaros' ? 0 : -1 }
qualityDelta(item: Item) {
if (item.name === 'Sulfuras, Hand of Ragnaros') return 0
if (item.name === 'Aged Brie') return item.sellIn < 0 ? 2 : 1
if (item.name === 'Backstage passes to a TAFKAL80ETC concert') {
if (item.sellIn < 0) return -item.quality
if (item.sellIn < 5) return 3
if (item.sellIn < 10) return 2
return 1
}
return -(item.sellIn < 0 ? 2 : 1) * (item.name === 'Conjured Mana Cake' ? 2 : 1)
}
newQuality(quality: number, delta: number) { return delta > 0 ? Math.min(quality + delta, 50) : Math.max(quality + delta, 0) }
}
@monlovesmango
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment