From d1907e56a85ea7ad36eb4ffe5e8d3d6e825f7881 Mon Sep 17 00:00:00 2001 From: Benjamin Barreto <4544900-benalexb@users.noreply.gitlab.com> Date: Thu, 22 Oct 2020 08:40:20 +0200 Subject: [PATCH] Added more comments and dock blocks --- js-jest/src/gilded_rose.js | 38 ++++++++++++++++++++++++++++++++ js-jest/test/texttest_fixture.js | 4 ++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 9ce8a4e5..469bbbb8 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -23,20 +23,43 @@ export class RegularItem extends Item { this.depreciationRate = 1 } + /** + * Item name validator + * + * @param {String} name + * @returns {Boolean} + */ isNameValid (name) { return typeof name === 'string' && name.length } + /** + * Item "sellIn" validator + * + * @param {Number} sellIn + * @returns {Boolean} + */ isSellInValid (sellIn) { return typeof sellIn === 'number' } + /** + * Item quality validator + * + * @param {Number} quality + * @returns {Boolean} + */ isQualityValid (quality) { // "The Quality of an item is never negative" // "The Quality of an item is never more than 50" return typeof quality === 'number' && quality >= 0 && quality <= 50 } + /** + * Item props validation to be used during initialization. + * + * @param {Object} itemProps + */ validateItemProps ({ name, sellIn, quality }) { const errors = [] @@ -51,6 +74,8 @@ export class RegularItem extends Item { /** * "Once the sell by date has passed, Quality degrades twice as fast" + * + * @returns {Number} */ getDepreciationRate () { 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 () { // "The Quality of an item is never negative" this.setQuality(Math.max(0, this.quality - this.getDepreciationRate())) @@ -87,6 +116,9 @@ export class ConcertPass extends RegularItem { this.depreciationRate = -1 } + /** + * Calculates a concert pass' depreciation rate according to how close the concert is. + */ getDepreciationRate () { // "Quality drops to 0 after the concert" if (this.sellIn < 0) { @@ -115,6 +147,11 @@ export class AgedCheese extends RegularItem { 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 () { return this.sellIn <= 0 ? this.depreciationRate * 2 : this.depreciationRate } @@ -233,6 +270,7 @@ export class ShopV2 extends Shop { let ItemClass = RegularItem // Special Items + if (LEGENDARY_ITEMS.indexOf(name) !== -1) { ItemClass = LegendaryItem } diff --git a/js-jest/test/texttest_fixture.js b/js-jest/test/texttest_fixture.js index d756f9a9..8591a8e2 100644 --- a/js-jest/test/texttest_fixture.js +++ b/js-jest/test/texttest_fixture.js @@ -26,8 +26,8 @@ const gildedRoseV2 = new ShopV2(items) for (let day = 0; day < days; day++) { const shopTable = new Table({ - head: ['Name', 'Sell In (v1)', 'Quality (v1)', 'Sell In (v2)', 'Quality (v2)'], - colWidths: [50, 15, 15, 15, 15] + head: ['Name', 'SellIn v1', 'Qlty v1', 'SellIn v2', 'Qlty v2'], + colWidths: [43, 11, 9, 11, 9] }) shopTable.push(...items.map(({ name, sellIn, quality }, index) => {