npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphology-utils

v2.5.2

Published

Miscellaneous utils for graphology.

Downloads

880,509

Readme

Graphology Utils

Miscellaneous utility functions to be used with graphology.

Installation

npm install graphology-utils

Usage

Assertions

Introspection

Typical edge patterns

Miscellaneous helpers

#.isGraph

Function returning whether the given value is a graphology implementation's instance.

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

  • value any: value to test.

#.isGraphConstructor

Function returning whether the given value is a graphology constructor.

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

  • value any: value to test.

#.inferMulti

Function returning whether the given graph is truly multi, i.e. if we can find at least one occurrence of multiple edges of the same type and direction between two nodes.

import {inferMulti} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import inferMulti from 'graphology-utils/infer-multi';

const graph = new MultiGraph();
graph.addEdge(1, 2);

inferMulti(graph);
>>> false

graph.addEdge(1, 2);

inferMulti(graph);
>>> true

#.inferType

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 {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'

#.mergeClique

Function adding a clique to the given graph.

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]]

#.mergeCycle

Function adding a cycle to the given graph.

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

  • graph Graph: target graph.
  • cycle array: array of nodes representing the cycle to add.

#.mergePath

Function adding a path to the given graph.

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

  • graph Graph: target graph.
  • path array: array of nodes representing the path to add.

#.mergeStar

Function adding a star to the given graph.

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

  • graph Graph: target graph.
  • 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.

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.

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.
  • edgeKeyUpdater function: A function to compute a new edge key from the same arguments that would be given to #.forEachEdge.