graphology-utils
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -10,1 +10,3 @@ export * from './add-edge'; | ||
export {default as mergeStar} from './merge-star'; | ||
export {default as renameGraphKeys} from './rename-graph-keys'; | ||
export {default as updateGraphKeys} from './update-graph-keys'; |
@@ -22,1 +22,3 @@ /** | ||
exports.mergeStar = require('./merge-star.js'); | ||
exports.renameGraphKeys = require('./rename-graph-keys.js'); | ||
exports.updateGraphKeys = require('./update-graph-keys.js'); |
{ | ||
"name": "graphology-utils", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Miscellaneous utils for graphology.", | ||
@@ -17,3 +17,5 @@ "main": "index.js", | ||
"merge-path.js", | ||
"merge-star.js" | ||
"merge-star.js", | ||
"rename-graph-keys.js", | ||
"update-graph-keys.js" | ||
], | ||
@@ -24,2 +26,3 @@ "types": "./index.d.ts", | ||
"prepublish": "npm run lint && npm test", | ||
"prepublishOnly": "npm run prepublish", | ||
"test": "mocha test.js" | ||
@@ -53,6 +56,6 @@ }, | ||
"@yomguithereal/eslint-config": "^4.0.0", | ||
"eslint": "^7.15.0", | ||
"graphology": "^0.19.2", | ||
"graphology-types": "0.19.0", | ||
"mocha": "^8.2.1" | ||
"eslint": "^7.27.0", | ||
"graphology": "^0.19.3", | ||
"graphology-types": "0.19.2", | ||
"mocha": "^8.4.0" | ||
}, | ||
@@ -59,0 +62,0 @@ "eslintConfig": { |
@@ -31,2 +31,7 @@ [![Build Status](https://travis-ci.org/graphology/graphology-utils.svg)](https://travis-ci.org/graphology/graphology-utils) | ||
*Miscellaneous helpers* | ||
* [#.renameGraphKeys](#renamegraphkeys) | ||
* [#.updateGraphKeys](#updategraphkeys) | ||
### #.inferType | ||
@@ -182,1 +187,80 @@ | ||
* **star** *array*: array of nodes representing the star to add. | ||
### #.renameGraphKeys | ||
Function renaming the nodes & edges key of a graph using mappings and returning a new graph with renamed keys. | ||
```js | ||
import Graph from 'graphology'; | ||
import {renameGraphKeys} from 'graphology-utils'; | ||
// Alternatively, if you want to only load the relevant code: | ||
import renameGraphKeys from 'graphology-utils/rename-graph-keys'; | ||
const graph = new Graph(); | ||
graph.addNode('Martha'); | ||
graph.addNode('Catherine'); | ||
graph.addNode('John'); | ||
graph.addEdgeWithKey('M->C', 'Martha', 'Catherine'); | ||
graph.addEdgeWithKey('C->J', 'Catherine', 'John'); | ||
const renamedGraph = renameGraphKeys( | ||
graph, | ||
{Martha: 1, Catherine: 2, John: 3}, | ||
{'M->C': 'rel1', 'C->J': 'rel2'} | ||
); | ||
renamedGraph.nodes(); | ||
>>> [1, 2, 3]; | ||
renamedGraph.edges(); | ||
>>> ['rel1', 'rel2']; | ||
``` | ||
*Arguments* | ||
* **graph** *Graph*: target graph. | ||
* **nodeKeyMapping** *object*: A key/value map for the node key mapping. | ||
* **edgeKeyMapping** *?object*: A key/value map for the edge key mapping. | ||
### #.updateGraphKeys | ||
Function updating the nodes & edges key of a graph using functions and returning a new graph with updated keys. | ||
```js | ||
import Graph from 'graphology'; | ||
import {updateGraphKeys} from 'graphology-utils'; | ||
// Alternatively, if you want to only load the relevant code: | ||
import updateGraphKeys from 'graphology-utils/update-graph-keys'; | ||
const graph = new Graph(); | ||
graph.addNode('Martha'); | ||
graph.addNode('Catherine'); | ||
graph.addNode('John'); | ||
graph.addEdgeWithKey('M->C', 'Martha', 'Catherine'); | ||
graph.addEdgeWithKey('C->J', 'Catherine', 'John'); | ||
const updatedGraph = updateGraphKeys( | ||
graph, | ||
(key)=> { | ||
if (key === 'Martha') return 1; | ||
if (key === 'Catherine') return 2; | ||
return 3; | ||
}, | ||
(key) => { | ||
if (key === 'M->C') return 'rel1'; | ||
return 'rel2'; | ||
} | ||
); | ||
updatedGraph.nodes(); | ||
>>> [1, 2, 3]; | ||
updatedGraph.edges(); | ||
>>> ['rel1', 'rel2']; | ||
``` | ||
*Arguments* | ||
* **graph** *Graph*: target graph. | ||
* **nodeKeyUdater** *function*: A function to compute a new node key from the same arguments that would be given to [`#.forEachNode`](https://graphology.github.io/iteration.html#foreachnode). | ||
* **edgeKeyUpdater** *function*: A function to compute a new edge key from the same arguments that would be given to [`#.forEachEdge`](https://graphology.github.io/iteration.html#foreachedge). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22863
27
453
265