Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dependency-graph

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dependency-graph - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

6

CHANGELOG.md

@@ -5,2 +5,8 @@ # Dependency Graph Changelog

- No changes yet
## 0.3.0 (July 24, 2015)
- Fix issue where if you call `addNode` twice with the same name, it would clear all edges for that node. Now it will do nothing if a node with the specified name already exists. (Fixes #3)
## 0.2.1 (July 3, 2015)

@@ -7,0 +13,0 @@

85

lib/dep_graph.js

@@ -44,23 +44,42 @@ /**

DepGraph.prototype = {
/**
* Add a node to the dependency graph. If a node with the specified
* name already exists, this method will do nothing.
*/
addNode:function (name) {
this.nodes[name] = name;
this.outgoingEdges[name] = [];
this.incomingEdges[name] = [];
if (!this.hasNode(name)) {
this.nodes[name] = name;
this.outgoingEdges[name] = [];
this.incomingEdges[name] = [];
}
},
/**
* Remove a node from the dependency graph. If a node with the specified
* name does not exist, this method will do nothing.
*/
removeNode:function (name) {
delete this.nodes[name];
delete this.outgoingEdges[name];
delete this.incomingEdges[name];
[this.incomingEdges, this.outgoingEdges].forEach(function (edgeList) {
Object.keys(edgeList).forEach(function (key) {
var idx = edgeList[key].indexOf(name);
if (idx >= 0) {
edgeList[key].splice(idx, 1);
}
}, this);
});
if (this.hasNode(name)) {
delete this.nodes[name];
delete this.outgoingEdges[name];
delete this.incomingEdges[name];
[this.incomingEdges, this.outgoingEdges].forEach(function (edgeList) {
Object.keys(edgeList).forEach(function (key) {
var idx = edgeList[key].indexOf(name);
if (idx >= 0) {
edgeList[key].splice(idx, 1);
}
}, this);
});
}
},
/**
* Check if a node exists in the graph
*/
hasNode:function (name) {
return !!this.nodes[name];
},
/**
* Add a dependency between two nodes. If either of the nodes does not exist,
* an Error will be thrown.
*/
addDependency:function (from, to) {

@@ -79,14 +98,28 @@ if (this.hasNode(from) && this.hasNode(to)) {

},
/**
* Remove a dependency between two nodes.
*/
removeDependency:function (from, to) {
var idx = this.outgoingEdges[from].indexOf(to);
if (idx >= 0) {
this.outgoingEdges[from].splice(idx, 1);
var idx;
if (this.hasNode(from)) {
idx = this.outgoingEdges[from].indexOf(to);
if (idx >= 0) {
this.outgoingEdges[from].splice(idx, 1);
}
}
idx = this.incomingEdges[to].indexOf(from);
if (idx >= 0) {
this.incomingEdges[to].splice(idx, 1);
if (this.hasNode(to)) {
idx = this.incomingEdges[to].indexOf(from);
if (idx >= 0) {
this.incomingEdges[to].splice(idx, 1);
}
}
},
/**
* Get an array containing the nodes that the specified node depends on (transitively).
* If `leavesOnly` is true, only nodes that do not depend on any other nodes will be returned
* in the array.
*/
dependenciesOf:function (name, leavesOnly) {
if (this.nodes[name]) {
if (this.hasNode(name)) {
var result = [];

@@ -105,4 +138,8 @@ var DFS = createDFS(this.outgoingEdges, leavesOnly, result);

},
/**
* get an array containing the nodes that depend on the specified node (transitively).
* If `leavesOnly` is true, only nodes that do not have any dependants will be returned in the array.
*/
dependantsOf:function (name, leavesOnly) {
if (this.nodes[name]) {
if (this.hasNode(name)) {
var result = [];

@@ -120,2 +157,6 @@ var DFS = createDFS(this.incomingEdges, leavesOnly, result);

},
/**
* Construct the overall processing order for the dependency graph.
* If `leavesOnly` is true, only nodes that do not depend on any other nodes will be returned.
*/
overallOrder:function (leavesOnly) {

@@ -122,0 +163,0 @@ var self = this;

{
"name": "dependency-graph",
"description": "Simple dependency graph.",
"version": "0.2.1",
"version": "0.3.0",
"author": "Jim Riecken <jriecken@gmail.com>",

@@ -6,0 +6,0 @@ "keywords": [

@@ -20,2 +20,28 @@ var DepGraph = require('../lib/dep_graph').DepGraph;

it('should do nothing if creating a node that already exists', function () {
var graph = new DepGraph();
graph.addNode('a');
graph.addNode('b');
graph.addDependency('a','b');
graph.addNode('a');
expect(graph.dependenciesOf('a')).toEqual(['b']);
});
it('should do nothing if removing a node that does not exist', function () {
var graph = new DepGraph();
graph.addNode('a');
expect(graph.hasNode('a')).toBe(true);
graph.removeNode('a');
expect(graph.hasNode('Foo')).toBe(false);
graph.removeNode('a');
expect(graph.hasNode('Foo')).toBe(false);
});
it('should be able to add dependencies between nodes', function () {

@@ -22,0 +48,0 @@ var graph = new DepGraph();

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc