Code coverage report for models/AjaxList.js

Statements: 100% (110 / 110)      Branches: 100% (48 / 48)      Functions: 100% (18 / 18)      Lines: 100% (31 / 31)      Ignored: 23 statements, 19 branches     

All files » models/ » AjaxList.js
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                    1 1     1 18 18 18       40       21 1   20   20       9 9       32       4 2   2 2     4       10 10     4 3 4   3       5 5   5 1   5        
/**
 * AjaxList class
 *
 * @module AjaxList
 *
 * @author Luca Pau <luca.pau82@gmail.com>
 */
 
import AjaxCall from './AjaxCall';
 
let _items = Symbol();
let _uniqueItems = Symbol();
 
class AjaxList extends AjaxCall {
  constructor() {
    super();
    this.items = [];
    this[_uniqueItems] = new Set();
  }
 
  get items() {
    return this[_items];
  }
 
  set items(items) {
    if(!Array.isArray(items))
      throw new TypeError('Items must be array!');
 
    this[_items] = items;
 
    return this;
  }
 
  addItem(item) {
    this.items.push(item);
    return this;
  }
 
  get uniqueItems() {
    return this[_uniqueItems];
  }
 
  set uniqueItems(uniqueItems) {
    if(Array.isArray(uniqueItems))
      this[_uniqueItems] = new Set(uniqueItems);
    else {
      this.uniqueItems.clear();
      this.uniqueItems.add(uniqueItems);
    }
 
    return this;
  }
 
  addUniqueItem(uniqueItem) {
    this.uniqueItems.add(uniqueItem);
    return this;
  }
 
  concatItems(...items) {
    for(let item of items) {
      this[_items] = this[_items].concat(item);
    }
    return this;
  }
 
  getStruct() {
    var superStruct = super.getStruct();
    superStruct.items = this.items;
 
    if(this.uniqueItems.size > 0)
      superStruct.uniqueItems = [...this.uniqueItems];
 
    return superStruct;
  }
}
 
export default AjaxList;