all files / src/ node.js

100% Statements 57/57
100% Branches 14/14
100% Functions 28/28
100% Lines 44/44
2 branches 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 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131       160× 160× 160× 160×   160× 160× 160× 160× 160× 160×         52×   51×       275×                                   19× 19×   19×             157×                                                                                                        
import {LEAF} from './generators';
import {getTag, getTypeFromTag} from './tag';
 
 
export default class Node {
  constructor(key, value, parentNode, isCircular) {
    this._value = value;
    this._key = key;
    this._parent = parentNode.value,
    this._parentNode = parentNode;
    this._selectGenerator = parentNode._selectGenerator;
    this._tag = getTag(value);
    this._generator = this._selectGenerator.getGenerator(this._tag);
    this._isCircular = isCircular;
    this._isLeaf = this._generator === LEAF || this._isCircular;
    if (!this._isLeaf && this._selectGenerator.skipIteration(this)) {
      this._isLeaf = true;
      this._generator = LEAF;
    }
  }
 
  createIterator() {
    if (this._generator === LEAF) {
      throw new Error("createIterator called on a non iterable node.");
    }
    return this._generator(this.value);
  }
 
  get value() {
    return this._value;
  }
 
  get key() {
    return this._key;
  }
 
  get parent() {
    return this._parent;
  }
 
  get parentNode() {
    return this._parentNode;
  }
 
  get path() { // evaluated only when needed - could memoize it
    return [...this.parentNode.path, this.key];
  }
 
  get type() {
    // simple memoization
    /* istanbul ignore next */
    Eif (this._type === undefined) {
      this._type = getTypeFromTag(this._tag);
    }
    return this._type;
  }
 
  isCircular() {
    return this._isCircular;
  }
 
  isLeaf() {
    return this._isLeaf;
  }
 
  isNull() {
    return this.type === 'Null';
  }
 
  isUndefined() {
    return this.type === 'Undefined';
  }
 
  isBoolean() {
    return this.type === 'Boolean';
  }
 
  isNumber() {
    return this.type === 'Number';
  }
 
  isString() {
    return this.type === 'String';
  }
 
  isSymbol() {
    return this.type === 'Symbol';
  }
 
  isDate() {
    return this.type === 'Date';
  }
 
  isRegExp() {
    return this.type === 'RegExp';
  }
 
  isFunction() {
    return this.type === 'Function';
  }
 
  isGeneratorFunction() {
    return this.type === 'GeneratorFunction';
  }
 
  isPromise() {
    return this.type === 'Promise';
  }
 
  isArray() {
    return this.type === 'Array';
  }
 
  isSet() {
    return this.type === 'Set';
  }
 
  isMap() {
    return this.type === 'Map';
  }
 
  isUserDefinedIterable() {
    return this.type === 'UserDefinedIterable';
  }
 
  isNonIterableObject() {
    return this.type === 'NonIterableObject';
  }
 
}