JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 408
  • Score
    100M100P100Q87772F
  • License Apache-2.0

A library for modeling and traversing graphs

Package Exports

  • cleargraph

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cleargraph) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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 ND and ED represent the node value and edge value respectively.

When instantiating the graph, specify the values of ND and ED. In addition, in order to allow graph serialization, ND and ED must implement toString() and can implement fromString().

Here is an example of ND (Node Data) and ED (Edge Data) classes:

class NodeData implements Serializable {
    name: string;
    radius: number;
    constructor(name:string, radius:number){
        this.name = name;
        this.radius = radius;
    }
    toString(){
        return JSON.stringify({name: this.name, radius: this.radius});
    }
    fromString(json:string){
        const obj = JSON.parse(json);
        return new NodeData(obj.name, obj.radius);
    }
}

class EdgeData implements Serializable {
    relationType: string;
    proximity: number;
    constructor(relationType: string, proximity: number){
        this.relationType = relationType;
        this.proximity = proximity;
    }
    toString(){
        return JSON.stringify({relationType: this.relationType, proximity: this.proximity});
    }
    fromString(json:string){
        const obj = JSON.parse(json);
        return new NodeData(obj.relationType, obj.proximity);
    }
}

Now we will use these classes to implement a graph:

let g = new Graph<NodeData, EdgeData>();

new Node('a', new NodeData('comp1', '1.0.0'))

g.setNode(new Node('earth', new NodeData('earth', 6371)));
g.setNode(new Node('moon', new NodeData('moon', 1737)));
g.setNode(new Node('sun', new NodeData('sun', 696340)));
g.setEdge(new Edge('moon','earth', new EdgeData('orbits', 384400)));
g.setEdge(new Edge('earth','sun', new EdgeData('orbits', 147240000)));

Contributing

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

License

Apache license, version 2.0