all files / src/ shop.js

100% Statements 28/28
100% Branches 8/8
100% Functions 10/10
100% Lines 28/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                   14× 14×   14×       14× 14×       14× 14×       14× 40×           14× 13×         14× 14×       14×         14×            
var glob = require('glob')
  , path = require('path');
 
let itemTypes = [];
 
glob.sync('./src/item_types/**/*.js').forEach(function (file) {
  itemTypes.push(require(path.resolve(file)));
});
 
const standardItem = require('./standard_update.js');
class Shop {
  constructor(items = []) {
    this.items = items;
    this.MAX_QUALITY = 50;
    this.MIN_QUALITY = 0;
  }
  updateQuality() {
    this.items.forEach(item =>
      this._updateItem(item)
    );
    return this.items;
  }
 
  _updateItem(item) {
    this._updateItemQuality(item);
    this._updateItemSellIn(item);
  }
 
  _updateItemQuality(item) {
    this._conditionallyUpdateQuality(item);
    this._checkQualityBounds(item);
  }
 
  _conditionallyUpdateQuality(item) {
    for (const itemType of itemTypes) {
      if (item.name.toLowerCase().match(itemType.regex_matcher)) {
        return item.quality += itemType.qualityChange(item.sellIn, item.quality);
      }
    }
    return item.quality += standardItem.qualityChange(item.sellIn);
  }
 
  _updateItemSellIn(item) {
    if (item.name != 'Sulfuras, Hand of Ragnaros') {
      item.sellIn = item.sellIn - 1;
    }
  }
 
  _checkQualityBounds(item) {
    this._checkMaxQuality(item);
    this._checkMinQuality(item);
  }
 
  _checkMaxQuality(item) {
    if (item.quality > this.MAX_QUALITY) {
      item.quality = this.MAX_QUALITY;
    }
  }
 
  _checkMinQuality(item) {
    if (item.quality < this.MIN_QUALITY) {
      item.quality = this.MIN_QUALITY;
    }
  }
}
module.exports = {
  Shop
};