all files / src/ seen.js

100% Statements 41/41
100% Branches 9/9
100% Functions 14/14
100% Lines 19/19
1 branch Ignored     
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  14× 38×         75× 75×       33×       67×                               49× 42×      
class NoCheckSeen {
  add() {return this}
  has() {return false}
}
 
// TODO : use an immutable collection
class LeafSeen {
  constructor(arr) {
    this._arr = arr;
  }
 
  add(value) {
    return new LeafSeen([...this._arr, value]);
  }
 
  has(value) {
    return this._arr.includes(value);
  }
}
 
// TODO : use an immutable collection
class ThrowSeen {
  constructor(arr) {
    this._arr = arr;
  }
 
  add(value) {
    return new ThrowSeen([...this._arr, value]);
  }
 
  has(value) {
    if (this._arr.includes(value)) {
      throw new Error(`Circular reference : ${value}`);
    }
    return false;
  }
}
 
export default function makeSeen(circularReference) {
  switch (circularReference) {
    case 'leaf': return new LeafSeen([]);
    case 'throw': return new ThrowSeen([]);
    case 'noCheck': return new NoCheckSeen();
  }
  throw new Error(`Incorrect value ${circularReference} for circularReference option.`);
}