All files / src/Graph toJSON.ts

100% Statements 35/35
100% Branches 14/14
100% Functions 8/8
100% Lines 35/35

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 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 147 148 149 150 151 152 153 154 155 156                            16x     23x 23x 23x 23x         23x 21x   23x 22x   23x                                                                 16x     23x 8x 8x             8x 4x     8x 4x     8x                                 16x               23x                   23x 21x     23x                 16x               23x 23x 2x   23x 23x 23x 1x     23x 8x   23x    
import Graph, { GraphOption } from '.';
 
type JSONNode<NodeIDType = string, NodeType = any> = {
  id: NodeIDType;
  value?: NodeType;
  parent?: NodeIDType;
};
 
/**
 * @description Convert a graph's node to JSON.
 * @description.zh-CN 转换图的节点为 JSON。
 * @param graph
 * @returns
 */
const nodeToJSON = <NodeIDType = string, NodeType = any>(
  graph: Graph<NodeIDType, NodeType, any, any>,
) => {
  return graph.nodes().map((n) => {
    const value = graph.node(n);
    const parent = graph.parent(n);
    const node = {
      id: n,
      value,
      parent,
    };
    if (node.value === undefined) {
      delete node.value;
    }
    if (node.parent === undefined) {
      delete node.parent;
    }
    return node as JSONNode<NodeIDType, NodeType>;
  });
};
 
type JSONEdge<NodeIDType = string, EdgeType = Record<string, any>> = {
  /**
   * @description The source node id.
   * @description.zh-CN 源节点 id。
   */
  v: NodeIDType;
  /**
   * @description The target node id.
   * @description.zh-CN 目标节点 id。
   */
  w: NodeIDType;
  /**
   * @description The edge value.
   * @description.zh-CN 边的值。
   */
  value?: EdgeType;
  /**
   * @description The edge name.
   * @description.zh-CN 边的名称。
   */
  name?: string;
};
 
/**
 * @description Convert all graph's edges to JSON.
 * @description.zh-CN 转换图的所有边为 JSON。
 * @param graph
 * @returns
 */
const edgeToJSON = <NodeIDType = string, EdgeType = Record<string, any>>(
  graph: Graph<NodeIDType, any, EdgeType, any>,
) => {
  return graph.edges().map((edge) => {
    const value = graph.edge(edge);
    const e = {
      v: edge.v,
      w: edge.w,
      value,
      name: edge.name,
    };
 
    if (e.name === undefined) {
      delete e.name;
    }
 
    if (e.value === undefined) {
      delete e.value;
    }
 
    return e as JSONEdge<NodeIDType, EdgeType>;
  });
};
 
type JSONGraph<NodeIDType, NodeType, EdgeType, GraphType> = {
  options: GraphOption;
  nodes: JSONNode<NodeIDType, NodeType>[];
  edges: JSONEdge<NodeIDType, EdgeType>[];
  value?: GraphType;
};
 
/**
 * @description Convert a graph to JSON.
 * @description.zh-CN 转换图为 JSON。
 * @param graph
 * @returns
 */
export const write = <
  NodeIDType = string,
  NodeType = Record<string, any>,
  EdgeType = Record<string, any>,
  GraphType = string,
>(
  graph: Graph<NodeIDType, NodeType, EdgeType, GraphType>,
) => {
  const json: JSONGraph<NodeIDType, NodeType, EdgeType, GraphType> = {
    options: {
      directed: graph.isDirected(),
      multigraph: graph.isMultigraph(),
      compound: graph.isCompound(),
    },
    nodes: nodeToJSON<NodeIDType, NodeType>(graph),
    edges: edgeToJSON<NodeIDType, EdgeType>(graph),
    value: graph.graph(),
  };
  if (json.value === undefined) {
    delete json.value;
  }
 
  return json;
};
 
/**
 * @description read a graph from JSON.
 * @description.zh-CN 从 JSON 读取图。
 * @param json
 * @returns
 */
export const read = <
  NodeIDType = string,
  NodeType = Record<string, any>,
  EdgeType = Record<string, any>,
  GraphType = string,
>(
  json: JSONGraph<NodeIDType, NodeType, EdgeType, GraphType>,
) => {
  const graph = new Graph<NodeIDType, NodeType, EdgeType, GraphType>(json.options);
  if (json.value !== undefined) {
    graph.setGraph(json.value);
  }
  json.nodes.forEach((entry) => {
    graph.setNode(entry.id, entry.value);
    if (entry.parent) {
      graph.setParent(entry.id, entry.parent);
    }
  });
  json.edges.forEach((entry) => {
    graph.setEdge(entry.v, entry.w, entry.value, entry.name);
  });
  return graph;
};