All files / packages/tao/src AppCtx.js

100% Statements 50/50
100% Branches 37/37
100% Functions 4/4
100% Lines 50/50
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      252x 194x 126x   109x 80x   80x 31x 30x   10x 17x 12x 4x 6x 9x 4x 12x 8x 4x 7x   13x 7x 5x 7x 5x 13x 3x 2x 9x   10x 2x 8x 11x 4x 10x 15x 53x 32x     59x 16x         100x 37x   100x 134x   176x 118x   114x                                                                                     126x     126x       110x       64x          
import AppCtxRoot from './AppCtxRoot';
 
function _cleanDatum(term, action, orient, ...data) {
  const datum = {};
  if (!data.length) {
    return {};
  }
  let checkData = data;
  if (checkData.length === 1) {
    // MUST check Array first b/c ([] instanceof Object === true)
    if (Array.isArray(checkData[0])) {
      checkData = checkData[0];
    } else if (checkData[0] instanceof Object) {
      // infer all context data from single object
      const obj = checkData[0];
      let assigned = false;
      if (typeof obj.term !== 'undefined') {
        datum[term] = obj.term;
        assigned = true;
      } else if (typeof obj.t !== 'undefined') {
        datum[term] = obj.t;
        assigned = true;
      } else if (typeof obj[term] !== 'undefined') {
        datum[term] = obj[term];
        assigned = true;
      }
      if (typeof obj.action !== 'undefined') {
        datum[action] = obj.action;
        assigned = true;
      } else if (typeof obj.a !== 'undefined') {
        datum[action] = obj.a;
        assigned = true;
      } else if (typeof obj[action] !== 'undefined') {
        datum[action] = obj[action];
        assigned = true;
      }
      if (typeof obj.orient !== 'undefined') {
        datum[orient] = obj.orient;
        assigned = true;
      } else if (typeof obj.o !== 'undefined') {
        datum[orient] = obj.o;
        assigned = true;
      } else if (typeof obj[orient] !== 'undefined') {
        datum[orient] = obj[orient];
        assigned = true;
      }
      // if the single object passed defined a tuple with the expected keys
      if (assigned) {
        return datum;
      }
    }
  }
  // if we are left then assume the first object is term only
  if (typeof checkData[0] !== 'undefined') {
    datum[term] = checkData[0];
  }
  if (typeof checkData[1] !== 'undefined') {
    datum[action] = checkData[1];
  }
  if (typeof checkData[2] !== 'undefined') {
    datum[orient] = checkData[2];
  }
  return datum;
}
 
// Keeping for potential use but not used now
// commenting to remove from test coverage calculation
// class Datum {
//   constructor(term, action, orient, ...data) {
//     Object.assign(this, _cleanDatum(term, action, orient, ...data));
//     this._term = term;
//     this._action = action;
//     this._orient = orient;
//   }
 
//   get(key) {
//     return this[key];
//   }
 
//   get t() {
//     return this.get(this._term);
//   }
 
//   get a() {
//     return this.get(this._action);
//   }
 
//   get o() {
//     return this.get(this._orient);
//   }
 
//   get unwrap() {
//     return {
//       [this._term]: this.t,
//       t: this.t,
//       [this._action]: this.a,
//       a: this.a,
//       [this._orient]: this.o,
//       o: this.o
//     };
//   }
// }
 
export default class AppCtx extends AppCtxRoot {
  constructor(term, action, orient, ...data) {
    super(term, action, orient);
    // TODO: figure out how to deal with associated AppCon data <-- what did I mean by this?
    // this.datum = new Datum(term, action, orient, ...data);
    this.datum = _cleanDatum(term, action, orient, ...data);
  }
 
  get data() {
    return this.datum;
  }
 
  unwrapCtx(verbose = false) {
    return !verbose
      ? { t: this.t, a: this.a, o: this.o }
      : { term: this.t, action: this.a, orient: this.o };
  }
}