Permalink
Browse files

Minor adaptations for pull request #325

Here are the minor adaptations to merge the pull request:
 - Moved methods `adjacentNodes` and `adjacentEdges` from sigma.classes.graph.js into sigma.plugins.filter.js
 - Moved the related unit tests into unit.plugins.filter.js
 - Added sigma.classes.graph.hasMethod to check wether a methodName already exists or not (and its unit tests)
 - Used hasMethod in sigma.plugins.filter.js to check if `adjacentNodes` and `adjacentEdges` already exist or not
  • Loading branch information...
1 parent 0198d2a commit 3415e034b4016b8b72b6f906b90e80c65abf3221 @jacomyal committed Jul 30, 2014
@@ -6,7 +6,50 @@
// Initialize package:
sigma.utils.pkg('sigma.plugins');
-
+
+ // Add custom graph methods:
+ /**
+ * This methods returns an array of nodes that are adjacent to a node.
+ *
+ * @param {string} id The node id.
+ * @return {array} The array of adjacent nodes.
+ */
+ if (!sigma.classes.graph.hasMethod('adjacentNodes'))
+ sigma.classes.graph.addMethod('adjacentNodes', function(id) {
+ if (typeof id !== 'string')
+ throw 'adjacentNodes: the node id must be a string.';
+
+ var target,
+ nodes = [];
+ for(target in this.allNeighborsIndex[id]) {
+ nodes.push(this.nodesIndex[target]);
+ }
+ return nodes;
+ });
+
+ /**
+ * This methods returns an array of edges that are adjacent to a node.
+ *
+ * @param {string} id The node id.
+ * @return {array} The array of adjacent edges.
+ */
+ if (!sigma.classes.graph.hasMethod('adjacentEdges'))
+ sigma.classes.graph.addMethod('adjacentEdges', function(id) {
+ if (typeof id !== 'string')
+ throw 'adjacentEdges: the node id must be a string.';
+
+ var a = this.allNeighborsIndex[id],
+ eid,
+ target,
+ edges = [];
+ for(target in a) {
+ for(eid in a[target]) {
+ edges.push(a[target][eid]);
+ }
+ }
+ return edges;
+ });
+
/**
* Sigma Filter
* =============================
@@ -40,7 +83,7 @@
// hide node, or keep former value
while(ln--)
n[ln].hidden = !fn.call(_g, n[ln]) || n[ln].hidden;
-
+
while(le--)
if (_g.nodes(e[le].source).hidden || _g.nodes(e[le].target).hidden)
e[le].hidden = true;
@@ -71,14 +114,14 @@
neighbors = _g.adjacentNodes(id),
nn = neighbors.length,
no = {};
-
+
while(nn--)
no[neighbors[nn].id] = true;
while(ln--)
if (n[ln].id !== id && !(n[ln].id in no))
n[ln].hidden = true;
-
+
while(le--)
if (_g.nodes(e[le].source).hidden || _g.nodes(e[le].target).hidden)
e[le].hidden = true;
@@ -98,10 +141,10 @@
if (key != undefined && !key.length)
throw 'The filter key must be a non-empty string.';
-
+
if (typeof fn !== 'function')
throw 'The predicate of key "'+ key +'" must be a function.';
-
+
if ('undo' === key)
throw '"undo" is a reserved key.';
@@ -155,7 +198,7 @@
* function hides the nodes that fail a truth test (predicate). The method
* adds the anonymous function to the chain of filters. The filter is not
* executed until the apply() method is called.
- *
+ *
* > var filter = new sigma.plugins.filter(s);
* > filter.nodesBy(function(n) {
* > return this.degree(n.id) > 0;
@@ -168,7 +211,7 @@
Filter.prototype.nodesBy = function(fn, key) {
// Wrap the predicate to be applied on the graph and add it to the chain.
register(Processors.nodes, fn, key);
-
+
return this;
};
@@ -287,7 +330,7 @@
if (Object.prototype.toString.call(v) === '[object Array]')
for (var i = 0, len = v.length; i < len; i++)
q[v[i]] = true;
-
+
else // 1 filter key
q[v] = true;
@@ -333,7 +376,7 @@
eval(" copy[i] = " + o[i].toString());
//copy[i] = o[i].bind(_g);
}
-
+
else
copy[i] = o[i];
}
@@ -413,7 +456,7 @@
if (typeof copy[i].predicate !== 'function')
throw 'The predicate of key "'+ copy[i].key +'" must be a function.';
-
+
if (typeof copy[i].processor !== 'string')
throw 'The processor of key "'+ copy[i].key +'" must be a string.';
@@ -173,7 +173,7 @@
)
throw 'addMethod: Wrong arguments.';
- if (_methods[methodName])
+ if (_methods[methodName] || graph[methodName])
throw 'The method "' + methodName + '" already exists.';
_methods[methodName] = fn;
@@ -183,6 +183,23 @@
};
/**
+ * This global method returns true if the method has already been added, and
+ * false else.
+ *
+ * Here are some examples:
+ *
+ * > graph.hasMethod('addNode'); // returns true
+ * > graph.hasMethod('hasMethod'); // returns true
+ * > graph.hasMethod('unexistingMethod'); // returns false
+ *
+ * @param {string} methodName The name of the method.
+ * @return {boolean} The result.
+ */
+ graph.hasMethod = function(methodName) {
+ return !!(_methods[methodName] || graph[methodName]);
+ };
+
+ /**
* This global methods attaches a function to a method. Anytime the specified
* method is called, the attached function is called right after, with the
* same arguments and in the same scope.
@@ -771,47 +788,6 @@
});
- /**
- * This methods returns an array of nodes that are adjacent to a node.
- *
- * @param {string} id The node id.
- * @return {array} The array of adjacent nodes.
- */
- graph.addMethod('adjacentNodes', function(id) {
- if (typeof id !== 'string')
- throw 'adjacentNodes: the node id must be a string.';
-
- var target,
- nodes = [];
- for(target in this.allNeighborsIndex[id]) {
- nodes.push(this.nodesIndex[target]);
- }
- return nodes;
- });
-
- /**
- * This methods returns an array of edges that are adjacent to a node.
- *
- * @param {string} id The node id.
- * @return {array} The array of adjacent edges.
- */
- graph.addMethod('adjacentEdges', function(id) {
- if (typeof id !== 'string')
- throw 'adjacentEdges: the node id must be a string.';
-
- var a = this.allNeighborsIndex[id],
- eid,
- target,
- edges = [];
- for(target in a) {
- for(eid in a[target]) {
- edges.push(a[target][eid]);
- }
- }
- return edges;
- });
-
-
/**
View
@@ -129,12 +129,6 @@ test('Basic manipulation', function() {
'"nodes" with a strings array as arguments returns the array of specified nodes.'
);
- deepEqual(
- myGraph.adjacentNodes('n0'),
- [graph.nodes[1]],
- '"adjacentNodes" returns the adjacent nodes of a specified node'
- );
-
throws(
function() {
myGraph.nodes(['n0', 'n1', 123]);
@@ -244,12 +238,6 @@ test('Basic manipulation', function() {
'"edges" with a strings array as arguments returns the array of specified edge.'
);
- deepEqual(
- myGraph.adjacentEdges('n0'),
- [graph.edges[0]],
- '"adjacentEdges" returns the adjacent edges of a specified node'
- );
-
throws(
function() {
myGraph.edges(['e0', 123]);
@@ -334,10 +322,28 @@ test('Methods and attached functions', function() {
counter++;
});
+ strictEqual(
+ false,
+ sigma.classes.graph.hasMethod('getNodeLabel'),
+ 'sigma.classes.hasMethod returns false if the method does not exist.'
+ );
+
sigma.classes.graph.addMethod('getNodeLabel', function(nId) {
return (this.nodesIndex[nId] || {}).label;
});
+ strictEqual(
+ true,
+ sigma.classes.graph.hasMethod('getNodeLabel'),
+ 'sigma.classes.hasMethod returns true if the method has been added with addMethod.'
+ );
+
+ strictEqual(
+ true,
+ sigma.classes.graph.hasMethod('hasMethod'),
+ 'sigma.classes.hasMethod returns true if the method is implemented in the core.'
+ );
+
myGraph = new sigma.classes.graph();
myGraph.addNode({ id: 'n0', label: 'My node' });
strictEqual(
Oops, something went wrong.

0 comments on commit 3415e03

Please sign in to comment.