All files utils.ts

98% Statements 49/50
100% Branches 30/30
85.71% Functions 6/7
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    5x     57x 61x 73x     33x   19x 19x 19x   22x 22x   1x       21x   10x   11x   21x     9x   18x   18x   4x   55x     5x 5x 5x 10024x 10012x   9994x   18x   12x     10012x     5x   16x 16x 14x 14x 14x       15x 15x 210x   14x 14x   14x   1x     2x   14x 14x 14x      
import { ITrackerArg } from './types';
 
export function insertToArray(
  array: any[],
  item: any,
  position: 'START' | 'END' | 'BEFORE' | 'AFTER' = 'END',
  anchor: any | string | any[] | string[] = [],
  findItem: (item: string) => any = x => x
) {
  if (position === 'END') {
    array.push(item);
  } else if (position === 'AFTER' || position === 'BEFORE') {
    const arr = Array.isArray(anchor) ? anchor : [anchor];
    let index = position === 'AFTER' ? -1 : array.length + 1;
    let i = 0;
    while (i < arr.length) {
      const s = arr[i];
      const item = typeof s === 'string' ? findItem(s) : s;
      if (item === undefined) {
        throw new Error(
          `Skill with name ${s} could not be found, please ensure it is added before ${name}`
        );
      }
      const indexB = array.indexOf(item);
      if (position === 'AFTER') {
        index = indexB > index ? indexB : index;
      } else {
        index = indexB < index ? indexB : index;
      }
      i++;
    }
    if (position === 'AFTER') {
      index = index < 0 ? array.length : index + 1;
    }
    index = index > array.length ? array.length : index;
    // index = index < 0 ? 0 : index;
    array.splice(index, 0, item);
  } else {
    array.splice(0, 0, item);
  }
  return array;
}
 
let idCache0 = 0;
let idCache1 = 0;
const start = new Date(2019, 3 - 1, 28).getTime();
export function generateID(token: string = '') {
  const time = new Date().getTime() - start;
  if (idCache0 === time) {
    idCache1 = idCache1 + 1;
  } else {
    idCache0 = time;
    if (idCache1 !== 0) {
      idCache1 = 0;
    }
  }
  return `${idCache0}.${token}${idCache1}`;
}
 
export function treeizeTracker(
  tracker: ITrackerArg[],
  ids: (string | number)[] = [],
  map = (x: ITrackerArg, prev?: ITrackerArg): any => {
    const { parents, id, ...rest } = x;
    rest.time = prev ? x.time - prev.time : 0;
    return rest;
  },
  prev?: ITrackerArg
): any {
  const path = ids.join('.');
  return tracker
    .filter(x => x.parents.join('.') === path)
    .reduce((state, x) => {
      const rawId = x.id.split('.')[0];
      const children = treeizeTracker(tracker, [...ids, x.id], map, x);
 
      const item = map(x, prev);
      if (Object.keys(children).length) {
        item.children = children;
      }
      if (!state[rawId]) {
        state[rawId] = [];
      }
      state[rawId].push(item);
      prev = x;
      return state;
    }, {});
}