All files / src/modules commerce.js

100% Statements 85/85
100% Branches 22/22
100% Functions 10/10
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x 1x 22x 22x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 8x 8x 8x 3x 3x 3x 3x 8x 8x 8x 8x 8x 8x 1x 1x 7x 7x 7x 7x 7x 7x 8x 8x 1x 1x 3x 3x 3x 3x 1x 1x 3x 3x 1x 3x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 1x 1x 2x 2x 2x 2x 1x 1x 2x 2x 1x 1x 4x 4x 1x 1x 1x 1x 1x  
export class Commerce {
  constructor(pure) {
    this.pure = pure;
  }
 
  color() {
    return this.pure.random.arrayElement(this.pure.registeredModules.commerce.color);
  }
 
  department() {
    return this.pure.random.arrayElement(this.pure.registeredModules.commerce.department);
  }
 
  productName() {
    return (
      `${this.pure.commerce.productAdjective()}` +
      ` ${this.pure.commerce.productMaterial()} ${this.pure.commerce.product()}`
    );
  }
 
  price(options) {
    let def = options;
 
    if (def === undefined) {
      def = {
        min: 1
      };
    }
 
    def.max = def.max || 1000 * def.min;
    def.dec = def.dec === undefined ? 2 : def.dec;
    def.symbol = def.symbol || '';
 
    if (def.min < 0 || def.max < 0) {
      return def.symbol + 0.0;
    }
 
    const randValue = this.pure.random
      .number({ max: def.max, min: def.min, precision: def.dec })
      .toFixed(def.dec);
    const finalValue = def.symbol + randValue.toString();
 
    return def.comma ? finalValue.replace(/\B(?=(\d{3})+(?!\d))/g, ',') : finalValue;
  }
 
  categories(num) {
    let categories = [];
    let def = num;
 
    if (typeof def === 'undefined') {
      def = this.pure.random.number({ min: 1, max: 10 });
    }
 
    if (def > this.pure.registeredModules.commerce.department.length) {
      categories = this.pure.registeredModules.commerce.department;
    } else {
      const shuffledArr = this.pure.helpers.shuffle(
        this.pure.registeredModules.commerce.department
      );
      const diff = this.pure.registeredModules.commerce.department.length - num;
      shuffledArr.splice(0, diff);
      categories = shuffledArr;
    }
 
    return categories;
  }
 
  productAdjective() {
    return this.pure.random.arrayElement(
      this.pure.registeredModules.commerce.productName.adjective
    );
  }
 
  productMaterial() {
    return this.pure.random.arrayElement(this.pure.registeredModules.commerce.productName.material);
  }
 
  product() {
    return this.pure.random.arrayElement(this.pure.registeredModules.commerce.productName.product);
  }
 
  productDescription() {
    return this.pure.random.arrayElement(this.pure.registeredModules.commerce.productDescription);
  }
}