Permalink
Browse files

added syntax for treating set() like dir()

  • Loading branch information...
tantalor committed Dec 23, 2013
1 parent 6ccea0a commit b398b9a6830baf8a328393e93f79027dec4df2a8
Showing with 41 additions and 0 deletions.
  1. +8 −0 README.md
  2. +5 −0 lib/graph.js
  3. +28 −0 test/core.js
View
@@ -33,6 +33,10 @@ Your graphs may have self edges, weighted edges, and directed edges, but not mul
b: {c: 3},
}); # path with vertices a, b, c and weights (a, b) = 2, (b, c) = 3
+#### With directed edges
+
+ new Graph({a: ['-b', '-c']}); # Directed edges to b and c.
+
### Degree, size, order, and adjacency
var g = new Graph({
@@ -58,6 +62,10 @@ Your graphs may have self edges, weighted edges, and directed edges, but not mul
g.dir('a', 'b'); # a ~ b, but b !~ a
g.has('a', 'b'); # true
g.has('b', 'a'); # false
+
+#### Alternative syntax
+
+ g.set('a', '-b'); # Same as g.dir('a', 'b');
### Deleting directed edges
View
@@ -102,6 +102,11 @@
Graph.prototype.set = function (u, v, edge)
{
+ // set('a', '-b', ...) is a synonym for dir('a', 'b', ...)
+ if (v[0] == '-') {
+ return this.dir(u, v.substr(1), edge);
+ }
+
// take an undefined edge as simply 'true' for convenience
edge = (edge === undefined ? true : edge);
View
@@ -183,6 +183,34 @@ this.core_suite =
test.done();
},
+ 'Constructor with directed edges': function (test)
+ {
+ var g = new Graph({'a': ['-b', '-c']});
+ test.ok(g.order() === 3,
+ "Number of vertices is 3.");
+ test.ok(g.size() === 2,
+ "Number of edges is 2.");
+ test.ok(g.degree('a') === 2,
+ "Out degree of 'a' is 1.");
+ test.ok(g.degree('b') === 0,
+ "Out degree of 'b' is 0.");
+ test.ok(g.degree('c') === 0,
+ "Out degree of 'c' is 0.");
+ test.ok(g.indegree('a') === 0,
+ "In degree of 'a' is 0.");
+ test.ok(g.indegree('b') === 1,
+ "In degree of 'b' is 1.");
+ test.ok(g.indegree('c') === 1,
+ "In degree of 'c' is 1.");
+ test.ok(g.has('a', 'b'));
+ test.ok(g.has('a', 'c'));
+ test.ok(!g.has('b', 'a'));
+ test.ok(!g.has('c', 'a'));
+ test.ok(!g.has('c', 'b'));
+ test.ok(!g.has('b', 'c'));
+ test.done();
+ },
+
'Multiget': function (test)
{
var g = new Graph();

0 comments on commit b398b9a

Please sign in to comment.