All files / lib/stack utils.js

100% Statements 70/70
100% Branches 61/61
100% Functions 17/17
100% Lines 70/70
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 132 133 134 135 136 137  540x 39x 39x   15x 9x 9x 9x 9x   6x     18x 12x 12x 12x 12x   6x     6x               6x 6x 3x 3x     6x         21x 21x 21x 30x 30x 66x 9x 57x 3x   54x       21x 21x         147x 147x 12x   147x       33x 24x 24x   9x       12x 6x 6x   6x       12x 6x 6x   6x       6x 3x 3x   3x       6x 6x       6x 6x       6x 6x       30x 15x 15x 15x   15x 6x 6x 6x     9x   21x       3x    
export function transform(type) {
  return function () {
    this._query[type] = this._query[type] || {};
    switch (arguments.length) {
      case 1:
        if (Array.isArray(arguments[0]) || typeof arguments[0] === 'string') {
          let query = this._query[type].BASE || [];
          query = query.concat(arguments[0]);
          this._query[type].BASE = query;
          return this;
        }
        throw Error('Kindly provide valid parameters');
 
      case 2:
        if (typeof arguments[0] === 'string' && (Array.isArray(arguments[1]) || typeof arguments[1] === 'string')) {
          let query = this._query[type][arguments[0]] || [];
          query = query.concat(arguments[1]);
          this._query[type][arguments[0]] = query;
          return this;
        }
        throw Error('Kindly provide valid parameters');
 
      default:
        throw Error('Kindly provide valid parameters');
    }
  };
}
 
 
// merge two objects
export function merge(target, source) {
  const newTraget = target;
  if (target && source) {
    Object.keys(source).forEach((key) => {
      newTraget[key] = source[key];
    });
  }
  return newTraget;
}
 
// merge two objects
export function mergeDeep(oldTarget, oldSource) {
  const newTarget = oldTarget;
  const self = this;
  const _mergeRecursive = (anotherTarget, source) => {
    const target = anotherTarget;
    Object.keys(source).forEach((key) => {
      if (self._type(source[key]) === 'object' && self._type(target[key]) === self._type(source[key])) {
        _mergeRecursive(target[key], source[key]);
      } else if (self._type(source[key]) === 'array' && self._type(target[key]) === self._type(source[key])) {
        target[key] = target[key].concat(source[key]);
      } else {
        target[key] = source[key];
      }
    });
  };
  _mergeRecursive(newTarget, oldSource);
  return newTarget;
}
 
 
export function _type(val) {
  let __typeof = typeof val;
  if (__typeof === 'object' && Array.isArray(val)) {
    __typeof = 'array';
  }
  return __typeof;
}
 
export function addParam(key, value) {
  if (key && typeof key === 'string' && value && typeof value === 'string') {
    this._query[key] = value;
    return this;
  }
  throw Error('Kindly provide valid parameters.');
}
 
export function addQuery(key, value) {
  if (key && value && typeof key === 'string') {
    this._query[key] = value;
    return this;
  }
  throw Error('First argument should be a String.');
}
 
export function language(languageCode) {
  if (languageCode && typeof languageCode === 'string') {
    this._query.locale = languageCode;
    return this;
  }
  throw Error('Argument should be a String.');
}
 
export function environment(environmentCode) {
  if (environmentCode && typeof environmentCode === 'string') {
    this._query.environment = environmentCode;
    return this;
  }
  throw Error('Argument should be a String.');
}
 
export function includeOwner() {
  this._query.include_owner = true;
  return this;
}
 
export function includeContentType() {
  this._query.include_content_type = true;
  return this;
}
 
export function includeSchema() {
  this._query.include_schema = true;
  return this;
}
 
export function includeReference(val) {
  if (Array.isArray(val)) {
    for (let i = 0; i < val.length; i += 1) {
      this._query['include[]'] = this._query['include[]'] || [];
      this._query['include[]'] = this._query['include[]'].concat(val[i]);
    }
  } else if (typeof val === 'string') {
    for (let i = 0; i < arguments.length; i += 1) {
      this._query['include[]'] = this._query['include[]'] || [];
      this._query['include[]'] = this._query['include[]'].concat(arguments[i]);
    }
  } else {
    throw Error('Argument should be a String or an Array.');
  }
  return this;
}
 
export function getReferences() {
  return this.fetch(`get${this.constructor.module()}References`);
}