All files / src/carousel array.js

100% Statements 52/52
100% Branches 38/38
100% Functions 8/8
100% Lines 48/48

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 86 87 88 89 90 91 92 93 94 95  339x   339x   339x     420682x       339x 339x 1x 338x 1x 337x 336x 52042x   336x         52042x 52042x     1x   338x     339x 5x 5x 5x 21x 21x 19x 14x   5x     339x   282151x 508x           281643x   282151x     339x 117106x 117106x 117106x     339x   170317x 64987x       105330x   170317x     339x 19x 1x   18x 1x 1x 1x   17x            
class CircularArray {
  array = [];
 
  key = [];
 
  origin = null;
 
  get length() {
    return this.array.length;
  }
 
  constructor(n, origin = null) {
    this.array = [];
    if (typeof n === 'number') {
      this.array = new Array(n);
    } else if (typeof n === 'object' && Array.isArray(n)) {
      this.array = n;
    } else if (typeof n === 'object' && n.length > 0 && n instanceof NodeList) {
      for (let i = 0; i < n.length; i += 1) {
        this.array.push(n[i]);
      }
      this.key = this.array.map((item) => {
        const {
          dataset: {
            carouselkey
          }
        } = item;
        return carouselkey;
      });
    } else {
      throw new Error('can not create array');
    }
    this.origin = origin;
  }
 
  toString = (array) => {
    const result = [];
    const newArray = array || this.array;
    for (let i = 0; i < newArray.length; i += 1) {
      const item = newArray[i];
      if (typeof item === 'object' && item instanceof Array) result.push(`[${this.toString(item)}]`);
      else if (typeof item === 'object') result.push(JSON.stringify(item));
      else result.push(item.toString());
    }
    return result.join(',');
  };
 
  get = (i) => {
    let result;
    if (i < 0 || i < this.length - this.array.length) {
      result = this.array[
        -i % this.array.length === 0
          ? 0
          : this.array.length + (i % this.array.length)
      ];
    } else {
      result = this.array[i % this.array.length];
    }
    return result;
  };
 
  getKeyIndex = (i) => {
    const originItem = this.origin.get(i);
    const { dataset: { carouselkey } } = originItem;
    return this.key.indexOf(carouselkey);
  }
 
  getIndex = (i) => {
    let result;
    if (i < 0 || i < this.length - this.array.length) {
      result = -i % this.array.length === 0
        ? 0
        : this.array.length + (i % this.array.length);
    } else {
      result = i % this.array.length;
    }
    return result;
  }
 
  set = (i, v) => {
    if (i < 0 || i < this.length - this.array.length) {
      throw new Error('can not set index < 0');
    }
    if (i >= this.length) {
      const newArr = new Array(i - this.length + 1);
      this.array = this.array.concat(newArr);
      this.array.splice(i, 1, v);
    } else {
      this.array[this.getIndex(i)] = v;
    }
  };
}
 
export default CircularArray;