All files / src/components graph-util.js

95.12% Statements 39/41
86.84% Branches 33/38
100% Functions 10/10
95.12% Lines 39/41
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                                                              77x 77x 27x                 77x       77x 77x 16x     16x         77x       79x 18x     18x 18x   18x 5x 5x 5x 5x           2x 2x 1x 1x   1x       5x 2x 3x 2x   1x       215x 215x 427x 260x 167x 166x 258x 12x     1x 1x       215x          
// @flow
/*
  Copyright(c) 2018 Uber Technologies, Inc.
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import { type IEdge } from './edge';
import { type INode } from './node';
 
export type INodeMapNode = {
  node: INode;
  originalArrIndex: number;
  incomingEdges: IEdge[];
  outgoingEdges: IEdge[];
  parents: INode[];
  children: INode[];
};
 
class GraphUtils {
  static getNodesMap(arr: INode[], key: string) {
    const map = {};
    arr.forEach((item, index) => {
      map[`key-${item[key]}`] = {
        children: [],
        incomingEdges: [],
        node: item,
        originalArrIndex: index,
        outgoingEdges: [],
        parents: []
      };
    });
    return map;
  }
 
  static getEdgesMap(arr: IEdge[]) {
    const map = {};
    arr.forEach((item, index) => {
      Iif (!item.target) {
        return;
      }
      map[`${item.source || ''}_${item.target}`] = {
        edge: item,
        originalArrIndex: index
      };
    });
    return map;
  }
 
  static linkNodesAndEdges(nodesMap: any, edges: IEdge[]) {
    edges.forEach((edge) => {
      Iif (!edge.target) {
        return;
      }
      const nodeMapSourceNode = nodesMap[`key-${edge.source || ''}`];
      const nodeMapTargetNode = nodesMap[`key-${edge.target}`];
      // avoid an orphaned edge
      if (nodeMapSourceNode && nodeMapTargetNode) {
        nodeMapSourceNode.outgoingEdges.push(edge);
        nodeMapTargetNode.incomingEdges.push(edge);
        nodeMapSourceNode.children.push(nodeMapTargetNode);
        nodeMapTargetNode.parents.push(nodeMapSourceNode);
      }
    });
  }
 
  static removeElementFromDom(id: string) {
    const container = document.getElementById(id);
    if (container && container.parentNode) {
      container.parentNode.removeChild(container);
      return true;
    }
    return false;
  }
 
  static findParent(element: any, selector: string) {
    if (element && element.matches && element.matches(selector)) {
      return element;
    } else if (element && element.parentNode) {
      return GraphUtils.findParent(element.parentNode, selector);
    }
    return null;
  }
 
  static classNames(...args: any[]) {
    let className = '';
    for (const arg of args) {
      if (typeof arg === 'string' || typeof arg === 'number') {
        className += ` ${arg}`;
      } else if (typeof arg === 'object' && !Array.isArray(arg) && arg !== null) {
        Object.keys(arg).forEach((key) => {
          if (Boolean(arg[key])) {
            className += ` ${key}`;
          }
        });
      } else Eif (Array.isArray(arg)) {
        className += ` ${arg.join(' ')}`;
      }
    }
 
    return className.trim();
  }
}
 
export default GraphUtils;