refactored

This commit is contained in:
Stuart Kolodner 2020-01-31 01:04:03 -05:00
parent 6bd9e8e389
commit 20b66ae5b7
3 changed files with 132 additions and 72 deletions

View File

@ -1563,8 +1563,7 @@
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",
"integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=",
"dev": true,
"optional": true
"dev": true
},
"aproba": {
"version": "1.2.0",
@ -1588,15 +1587,13 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true,
"optional": true
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -1613,22 +1610,19 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz",
"integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=",
"dev": true,
"optional": true
"dev": true
},
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true,
"optional": true
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
"integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=",
"dev": true,
"optional": true
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@ -1759,8 +1753,7 @@
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
"dev": true,
"optional": true
"dev": true
},
"ini": {
"version": "1.3.5",
@ -1774,7 +1767,6 @@
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz",
"integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=",
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -1791,7 +1783,6 @@
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -1800,15 +1791,13 @@
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true,
"optional": true
"dev": true
},
"minipass": {
"version": "2.3.5",
"resolved": "https://registry.npmjs.org/minipass/-/minipass-2.3.5.tgz",
"integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==",
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
@ -1829,7 +1818,6 @@
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@ -1918,8 +1906,7 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz",
"integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=",
"dev": true,
"optional": true
"dev": true
},
"object-assign": {
"version": "4.1.1",
@ -1933,7 +1920,6 @@
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@ -2029,8 +2015,7 @@
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
"dev": true,
"optional": true
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
@ -2072,7 +2057,6 @@
"resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
"integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=",
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@ -2094,7 +2078,6 @@
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@ -2143,15 +2126,13 @@
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true,
"optional": true
"dev": true
},
"yallist": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz",
"integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==",
"dev": true,
"optional": true
"dev": true
}
}
},

View File

@ -10,51 +10,53 @@ class Shop {
constructor(items=[]){
this.items = items;
}
// Should really be method on Item, but I don't want to offend the goblins.
isAgedBrie(item) {
return item.name.startsWith('Aged Brie');
}
// Should really be method on Item, but I don't want to offend the goblins.
isConcertTicket(item) {
return item.name.startsWith('Backstage passes to ');
}
// Should really be method on Item, but I don't want to offend the goblins.
isConjured(item) {
return item.name.startsWith('Conjured ');
}
updateQuality() {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].name != 'Aged Brie' && 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;
}
for (let item of this.items) {
if (item.name == 'Sulfuras, Hand of Ragnaros') {
continue;
}
let qualityChange = 0;
if(this.isAgedBrie(item)) {
// Does conjured cheese quality increase twice as much? Assume not, but
// statement of problem doesn't say cheese quality doubles twice as fast after
// the sell by date, but the code implements it that way.
qualityChange = item.sellIn <= 0 ? 2 : 1;
} else if (this.isConcertTicket(item)) {
if(item.sellIn <= 0) {
qualityChange = -1 * item.quality;
} else if(item.sellIn < 6) {
qualityChange = 3;
} else if(item.sellIn < 11) {
qualityChange = 2;
} else {
qualityChange = 1;
}
} else {
if (this.items[i].quality < 50) {
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;
}
qualityChange = item.sellIn <= 0 ? -2 : -1;
if(this.isConjured(item)) {
qualityChange = qualityChange * 2;
}
}
item.sellIn--;
item.quality = Math.min(Math.max(item.quality + qualityChange, 0), 50);
}
return this.items;

View File

@ -1,9 +1,86 @@
const {Shop, Item} = require("../src/gilded_rose");
describe("Gilded Rose", function() {
it("should foo", function() {
it("name should be foo", function() {
const gildedRose = new Shop([new Item("foo", 0, 0)]);
const items = gildedRose.updateQuality();
expect(items[0].name).toBe("fixme");
expect(items[0].name).toBe("foo");
});
it("sellIn should be -1", function() {
const gildedRose = new Shop([new Item("foo", 0, 0)]);
const items = gildedRose.updateQuality();
expect(items[0].sellIn).toBe(-1);
});
it("foo quality should be 0", function() {
const gildedRose = new Shop([new Item("foo", 0, 0)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(0);
});
it("foo quality should be 0", function() {
const gildedRose = new Shop([new Item("foo", 10, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(9);
});
it("conjured quality should be 8", function() {
const gildedRose = new Shop([new Item("Conjured foo", 10, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(8);
});
it("conjured quality should be 8", function() {
const gildedRose = new Shop([new Item("Conjured foo", 10, 1)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(0);
});
it("Sulfuras sellIn should be unchanged", function() {
const gildedRose = new Shop([new Item("Sulfuras, Hand of Ragnaros", 10, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].sellIn).toBe(10);
});
it("Sulfuras quality should be unchanged", function() {
const gildedRose = new Shop([new Item("Sulfuras, Hand of Ragnaros", 10, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(10);
});
it("Brie quality should be 11", function() {
const gildedRose = new Shop([new Item('Aged Brie', 10, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(11);
});
it("Brie quality should be 50", function() {
const gildedRose = new Shop([new Item('Aged Brie', 10, 50)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(50);
});
if("Brie quality should be 12", function() {
const gildedRose = new Shop([new Item('Aged Brie', 0, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(12);
});
it("Brie quality should be 50", function() {
const gildedRose = new Shop([new Item('Aged Brie', 0, 49)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(50);
});
it("Backstage pass quality should be 11", function() {
const gildedRose = new Shop([new Item('Backstage passes to a TAFKAL80ETC concert', 11, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(11);
});
it("Backstage pass quality should be 12", function() {
const gildedRose = new Shop([new Item('Backstage passes to a TAFKAL80ETC concert', 10, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(12);
});
it("Backstage pass quality should be 13", function() {
const gildedRose = new Shop([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(13);
});
it("Backstage pass quality should be 13", function() {
const gildedRose = new Shop([new Item('Backstage passes to a TAFKAL80ETC concert', 0, 10)]);
const items = gildedRose.updateQuality();
expect(items[0].quality).toBe(0);
});
});