mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
refactor business logic, add test to verify backstage item quality limit
This commit is contained in:
parent
b2205f03db
commit
e19e99800c
@ -3,7 +3,7 @@ export class Item {
|
|||||||
sellIn: number;
|
sellIn: number;
|
||||||
quality: number;
|
quality: number;
|
||||||
|
|
||||||
constructor(name, sellIn, quality) {
|
constructor(name: string, sellIn: number, quality: number) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.sellIn = sellIn;
|
this.sellIn = sellIn;
|
||||||
this.quality = quality;
|
this.quality = quality;
|
||||||
@ -11,6 +11,7 @@ export class Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class GildedRose {
|
export class GildedRose {
|
||||||
|
private QUALITY_LIM = 50;
|
||||||
items: Array<Item>;
|
items: Array<Item>;
|
||||||
|
|
||||||
constructor(items = [] as Array<Item>) {
|
constructor(items = [] as Array<Item>) {
|
||||||
@ -18,52 +19,74 @@ export class GildedRose {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateQuality() {
|
updateQuality() {
|
||||||
for (let i = 0; i < this.items.length; i++) {
|
return this.items.map((item) => {
|
||||||
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
|
if (this._isAgeable(item.name)) {
|
||||||
if (this.items[i].quality > 0) {
|
return this._decayAgeableItem(item);
|
||||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
} else if (this._isLegendary(item.name)) {
|
||||||
this.items[i].quality = this.items[i].quality - 1
|
return this._decayLegendaryItem(item);
|
||||||
}
|
} else if (this._isBackstagePass(item.name)) {
|
||||||
}
|
return this._decayBackstagePassItem(item);
|
||||||
} else {
|
} else {
|
||||||
if (this.items[i].quality < 50) {
|
return this._decayGeneralItem(item);
|
||||||
this.items[i].quality = this.items[i].quality + 1
|
|
||||||
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
|
|
||||||
if (this.items[i].sellIn < 11) {
|
|
||||||
if (this.items[i].quality < 50) {
|
|
||||||
this.items[i].quality = this.items[i].quality + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this.items[i].sellIn < 6) {
|
|
||||||
if (this.items[i].quality < 50) {
|
|
||||||
this.items[i].quality = this.items[i].quality + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
});
|
||||||
this.items[i].sellIn = this.items[i].sellIn - 1;
|
|
||||||
}
|
|
||||||
if (this.items[i].sellIn < 0) {
|
|
||||||
if (this.items[i].name != 'Aged Brie') {
|
|
||||||
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
|
|
||||||
if (this.items[i].quality > 0) {
|
|
||||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
|
||||||
this.items[i].quality = this.items[i].quality - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.items[i].quality = this.items[i].quality - this.items[i].quality
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (this.items[i].quality < 50) {
|
|
||||||
this.items[i].quality = this.items[i].quality + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.items;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _isAgeable = (name: string): boolean => name === "Aged Brie";
|
||||||
|
|
||||||
|
private _isLegendary = (name: string): boolean =>
|
||||||
|
name === "Sulfuras, Hand of Ragnaros";
|
||||||
|
|
||||||
|
private _isBackstagePass = (name: string): boolean =>
|
||||||
|
name === "Backstage passes to a TAFKAL80ETC concert";
|
||||||
|
|
||||||
|
private _decayGeneralItem = (item: Item): Item => {
|
||||||
|
const qualityDecay = item.sellIn > 0 ? 1 : 2;
|
||||||
|
return {
|
||||||
|
name: item.name,
|
||||||
|
quality: item.quality > 0 ? item.quality - qualityDecay : 0,
|
||||||
|
sellIn: item.sellIn - 1,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
private _decayLegendaryItem = (item: Item): Item => {
|
||||||
|
return item;
|
||||||
|
};
|
||||||
|
|
||||||
|
private _decayAgeableItem = (item: Item): Item => {
|
||||||
|
const newItem = {
|
||||||
|
name: item.name,
|
||||||
|
quality: item.quality + 1,
|
||||||
|
sellIn: item.sellIn - 1,
|
||||||
|
};
|
||||||
|
return this._applyQualityLimitation(newItem);
|
||||||
|
};
|
||||||
|
|
||||||
|
private _decayBackstagePassItem = (item: Item): Item => {
|
||||||
|
let newQuality;
|
||||||
|
if (item.sellIn > 10) {
|
||||||
|
newQuality = item.quality + 1;
|
||||||
|
} else if (item.sellIn > 5) {
|
||||||
|
newQuality = item.quality + 2;
|
||||||
|
} else if (item.sellIn > 0) {
|
||||||
|
newQuality = item.quality + 3;
|
||||||
|
} else {
|
||||||
|
newQuality = 0;
|
||||||
|
}
|
||||||
|
const newItem = {
|
||||||
|
name: item.name,
|
||||||
|
quality: newQuality,
|
||||||
|
sellIn: item.sellIn - 1,
|
||||||
|
};
|
||||||
|
return this._applyQualityLimitation(newItem);
|
||||||
|
};
|
||||||
|
|
||||||
|
private _applyQualityLimitation = (item: Item): Item => {
|
||||||
|
return {
|
||||||
|
name: item.name,
|
||||||
|
quality:
|
||||||
|
item.quality > this.QUALITY_LIM ? this.QUALITY_LIM : item.quality,
|
||||||
|
sellIn: item.sellIn,
|
||||||
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -95,5 +95,11 @@ describe("Gilded Rose", () => {
|
|||||||
expect(items[0].sellIn).toBe(-1);
|
expect(items[0].sellIn).toBe(-1);
|
||||||
expect(items[0].quality).toBe(0);
|
expect(items[0].quality).toBe(0);
|
||||||
});
|
});
|
||||||
|
it("should not exceed 50 quality", () => {
|
||||||
|
const gildedRose = new GildedRose([backstagePassItem(5, 50)]);
|
||||||
|
const items = gildedRose.updateQuality();
|
||||||
|
expect(items[0].sellIn).toBe(4);
|
||||||
|
expect(items[0].quality).toBe(50);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user