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

graph-utils

v0.1.0

Published

Utilities for working with graphs in javascript.

Downloads

6

Readme

graph-utils

js-standard-style

What is it?

install

npm i --S graph-utils

usage

graph api

The fundamental entry point is a graph.

const grutils = require('graph-utils')

const g = new grutils.Graph()
Graph.addVertex(id:String, data:*=null):Vertex

Adds a new vertex to the graph and returns the reference.

v = g.addVertex('1', 'foo')

g.vertices // { '1': Vertex { } }

v.id // '1'
v._ // 'foo'
v.edges.outgoing // []
v.edges.incoming // []
Graph.getVertex(id:String):Vertex

Get a reference to the Vertex with vertex.id === id. Note that at the current time there is no equivalent getEdge(id:String) api.


g.getVertex('1') // v { id: '1', _: 'foo' }
Graph.addEdge(weight:Number, sourceId:String, targetId:String, data:*=null):Edge

Adds a new edge to the graph originating from one vertex to another. At your option, you may add some metadata to the edge.

v1 = g.addVertex('1', 'SanFrancisco')
v2 = g.addVertex('2', 'Oakland')

e1 = g.addEdge(3.1, v1.id, v2.id, 'Bay Bridge')

e1._  // 'Bay Bridge'
e1.weight // 3.1
e1.source // '1'
e1.target // '2'

g.edges // [e]
v1.edges.outgoing // [e]
v2.edges.incoming // [e]
Graph.removeEdge(edge:Edge):Edge

Remove an edge from the graph by reference. The edge will be unlinked from edge.source and edge.target.

// Bye-bye Bay Bridge!
g.removeEdge(e1)

g.edges // []
v1.edges.outgoing // []
v2.edges.incoming // []
Graph.removeVertex(vertex:Vertex):Vertex

Remove a vertex from the graph by reference. Note that all edges that have this vertex as edge.source or edge.target will be removed from the graph as well.

v1 = g.addVertex('1', 'SanFrancisco')
v2 = g.addVertex('2', 'Oakland')

e1 = g.addEdge(3.1, v1.id, v2.id, 'Bay Bridge')

// Bye-bye Oakland and Bay Bridge!
g.removeVertex(v2)

g.edges // []
g.vertices // { '1': Vertex { } }

v1.edges.outgoing // []
Graph.setEdge(weight:Number, sourceId:String, sourceIndex:Number, targetId:String, targetIndex:Number=0, data:*=null)

Replace a specific edge(s) in the graph: g.getVertex(sourceId).edges.outgoing[sourceIndex] and link the new edge to g.getVertex(targetId).edges.incoming[targetIndex]. You shouldn't need to call this directly, but it is used by the Tree/TreeNode implementation under the covers. See the lib/node.js for more details.

tree/tree node api

The fundamental entries are trees and nodes, respectively. Underneath the hood the tree is implemented as a graph and unfortunately it is required to pass the reference to the underlying Graph object as the first parameter when constructing new trees and nodes. Note that the underlying vertex of a node has a reference to the node in vertex._.

const grutils = require('graph-utils')

const g = new grutils.Graph()
const tree = new grutils.Tree(g, '1', { 'foo': 'bar' })
const node1 = new grutils.Node(g, '2', 'baz')

tree.root // Node { id: '1', _: { 'foo': 'bar' } }
node1 // Node { id: '2', _: 'baz' }

g.vertices // { '1': Vertex { }, '2': Vertex { } }

tree.root.left = node1

tree.root.left // Node { id: '2', _: 'baz' }

g.edges // [ Edge { weight: 1, source: '1', target: '2' } ]

Note that while some core functionality is there, this project is still very much a work in progress.

How do I work on it?

development

git clone https://github.com/sterpe/graph-utils.git
cd graph-utils
make

build

make build

test

make JEST_FLAGS=--coverage test

lint

make lint

Consult the Makefile for further details.