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

cleargraph

v7.0.0

Published

A library for modeling and traversing graphs

Downloads

6,183

Readme

Cleargraph

Cleargraph is a graph library offering:

  • An abstraction over graphs that supports generic data types
  • Traversal over successors and predecessors
  • Use of user-defined filters on nodes and edges information and traversal
  • Strictly-typed implementation

Installation

npm install cleargraph
yarn add cleargraph

Getting started

The nodes and edges in the graph are represented by key-value pairs where the keys are strings, and the generics N and E represent the node value and edge value respectively.

When instantiating the graph, specify the values of N and E. In addition, in order to allow graph serialization, N and E should implement stringify(). If it's not implemented, the graph will call its own default serialization method that might not work for complex objects.

Here is an example of N (Node Data) and E (Edge Data) classes:

class Orb { // a node in the graph
    name: string;
    radius: number;
    constructor(name:string, radius:number){
        this.name = name;
        this.radius = radius;
    }
    stringify(){ // Add a specific stringify() implementation if your class will not stringify correctly with just JSON.stringiy when serializing the graph
        return JSON.stringify({name: this.name, radius: this.radius});
    }
}

class OrbRelation{ // an edge in the graph
    relationType: string;
    proximity: number;
    constructor(relationType: string, proximity: number){
        this.relationType = relationType;
        this.proximity = proximity;
    }
    stringify(){
        return JSON.stringify({relationType: this.relationType, proximity: this.proximity});
    }
}

Using these classes to implement a graph:


let g = new Graph<Orb, OrbRelation>();

g.setNode('earth', new Orb('earth', 6371));
g.setNode('moon', new Orb('moon', 1737));
g.setNode('sun', new Orb('sun', 696340));
g.setEdge('moon','earth', new OrbRelation('orbits', 384400));
g.setEdge('earth','sun', new OrbRelation('orbits', 147240000));

Some uses of the graph:

g.node('moon');
// Orb{name: 'moon', radius: 1737}
g.edge('earth', 'sun');
//OrbRelation{relationType: 'orbits', proximity: 147240000}
g.succssors('moon'); // returns the immediate nodes the given node point to
// Map 
// {"earth" => Orb} {key: "earth", value: Orb}
//     key:"earth"
//     value:Orb {name: "earth", radius: 6371}
g.successorsArray('moon'); // returns an array of all the nodes the given node points to *recursively*
// Array(2) [Orb, Orb]
// 0:Orb {name: "earth",radius: 6371}
// 1:Orb {name: "sun", radius: 696340}
g.toposort(); // performs a topological sort on the graph
// Array(3) [Orb, Orb, Orb]
// 0:Orb {name: "moon",radius: 1737}
// 1:Orb {name: "earth",radius: 6371}
// 2:Orb {name: "sun", radius: 696340}

Contributing

Contributions are always welcome, no matter how large or small.

License

Apache license, version 2.0