Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
graphology-utils
Advanced tools
The graphology-utils npm package provides a set of utility functions for working with graphology, a JavaScript library for manipulating and analyzing graphs. These utilities help in performing common graph operations such as traversal, manipulation, and analysis.
Graph Traversal
This feature allows you to traverse through all nodes in a graph and perform operations on them. The code sample demonstrates how to use the `forEachNode` utility to log each node and its attributes.
const { forEachNode } = require('graphology-utils');
const Graph = require('graphology');
const graph = new Graph();
graph.addNode('John');
graph.addNode('Martha');
graph.addEdge('John', 'Martha');
forEachNode(graph, (node, attributes) => {
console.log(node, attributes);
});
Graph Manipulation
This feature allows you to merge multiple graphs into one. The code sample demonstrates how to use the `mergeGraphs` utility to combine two graphs into a single graph.
const { mergeGraphs } = require('graphology-utils');
const Graph = require('graphology');
const graph1 = new Graph();
graph1.addNode('Alice');
const graph2 = new Graph();
graph2.addNode('Bob');
const mergedGraph = mergeGraphs(graph1, graph2);
console.log(mergedGraph.nodes());
Graph Analysis
This feature allows you to perform analysis on the graph, such as calculating the degree of a node. The code sample demonstrates how to use the `degree` utility to find the degree of a node named 'Alice'.
const { degree } = require('graphology-utils');
const Graph = require('graphology');
const graph = new Graph();
graph.addNode('Alice');
graph.addNode('Bob');
graph.addEdge('Alice', 'Bob');
const aliceDegree = degree(graph, 'Alice');
console.log(aliceDegree);
Cytoscape is a graph theory library for analysis and visualization. It provides a rich set of features for graph manipulation and analysis, similar to graphology-utils, but also includes advanced visualization capabilities.
d3-graphviz is a library for rendering graphs using Graphviz and D3.js. While it focuses more on visualization, it also provides utilities for graph manipulation and analysis, making it somewhat comparable to graphology-utils.
Sigma is a JavaScript library dedicated to graph drawing. It provides functionalities for graph manipulation and visualization, similar to graphology-utils, but with a stronger emphasis on rendering and interaction.
Miscellaneous utility functions to be used with graphology
.
npm install graphology-utils
Assertions
Introspection
Typical edge patterns
Miscellaneous helpers
Function returning the inferred type of the given graph. This function is useful to check whether a given mixed graph is in fact a mere directed
or undirected
graph based on its actual edges.
import Graph from 'graphology';
import {inferType} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import inferType from 'graphology-utils/infer-type';
const graph = new Graph();
graph.addUndirectedEdge(1, 2);
inferType(graph);
>>> 'directed'
Function returning whether the given value is a graphology
implementation's instance.
import Graph from 'graphology';
import {isGraph} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import isGraph from 'graphology-utils/is-graph';
const graph = new Graph();
isGraph(graph);
>>> true
isGraph(45);
>>> false
isGraph({hello: 'world'});
>>> false
Arguments
Function returning whether the given value is a graphology
constructor.
import Graph from 'graphology';
import {isGraphConstructor} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import isGraphConstructor from 'graphology-utils/is-graph-constructor';
isGraphConstructor(Graph);
>>> true
isGraphConstructor(45);
>>> false
isGraphConstructor(new Graph());
>>> false
Arguments
Function adding a clique to the given graph.
import Graph from 'graphology';
import {mergeClique} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergeClique from 'graphology-utils/merge-clique';
const graph = new Graph();
mergeClique(graph, [1, 2, 3]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [1, 3], [2, 3]]
Function adding a cycle to the given graph.
import Graph from 'graphology';
import {mergeCycle} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergeCycle from 'graphology-utils/merge-cycle';
const graph = new Graph();
mergeCycle(graph, [1, 2, 3, 4, 5]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [2, 3], [3, 4], [4, 5], [5, 1]]
Arguments
Function adding a path to the given graph.
import Graph from 'graphology';
import {mergePath} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergePath from 'graphology-utils/merge-path';
const graph = new Graph();
mergePath(graph, [1, 2, 3, 4, 5]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [2, 3], [3, 4], [4, 5]]
Arguments
Function adding a star to the given graph.
import Graph from 'graphology';
import {mergeStar} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergeStar from 'graphology-utils/merge-star';
const graph = new Graph();
mergeStar(graph, [1, 2, 3, 4, 5]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [1, 3], [1, 4], [1, 5]]
Arguments
Function renaming the nodes & edges key of a graph using mappings and returning a new graph with renamed keys.
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
Function updating the nodes & edges key of a graph using functions and returning a new graph with updated keys.
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
#.forEachNode
.#.forEachEdge
.FAQs
Miscellaneous utils for graphology.
The npm package graphology-utils receives a total of 237,628 weekly downloads. As such, graphology-utils popularity was classified as popular.
We found that graphology-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.