@tokens-studio/graph-engine-migration
Version:
A package to upgrade old graph engine files to the new format
52 lines • 1.69 kB
JavaScript
/**
* Finds all nodes of a specified type in the graph
* @param graph
* @param type
* @returns
*/
export const findNodesOfType = (graph, type) => Object.values(graph.nodes).filter(node => node.factory.type === type);
export const findOutEdges = (graph, id) => Object.values(graph.edges).filter(edge => edge.source === id);
/**
* Converts the array of nodes in the graph to a lookup for O(1) performance
* @param graph
* @returns
*/
export const toNodeLookup = (graph) => Object.values(graph.nodes).reduce((acc, node) => {
acc[node.id] = node;
return acc;
}, {});
/**
* Converts the array of edge in the graph to a lookup for O(1) performance
* @param graph
* @returns
*/
export const toEdgeLookup = (graph) => Object.values(graph.edges).reduce((acc, edge) => {
acc[edge.id] = edge;
return acc;
}, {});
/**
* Finds all nodes in the graph that have a specified source and have and edge connecting to the target
* @param graph
* @param sourceType
* @param target
* @returns
*/
export const findSourceToTargetOfType = (graph, sourceType, targetType) => {
const nodeLookup = toNodeLookup(graph);
return Object.values(graph.nodes)
.filter(node => node.factory.type === sourceType)
.reduce((acc, node) => {
const edges = findOutEdges(graph, node.id);
const foundTargets = edges
.filter(edge => nodeLookup[edge.target].factory.type === targetType)
.map(edge => {
return {
source: node,
edge,
target: nodeLookup[edge.target]
};
});
return acc.concat(foundTargets);
}, []);
};
//# sourceMappingURL=utils.js.map