graphology-operators
Advanced tools
Comparing version 1.0.0 to 1.1.0
export {default as reverse} from './reverse.js'; | ||
export {default as toDirected} from './to-directed.js'; | ||
export {default as toSimple} from './to-simple.js'; | ||
export {default as toUndirected} from './to-undirected.js'; | ||
export {default as union} from './union.js'; |
@@ -8,4 +8,5 @@ /** | ||
exports.reverse = require('./reverse.js'); | ||
exports.toDirected = require('./to-directed.js'); | ||
exports.toSimple = require('./to-simple.js'); | ||
exports.toUndirected = require('./to-undirected.js'); | ||
exports.union = require('./union.js'); |
{ | ||
"name": "graphology-operators", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Miscellaneous operators for graphology.", | ||
@@ -10,2 +10,3 @@ "main": "index.js", | ||
"reverse.js", | ||
"to-directed.js", | ||
"to-undirected.js", | ||
@@ -42,4 +43,4 @@ "to-simple.js", | ||
"eslint": "^6.8.0", | ||
"graphology": "^0.16.0", | ||
"mocha": "^7.1.0" | ||
"graphology": "^0.16.1", | ||
"mocha": "^7.1.1" | ||
}, | ||
@@ -46,0 +47,0 @@ "eslintConfig": { |
@@ -25,2 +25,3 @@ [](https://travis-ci.org/graphology/graphology-operators) | ||
* [toDirected](#todirected) | ||
* [toSimple](#tosimple) | ||
@@ -62,2 +63,32 @@ * [toUndirected](#toundirected) | ||
### toDirected | ||
Returns the directed version of the given graph where any undirected edge will be now considered as mutual. | ||
Note that you can pass a function to merge edge attributes in case of mixed edges conflicts. This can be useful to sum weights and so on... | ||
If an already directed graph is passed, the function will only return a copy of it. | ||
```js | ||
import {toDirected} from 'graphology-operators'; | ||
// Alternatively, to load only the relevant code: | ||
import toDirected from 'graphology-operators/to-directed'; | ||
const directedGraph = toDirected(graph); | ||
// Using a merging function | ||
const directedGraph = toDirected(graph, (currentAttr, nextAttr) => { | ||
return { | ||
...currentAttr, | ||
weight: currentAttr.weight + nextAttr.weight | ||
}; | ||
}); | ||
``` | ||
*Arguments* | ||
* **graph** *Graph*: target graph. | ||
* **mergeOrOptions** *?function|object*: either a merging function or an options object: | ||
* **mergeEdge** *?function*: merging function to use. | ||
### toSimple | ||
@@ -79,3 +110,3 @@ | ||
Returns the undirected version of the given graph where any directed edge will be considered as now undirected. | ||
Returns the undirected version of the given graph where any directed edge will be now considered as undirected. | ||
@@ -82,0 +113,0 @@ Note that you can pass a function to merge edge attributes in case of mutual edges or mixed edges conflicts. This can be useful to sum weights and so on... |
@@ -28,4 +28,8 @@ /** | ||
// Adding undirected edges | ||
graph.forEachUndirectedEdge(function(edge) { | ||
undirectedGraph.importEdge(edge); | ||
graph.forEachUndirectedEdge(function(edge, attr, source, target) { | ||
undirectedGraph.addUndirectedEdge( | ||
source, | ||
target, | ||
Object.assign({}, attr) | ||
); | ||
}); | ||
@@ -48,6 +52,7 @@ | ||
var serializedEdge = graph.exportEdge(edge); | ||
serializedEdge.undirected = true; | ||
undirectedGraph.importEdge(serializedEdge); | ||
undirectedGraph.addUndirectedEdge( | ||
source, | ||
target, | ||
Object.assign({}, attr) | ||
); | ||
}); | ||
@@ -54,0 +59,0 @@ |
13294
15
226
135