graphology-utils
Advanced tools
| import Graph, {Attributes, NodeKey, EdgeKey} from 'graphology-types'; | ||
| export function addEdge<EdgeAttributes extends Attributes = Attributes>( | ||
| graph: Graph, | ||
| undirected: boolean, | ||
| key: EdgeKey | null | undefined, | ||
| source: NodeKey, | ||
| target: NodeKey, | ||
| attributes?: EdgeAttributes | ||
| ): void; | ||
| export function copyEdge<EdgeAttributes extends Attributes = Attributes>( | ||
| graph: Graph, | ||
| undirected: boolean, | ||
| key: EdgeKey | null | undefined, | ||
| source: NodeKey, | ||
| target: NodeKey, | ||
| attributes?: EdgeAttributes | ||
| ): void; | ||
| export function mergeEdge<EdgeAttributes extends Attributes = Attributes>( | ||
| graph: Graph, | ||
| undirected: boolean, | ||
| key: EdgeKey | null | undefined, | ||
| source: NodeKey, | ||
| target: NodeKey, | ||
| attributes?: EdgeAttributes | ||
| ): void; | ||
| export function updateEdge<EdgeAttributes extends Attributes = Attributes>( | ||
| graph: Graph, | ||
| undirected: boolean, | ||
| key: EdgeKey | null | undefined, | ||
| source: NodeKey, | ||
| target: NodeKey, | ||
| attributes?: EdgeAttributes | ||
| ): void; |
+69
| /** | ||
| * Graphology Edge Adders | ||
| * ======================= | ||
| * | ||
| * Generic edge addition functions that can be used to avoid nasty repetitive | ||
| * conditions. | ||
| */ | ||
| exports.addEdge = function addEdge(graph, undirected, key, source, target, attributes) { | ||
| if (undirected) { | ||
| if (key === null || key === undefined) | ||
| graph.addUndirectedEdge(source, target, attributes); | ||
| else | ||
| graph.addUndirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| else { | ||
| if (key === null || key === undefined) | ||
| graph.addDirectedEdge(source, target, attributes); | ||
| else | ||
| graph.addDirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| }; | ||
| exports.copyEdge = function copyEdge(graph, undirected, key, source, target, attributes) { | ||
| attributes = Object.assign({}, attributes); | ||
| if (undirected) { | ||
| if (key === null || key === undefined) | ||
| graph.addUndirectedEdge(source, target, attributes); | ||
| else | ||
| graph.addUndirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| else { | ||
| if (key === null || key === undefined) | ||
| graph.addDirectedEdge(source, target, attributes); | ||
| else | ||
| graph.addDirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| }; | ||
| exports.mergeEdge = function mergeEdge(graph, undirected, key, source, target, attributes) { | ||
| if (undirected) { | ||
| if (key === null || key === undefined) | ||
| graph.mergeUndirectedEdge(source, target, attributes); | ||
| else | ||
| graph.mergeUndirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| else { | ||
| if (key === null || key === undefined) | ||
| graph.mergeDirectedEdge(source, target, attributes); | ||
| else | ||
| graph.mergeDirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| }; | ||
| exports.updateEdge = function updateEdge(graph, undirected, key, source, target, attributes) { | ||
| if (undirected) { | ||
| if (key === null || key === undefined) | ||
| graph.updateUndirectedEdge(source, target, attributes); | ||
| else | ||
| graph.updateUndirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| else { | ||
| if (key === null || key === undefined) | ||
| graph.updateDirectedEdge(source, target, attributes); | ||
| else | ||
| graph.updateDirectedEdgeWithKey(key, source, target, attributes); | ||
| } | ||
| }; |
| import Graph, {NodeKey, Attributes} from 'graphology-types'; | ||
| export function copyNode<NodeAttributes extends Attributes = Attributes>( | ||
| graph: Graph, | ||
| key: NodeKey, | ||
| attributes?: NodeAttributes | ||
| ): void; |
+11
| /** | ||
| * Graphology Node Adders | ||
| * ======================= | ||
| * | ||
| * Generic node addition functions that can be used to avoid nasty repetitive | ||
| * conditions. | ||
| */ | ||
| exports.copyNode = function(graph, key, attributes) { | ||
| attributes = Object.assign({}, attributes); | ||
| graph.addNode(key, attributes); | ||
| }; |
+2
-1
@@ -0,1 +1,3 @@ | ||
| export * from './add-edge'; | ||
| export * from './add-node'; | ||
| export {default as inferType} from './infer-type'; | ||
@@ -8,2 +10,1 @@ export {default as isGraph} from './is-graph'; | ||
| export {default as mergeStar} from './merge-star'; | ||
| export {default as subGraph} from './subgraph'; |
+8
-1
@@ -7,2 +7,10 @@ /** | ||
| */ | ||
| var addEdgeModule = require('./add-edge'); | ||
| var addNodeModule = require('./add-node'); | ||
| exports.addEdge = addEdgeModule.addEdge; | ||
| exports.copyEdge = addEdgeModule.copyEdge; | ||
| exports.mergeEdge = addEdgeModule.mergeEdge; | ||
| exports.updateEdge = addEdgeModule.updateEdge; | ||
| exports.copyNode = addNodeModule.updateNode; | ||
| exports.inferType = require('./infer-type.js'); | ||
@@ -15,2 +23,1 @@ exports.isGraph = require('./is-graph.js'); | ||
| exports.mergeStar = require('./merge-star.js'); | ||
| exports.subGraph = require('./subgraph.js'); |
+6
-5
| { | ||
| "name": "graphology-utils", | ||
| "version": "1.8.0", | ||
| "version": "2.0.0", | ||
| "description": "Miscellaneous utils for graphology.", | ||
@@ -8,2 +8,4 @@ "main": "index.js", | ||
| "*.d.ts", | ||
| "add-edge.js", | ||
| "add-node.js", | ||
| "index.js", | ||
@@ -16,4 +18,3 @@ "infer-type.js", | ||
| "merge-path.js", | ||
| "merge-star.js", | ||
| "subgraph.js" | ||
| "merge-star.js" | ||
| ], | ||
@@ -52,4 +53,4 @@ "types": "./index.d.ts", | ||
| "@yomguithereal/eslint-config": "^4.0.0", | ||
| "eslint": "^7.13.0", | ||
| "graphology": "^0.19.1", | ||
| "eslint": "^7.15.0", | ||
| "graphology": "^0.19.2", | ||
| "graphology-types": "0.19.0", | ||
@@ -56,0 +57,0 @@ "mocha": "^8.2.1" |
+9
-39
@@ -15,5 +15,13 @@ [](https://travis-ci.org/graphology/graphology-utils) | ||
| * [#.inferType](#infertype) | ||
| *Assertions* | ||
| * [#.isGraph](#isgraph) | ||
| * [#.isGraphConstructor](#isgraphconstructor) | ||
| *Introspection* | ||
| * [#.inferType](#infertype) | ||
| *Typical edge patterns* | ||
| * [#.mergeClique](#mergeclique) | ||
@@ -23,3 +31,2 @@ * [#.mergeCycle](#mergecycle) | ||
| * [#.mergeStar](#mergestar) | ||
| * [#.subGraph](#subgraph) | ||
@@ -176,38 +183,1 @@ ### #.inferType | ||
| * **star** *array*: array of nodes representing the star to add. | ||
| ### #.subGraph | ||
| Function returning the subgraph corresponding to the given list of nodes. | ||
| ```js | ||
| import Graph from 'graphology'; | ||
| import {subGraph} from 'graphology-utils'; | ||
| // Alternatively, if you want to only load the relevant code: | ||
| import subGraph from 'graphology-utils/subgraph'; | ||
| const graph = new Graph(); | ||
| graph.addNode('Dale'); | ||
| graph.addNode('Laura'); | ||
| graph.addNode('Norma'); | ||
| graph.addNode('Shelly'); | ||
| graph.addEdge('Dale', 'Laura'); | ||
| graph.addEdge('Dale', 'Norma'); | ||
| graph.addEdge('Shelly', 'Laura'); | ||
| graph.addUndirectedEdge('Norma', 'Shelly'); | ||
| subGraphResult = subGraph(graph, ['Dale', 'Laura']); | ||
| subGraphResult.nodes(); | ||
| >>> [ 'Dale', 'Laura' ] | ||
| subGraphResult.forEachEdge( | ||
| (edge, attributes, source, target) => { | ||
| console.log(`Edge from ${source} to ${target}`); | ||
| }); | ||
| >>> 'Edge from Dale to Laura' | ||
| ``` | ||
| *Arguments* | ||
| * **graph** *Graph*: source graph. | ||
| * **nodes** *array|Set|function*: the array, set or function defining the subgraph's nodes. | ||
| import Graph, {NodeKey, Attributes} from 'graphology-types'; | ||
| type SubgraphPredicateFunction = (key: string, attributes: Attributes) => boolean; | ||
| type SubgraphNodes = Array<NodeKey> | Set<NodeKey> | SubgraphPredicateFunction; | ||
| export default function subgraph(graph: Graph, nodes: SubgraphNodes): Graph; |
-81
| /** | ||
| * Graphology Sub Graph | ||
| * ===================== | ||
| * | ||
| * Function returning the subgraph composed of the nodes passed as parameters. | ||
| */ | ||
| /** | ||
| * Returning the subgraph composed of the nodes passed as parameters. | ||
| * | ||
| * @param {Graph} graph - Graph containing the subgraph. | ||
| * @param {array} nodes - Array, set or function defining the nodes wanted in the subgraph. | ||
| */ | ||
| module.exports = function subGraph(graph, nodes) { | ||
| var nodesSet; | ||
| var subGraphResult = graph.nullCopy(); | ||
| if (Array.isArray(nodes)) { | ||
| nodesSet = new Set(nodes); | ||
| } | ||
| else if (nodes instanceof Set) { | ||
| nodesSet = nodes; | ||
| } | ||
| else if (typeof nodes === 'function') { | ||
| nodesSet = new Set(); | ||
| graph.forEachNode(function(key, attrs) { | ||
| if (nodes(key, attrs)) { | ||
| nodesSet.add(key); | ||
| } | ||
| }); | ||
| } | ||
| else { | ||
| throw new Error( | ||
| 'The argument "nodes" is neither an array, nor a set, nor a function.' | ||
| ); | ||
| } | ||
| if (nodesSet.size === 0) return subGraphResult; | ||
| var insertedSelfloops = new Set(); // Useful to check if a selfloop has already been inserted or not | ||
| nodesSet.forEach(function(node) { | ||
| // Nodes addition | ||
| if (!graph.hasNode(node)) | ||
| throw new Error('graphology-utils/subgraph: the "' + node + '" node is not present in the graph.'); | ||
| subGraphResult.addNode(node, graph.getNodeAttributes(node)); | ||
| }); | ||
| nodesSet.forEach(function(node) { | ||
| // Edges addition | ||
| graph.forEachOutEdge(node, function(edge, attributes, source, target) { | ||
| if (nodesSet.has(target)) { | ||
| subGraphResult.importEdge(graph.exportEdge(edge)); | ||
| } | ||
| }); | ||
| graph.forEachUndirectedEdge(node, function( | ||
| edge, | ||
| attributes, | ||
| source, | ||
| target | ||
| ) { | ||
| if (source !== node) { | ||
| var tmp = source; | ||
| source = target; | ||
| target = tmp; | ||
| } | ||
| if (nodesSet.has(target)) { | ||
| if (source === target && !insertedSelfloops.has(edge)) { | ||
| subGraphResult.importEdge(graph.exportEdge(edge)); | ||
| insertedSelfloops.add(edge); | ||
| } | ||
| else if (source > target) { | ||
| subGraphResult.importEdge(graph.exportEdge(edge)); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| return subGraphResult; | ||
| }; |
16493
3.53%23
9.52%328
15.49%181
-14.22%