(
reactFlowNodes: IReactFlowNode[],
reactFlowEdges: IReactFlowEdge[],
options?: { isNonDirected?: boolean; isReversed?: boolean }
)
| 153 | * @param {{ isNonDirected?: boolean, isReversed?: boolean }} options |
| 154 | */ |
| 155 | export const constructGraphs = ( |
| 156 | reactFlowNodes: IReactFlowNode[], |
| 157 | reactFlowEdges: IReactFlowEdge[], |
| 158 | options?: { isNonDirected?: boolean; isReversed?: boolean } |
| 159 | ) => { |
| 160 | const nodeDependencies = {} as INodeDependencies |
| 161 | const graph = {} as INodeDirectedGraph |
| 162 | |
| 163 | for (let i = 0; i < reactFlowNodes.length; i += 1) { |
| 164 | const nodeId = reactFlowNodes[i].id |
| 165 | nodeDependencies[nodeId] = 0 |
| 166 | graph[nodeId] = [] |
| 167 | } |
| 168 | |
| 169 | if (options && options.isReversed) { |
| 170 | for (let i = 0; i < reactFlowEdges.length; i += 1) { |
| 171 | const source = reactFlowEdges[i].source |
| 172 | const target = reactFlowEdges[i].target |
| 173 | |
| 174 | if (Object.prototype.hasOwnProperty.call(graph, target)) { |
| 175 | graph[target].push(source) |
| 176 | } else { |
| 177 | graph[target] = [source] |
| 178 | } |
| 179 | |
| 180 | nodeDependencies[target] += 1 |
| 181 | } |
| 182 | |
| 183 | return { graph, nodeDependencies } |
| 184 | } |
| 185 | |
| 186 | for (let i = 0; i < reactFlowEdges.length; i += 1) { |
| 187 | const source = reactFlowEdges[i].source |
| 188 | const target = reactFlowEdges[i].target |
| 189 | |
| 190 | if (Object.prototype.hasOwnProperty.call(graph, source)) { |
| 191 | graph[source].push(target) |
| 192 | } else { |
| 193 | graph[source] = [target] |
| 194 | } |
| 195 | |
| 196 | if (options && options.isNonDirected) { |
| 197 | if (Object.prototype.hasOwnProperty.call(graph, target)) { |
| 198 | graph[target].push(source) |
| 199 | } else { |
| 200 | graph[target] = [source] |
| 201 | } |
| 202 | } |
| 203 | nodeDependencies[target] += 1 |
| 204 | } |
| 205 | |
| 206 | return { graph, nodeDependencies } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get starting node and check if flow is valid |
no test coverage detected