Socket
Socket
Sign inDemoInstall

graphs

Package Overview
Dependencies
16
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphs

Intuitive data structure for graphs


Version published
Weekly downloads
34
decreased by-29.17%
Maintainers
1
Install size
956 kB
Created
Weekly downloads
 

Readme

Source

graphs

An intuitive data structure for graphs, implemented using ES6 data structures.

var Graph = require('graphs')
var graph = new Graph()
var a = {name: 'a'}
var b = {name: 'b'}
graph.add(a)
graph.add(b)
graph.link(a, b)
graph.traverse(function(from, to) {
  console.log(from.name, 'linked to', to.name)
})
// => a linked to b

Examples

Adding Nodes

var graph = new Graph()
var a = {name: 'a'}

graph.add(a)
graph.has(a) // => true
graph.size // => 1

var b = {name: 'b'}
graph.has(b) // => false
graph.size // => 2

graph.add(b)
graph.has(b) // => true

Linking Nodes

graph.link(a, b)

Linking will also add nodes to the graph.

.to and .from return ES6 Sets of connected nodes.

graph.link(a, b)

graph.from(a) // Set of nodes connected from a
graph.from(a).size // => 1
graph.from(a).has(b) // => true


graph.to(b) // Set of nodes connected to b
graph.to(b).has(a) // => true
graph.from(b).size // => 0

Unlinking Nodes

graph.unlink(a, b)
graph.from(a).size // => 0

Deleting Nodes

  • Also removes any links (but not linked nodes).
graph.delete(b)

Iterating over all Nodes

  • .forEach will even include entirely unlinked nodes.
graph.forEach(function(node) {
  console.log('node: %s', node.name)
})

Traversing the Graph

graph.traverse will traverse all links from the specified node.

  • Arguments to the callback are from, to
  • Starts at a node and follows links.
  • May visit a node multiple times (depending on how many times it's linked to).
  • The callback will always fire with valid from and to parameters.
  • If startNode is not linked to anything, callback will not fire.
  • Will not follow cycles.
graph.traverse(startNode, function(from, to) {
  console.log('from: %s', from)
  console.log('to: %s', to)
})

Visiting Linked Nodes

graph.visit will visit each node that can be reached from the specified node, once.

  • Arguments to the callback are to, from
  • Will follow links but will not visit any node more than once.
  • The from argument may not be set if visit didn't follow a link to the current node (e.g. on the first iteration).
graph.visit(startNode, function(node, linkedFrom) {
  console.log('node: %s', node)
  console.log('linkedFrom: %s', linkedFrom)
})

Before/After/Guard Hooks

Makes it easy to embed custom logic into your graph.

graph.before('add', function(a,b) {
  // execute before add
})

graph.after('add', function(a,b) {
  // execute after add
})

graph.guard('add', function(node) {
  // prevent add from running if return falsey
})

See these libraries for usage information:

  • timoxley/beforefn
  • timoxley/guardfn
  • timoxley/afterfn

License

MIT

Keywords

FAQs

Last updated on 20 Jun 2014

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc