mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Added more comments and dock blocks
This commit is contained in:
parent
4520c75f4c
commit
d1907e56a8
@ -23,20 +23,43 @@ export class RegularItem extends Item {
|
|||||||
this.depreciationRate = 1
|
this.depreciationRate = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Item name validator
|
||||||
|
*
|
||||||
|
* @param {String} name
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
isNameValid (name) {
|
isNameValid (name) {
|
||||||
return typeof name === 'string' && name.length
|
return typeof name === 'string' && name.length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Item "sellIn" validator
|
||||||
|
*
|
||||||
|
* @param {Number} sellIn
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
isSellInValid (sellIn) {
|
isSellInValid (sellIn) {
|
||||||
return typeof sellIn === 'number'
|
return typeof sellIn === 'number'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Item quality validator
|
||||||
|
*
|
||||||
|
* @param {Number} quality
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
isQualityValid (quality) {
|
isQualityValid (quality) {
|
||||||
// "The Quality of an item is never negative"
|
// "The Quality of an item is never negative"
|
||||||
// "The Quality of an item is never more than 50"
|
// "The Quality of an item is never more than 50"
|
||||||
return typeof quality === 'number' && quality >= 0 && quality <= 50
|
return typeof quality === 'number' && quality >= 0 && quality <= 50
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Item props validation to be used during initialization.
|
||||||
|
*
|
||||||
|
* @param {Object} itemProps
|
||||||
|
*/
|
||||||
validateItemProps ({ name, sellIn, quality }) {
|
validateItemProps ({ name, sellIn, quality }) {
|
||||||
const errors = []
|
const errors = []
|
||||||
|
|
||||||
@ -51,6 +74,8 @@ export class RegularItem extends Item {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* "Once the sell by date has passed, Quality degrades twice as fast"
|
* "Once the sell by date has passed, Quality degrades twice as fast"
|
||||||
|
*
|
||||||
|
* @returns {Number}
|
||||||
*/
|
*/
|
||||||
getDepreciationRate () {
|
getDepreciationRate () {
|
||||||
return this.sellIn < 0 ? this.depreciationRate * 2 : this.depreciationRate
|
return this.sellIn < 0 ? this.depreciationRate * 2 : this.depreciationRate
|
||||||
@ -69,6 +94,10 @@ export class RegularItem extends Item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates this item's quality, ensuring it is never a negative number.
|
||||||
|
* Decrements the item's sellIn property.
|
||||||
|
*/
|
||||||
updateQuality () {
|
updateQuality () {
|
||||||
// "The Quality of an item is never negative"
|
// "The Quality of an item is never negative"
|
||||||
this.setQuality(Math.max(0, this.quality - this.getDepreciationRate()))
|
this.setQuality(Math.max(0, this.quality - this.getDepreciationRate()))
|
||||||
@ -87,6 +116,9 @@ export class ConcertPass extends RegularItem {
|
|||||||
this.depreciationRate = -1
|
this.depreciationRate = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates a concert pass' depreciation rate according to how close the concert is.
|
||||||
|
*/
|
||||||
getDepreciationRate () {
|
getDepreciationRate () {
|
||||||
// "Quality drops to 0 after the concert"
|
// "Quality drops to 0 after the concert"
|
||||||
if (this.sellIn < 0) {
|
if (this.sellIn < 0) {
|
||||||
@ -115,6 +147,11 @@ export class AgedCheese extends RegularItem {
|
|||||||
this.depreciationRate = -1
|
this.depreciationRate = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the depreciation rate of an aged cheese, which should be inverted (appreciation).
|
||||||
|
* For cheese, the legacy logic shows 0 sellIn is inclusive when determining the acceleration of
|
||||||
|
* the appreciation rate (twice as fast past its "sellIn" date).
|
||||||
|
*/
|
||||||
getDepreciationRate () {
|
getDepreciationRate () {
|
||||||
return this.sellIn <= 0 ? this.depreciationRate * 2 : this.depreciationRate
|
return this.sellIn <= 0 ? this.depreciationRate * 2 : this.depreciationRate
|
||||||
}
|
}
|
||||||
@ -233,6 +270,7 @@ export class ShopV2 extends Shop {
|
|||||||
let ItemClass = RegularItem
|
let ItemClass = RegularItem
|
||||||
|
|
||||||
// Special Items
|
// Special Items
|
||||||
|
|
||||||
if (LEGENDARY_ITEMS.indexOf(name) !== -1) {
|
if (LEGENDARY_ITEMS.indexOf(name) !== -1) {
|
||||||
ItemClass = LegendaryItem
|
ItemClass = LegendaryItem
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,8 +26,8 @@ const gildedRoseV2 = new ShopV2(items)
|
|||||||
|
|
||||||
for (let day = 0; day < days; day++) {
|
for (let day = 0; day < days; day++) {
|
||||||
const shopTable = new Table({
|
const shopTable = new Table({
|
||||||
head: ['Name', 'Sell In (v1)', 'Quality (v1)', 'Sell In (v2)', 'Quality (v2)'],
|
head: ['Name', 'SellIn v1', 'Qlty v1', 'SellIn v2', 'Qlty v2'],
|
||||||
colWidths: [50, 15, 15, 15, 15]
|
colWidths: [43, 11, 9, 11, 9]
|
||||||
})
|
})
|
||||||
|
|
||||||
shopTable.push(...items.map(({ name, sellIn, quality }, index) => {
|
shopTable.push(...items.map(({ name, sellIn, quality }, index) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user