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

v1.0.1

Published

Gremlin-like generalized graph traversal API

Downloads

13

Readme

graph-traversal

Gremlin-like generalized graph traversal API

Usage

This package comes in iterable and async iterable flavors. The exports are named the same in each.

import { VertexTraversal, EdgeTraversal } from "graph-traversal"
import { VertexTraversal, EdgeTraversal } from "graph-traversal/async"

Currently, the only way to begin a traversal is to call VertexTraversal.from. Any graph with a match method will be accepted. (Implementers can always make a proxy object that implements such.)

Almost all traversal methods yield a new traversal object that holds immutable state.

For the below method descriptions:

  • in means traveling towards the current result.
  • out means traveling away from the current result.
  • both means getting results from both in and out directions.
  • For the methods with optional parameters: undefined can be a valid vertex, so passing undefined is not the same as passing no arguments at all. Passing undefined will match undefined vertices or edges with the graph.

Common traversal methods

traversal.as(key)

Assigns a key name to the current object being traversed. For a vertex traversal, the object will be a vertex. For an edge traversal, the object will be an edge.

The key can be a string, a number, or a symbol.

traversal.collect()

Collects all of the named objects as a single object. For example, .as("foo").out("child").as("bar") will emit objects with the signature { foo: ?, bar: ? }.

traversal.filter(filterFn)

Filters the currently matched vertices/edges.

traversal.filterTriple(filterFn)

Filters the previously matched triple. For example, .out("x").filterTriple(fn) will consider all the triples that led to x. Note: At the beginning of the traversal, there will be no triples.

traversalSymbol.iterator or traversalSymbol.asyncIterator

Traverses through the currently matched vertices or edges.

Vertex traversal methods

VertexTraversal.from(graph, vertex)

Creates a new vertex traversal starting from the specified vertex.

graph should be an object that implements a match method that takes pattern. pattern is an object that has any of the keys subject, predicate, or object. match should return either an IterableIterator or an AsyncIterableIterator, depending on the flavor of traversal, that iterates through all of the triples matched by the pattern.

vt.out(), vt.in(), vt.both()

out, in, and both can take the following arguments:

  • () - go through all the adjacent vertices in the specified direction
  • (predicate: E) - filter by predicate
  • (predicate: E, vertex: V) - filter by predicate and adjacent vertex

Return value: vertex traversal

vt.outE(), vt.inE(), vt.bothE()

outE, inE, and bothE can take the following arguments:

  • () - go through all the adjacent edges
  • (predicate: E) - filter by predicate

Return value: edge traversal

Edge traversal methods

et.outV(), et.inV(), et.bothV()

outV, inV, and bothV depend on the last set of triples matched. There will be no previous triples at the start of the traversal

They can take the following arguments:

  • () - go through all the adjacent vertices in the specified direction
  • (vertex: V) - filter by vertex

Return value: vertex traversal