All files utils.ts

100% Statements 47/47
95.45% Branches 21/22
100% Functions 12/12
100% Lines 44/44
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 138 139 140 141 142 143 144 145 146                  1x 1016x     536x 536x                           1x 453x                   1x 212x             212x 383x     212x 57x   57x 38x     57x 40x 40x       212x     45x 45x 45x       212x     34x 34x 34x       212x     1x                       182x 91x 182x 142x     139x 139x         91x                       1x 21x 1x     20x                   1x 12x 12x 47x 47x     12x    
import IDictionary from './interfaces/IDictionary';
import * as JsonApi from './interfaces/JsonApi';
 
/**
 * Iterate trough object keys
 *
 * @param {object} obj - Object that needs to be iterated
 * @param {Function} fn - Function that should be called for every iteration
 */
export function objectForEach(obj: object, fn: Function): void {
  for (const key in obj) {
 
    /* istanbul ignore else */
    if (obj.hasOwnProperty(key)) {
      fn(key);
    }
  }
}
 
/**
 * Iterate trough one item or array of items and call the defined function
 *
 * @export
 * @template T
 * @param {(object|Array<object>)} data - Data which needs to be iterated
 * @param {Function} fn - Function that needs to be callse
 * @returns {(T|Array<T>)} - The result of iteration
 */
export function mapItems<T>(data: object|Array<object>, fn: Function): T|Array<T> {
  return data instanceof Array ? data.map((item) => fn(item)) : fn(data);
}
 
/**
 * Flatten the JSON API record so it can be inserted into the model
 *
 * @export
 * @param {IJsonApiRecord} record - original JSON API record
 * @returns {IDictionary<any>} - Flattened object
 */
export function flattenRecord(record: JsonApi.IRecord): IDictionary<any> {
  const data: IDictionary<any> = {
    __internal: {
      id: record.id,
      type: record.type,
    },
  };
 
  objectForEach(record.attributes, (key) => {
    data[key] = record.attributes[key];
  });
 
  objectForEach(record.relationships, (key) => {
    const rel: JsonApi.IRelationship = record.relationships[key];
 
    if (rel.meta) {
      data[`${key}Meta`] = rel.meta;
    }
 
    if (rel.links) {
      data.__internal.relationships = data.__internal.relationships || {};
      data.__internal.relationships[key] = rel.links;
    }
  });
 
  objectForEach(record.links, (key) => {
 
    /* istanbul ignore else */
    if (record.links[key]) {
      data.__internal.links = data.__internal.links || {};
      data.__internal.links[key] = record.links[key];
    }
  });
 
  objectForEach(record.meta, (key) => {
 
    /* istanbul ignore else */
    if (record.meta[key]) {
      data.__internal.meta = data.__internal.meta || {};
      data.__internal.meta[key] = record.meta[key];
    }
  });
 
  return data;
}
 
export const isBrowser: boolean = (typeof window !== 'undefined');
 
/**
 * Assign objects to the target object
 * Not a complete implementation (Object.assign)
 * Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign polyfill
 *
 * @private
 * @param {object} target - Target object
 * @param {Array<object>} args - Objects to be assigned
 * @returns
 */
export function assign(target: object, ...args: Array<object>) {
  args.forEach((nextSource: object) => {
    if (nextSource != null) {
      for (const nextKey in nextSource) {
 
        /* istanbul ignore else */
        if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
          target[nextKey] = nextSource[nextKey];
        }
      }
    }
  });
  return target;
}
 
/**
 * Returns the value if it's not a function. If it's a function
 * it calls it.
 *
 * @export
 * @template T
 * @param {(T|(() => T))} target can be  anything or function
 * @returns {T} value
 */
export function getValue<T>(target: T|(() => T)): T {
  if (typeof target === 'function') {
    return target();
  }
 
  return target;
}
 
/**
 * Get all object keys
 *
 * @export
 * @param {object} obj Object to process
 * @returns {Array<string>} List of object keys
 */
export function keys(obj: object): Array<string> {
  const keyList = [];
  for (const key in obj) {
    Eif (obj.hasOwnProperty(key)) {
      keyList.push(key);
    }
  }
  return keyList;
}