All files / packages/tao/src AppCtxRoot.js

100% Statements 31/31
100% Branches 60/60
100% Functions 16/16
100% Lines 31/31
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      2062x         724x 724x 724x       384x       600x       568x       588x       1000x         108x 61x 130x 84x 130x                           23x         136x 60x           106x 383x   365x 305x     311x   87x   34x 30x       367x       305x       275x       281x       57x       4x      
import { WILDCARD } from './constants';
 
function isPartWild(val) {
  return !val || WILDCARD === val;
}
 
export default class AppCtxRoot {
  constructor(term, action, orient) {
    this._term = term || WILDCARD;
    this._action = action || WILDCARD;
    this._orient = orient || WILDCARD;
  }
 
  get key() {
    return AppCtxRoot.getKey(this._term, this._action, this._orient);
  }
 
  get t() {
    return this._term;
  }
 
  get a() {
    return this._action;
  }
 
  get o() {
    return this._orient;
  }
 
  static getKey(term, action, orient) {
    return `${term || WILDCARD}|${action || WILDCARD}|${orient || WILDCARD}`;
  }
 
  // static isWildcard({ term = WILDCARD, action = WILDCARD, orient = WILDCARD } = {}) {
  static isWildcard(ac = {}) {
    const { t, term, a, action, o, orient } = ac;
    const checkT = t || term;
    const checkA = a || action;
    const checkO = o || orient;
    return (
      !checkT ||
      !checkA ||
      !checkO ||
      !checkT.length ||
      !checkA.length ||
      !checkO.length ||
      checkT === WILDCARD ||
      checkA === WILDCARD ||
      checkO === WILDCARD
    );
  }
 
  static isConcrete(ac = {}) {
    return !AppCtxRoot.isWildcard(ac);
  }
 
  // TODO: write TESTS for this
  static isMatch(ac, trigram, exact = false) {
    if (!(ac instanceof AppCtxRoot)) {
      ac = new AppCtxRoot(
        ac.t || ac.term,
        ac.a || ac.action,
        ac.o || ac.orient
      );
    }
    if (ac.key === AppCtxRoot.getKey(trigram.t, trigram.a, trigram.o)) {
      return true;
    }
    if (exact) {
      return false;
    }
    const matchTerm =
      ac.isTermWild || isPartWild(trigram.t) || ac.t === trigram.t;
    const matchAction =
      ac.isActionWild || isPartWild(trigram.a) || ac.a === trigram.a;
    const matchOrient =
      ac.isOrientWild || isPartWild(trigram.o) || ac.o === trigram.o;
    return matchTerm && matchAction && matchOrient;
  }
 
  get isTermWild() {
    return isPartWild(this._term);
  }
 
  get isActionWild() {
    return isPartWild(this._action);
  }
 
  get isOrientWild() {
    return isPartWild(this._orient);
  }
 
  get isWildcard() {
    return this.isTermWild || this.isActionWild || this.isOrientWild;
  }
 
  get isConcrete() {
    return !this.isWildcard;
  }
 
  isMatch(trigram, exact = false) {
    return AppCtxRoot.isMatch(this, trigram, exact);
  }
}