Skip to content

Commit a15bcde

Browse files
committedJan 19, 2014
Fixed jacomyal#118: trully empty objects for data
From now, in sigma.classes.graph, every data related objects are created with Object.create(null) instead of {}, to allow every strings as valid IDs, including "prototype", "constructor", "watch", etc...
1 parent 1036003 commit a15bcde

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed
 

‎src/classes/sigma.classes.graph.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
;(function(undefined) {
22
'use strict';
33

4-
var _methods = {},
5-
_indexes = {},
6-
_initBindings = {},
7-
_methodBindings = {},
4+
var _methods = Object.create(null),
5+
_indexes = Object.create(null),
6+
_initBindings = Object.create(null),
7+
_methodBindings = Object.create(null),
88
_defaultSettings = {
99
immutable: true,
1010
clone: true
@@ -62,22 +62,22 @@
6262
* ***************
6363
* These indexes just index data by ids.
6464
*/
65-
nodesIndex: {},
66-
edgesIndex: {},
65+
nodesIndex: Object.create(null),
66+
edgesIndex: Object.create(null),
6767

6868
/**
6969
* LOCAL INDEXES:
7070
* **************
7171
* These indexes refer from node to nodes. Each key is an id, and each
7272
* value is the array of the ids of related nodes.
7373
*/
74-
inNeighborsIndex: {},
75-
outNeighborsIndex: {},
76-
allNeighborsIndex: {},
74+
inNeighborsIndex: Object.create(null),
75+
outNeighborsIndex: Object.create(null),
76+
allNeighborsIndex: Object.create(null),
7777

78-
inNeighborsCount: {},
79-
outNeighborsCount: {},
80-
allNeighborsCount: {}
78+
inNeighborsCount: Object.create(null),
79+
outNeighborsCount: Object.create(null),
80+
allNeighborsCount: Object.create(null)
8181
};
8282

8383
// Execute bindings:
@@ -135,7 +135,7 @@
135135
var k;
136136

137137
for (k in obj)
138-
if (obj.hasOwnProperty(k))
138+
if (!('hasOwnProperty' in obj) || obj.hasOwnProperty(k))
139139
delete obj[k];
140140

141141
return obj;
@@ -177,7 +177,7 @@
177177
throw 'The method "' + methodName + '" already exists.';
178178

179179
_methods[methodName] = fn;
180-
_methodBindings[methodName] = {};
180+
_methodBindings[methodName] = Object.create(null);
181181

182182
return this;
183183
};
@@ -328,7 +328,7 @@
328328

329329
var k,
330330
id = node.id,
331-
validNode = {};
331+
validNode = Object.create(null);
332332

333333
// Check the "clone" option:
334334
if (this.settings('clone')) {
@@ -348,9 +348,9 @@
348348
validNode.id = id;
349349

350350
// Add empty containers for edges indexes:
351-
this.inNeighborsIndex[id] = {};
352-
this.outNeighborsIndex[id] = {};
353-
this.allNeighborsIndex[id] = {};
351+
this.inNeighborsIndex[id] = Object.create(null);
352+
this.outNeighborsIndex[id] = Object.create(null);
353+
this.allNeighborsIndex[id] = Object.create(null);
354354

355355
this.inNeighborsCount[id] = 0;
356356
this.outNeighborsCount[id] = 0;
@@ -396,7 +396,7 @@
396396
throw 'The edge "' + edge.id + '" already exists.';
397397

398398
var k,
399-
validEdge = {};
399+
validEdge = Object.create(null);
400400

401401
// Check the "clone" option:
402402
if (this.settings('clone')) {
@@ -433,19 +433,19 @@
433433
this.edgesIndex[validEdge.id] = validEdge;
434434

435435
if (!this.inNeighborsIndex[edge.target][edge.source])
436-
this.inNeighborsIndex[edge.target][edge.source] = {};
436+
this.inNeighborsIndex[edge.target][edge.source] = Object.create(null);
437437
this.inNeighborsIndex[edge.target][edge.source][edge.id] = edge;
438438

439439
if (!this.outNeighborsIndex[edge.source][edge.target])
440-
this.outNeighborsIndex[edge.source][edge.target] = {};
440+
this.outNeighborsIndex[edge.source][edge.target] = Object.create(null);
441441
this.outNeighborsIndex[edge.source][edge.target][edge.id] = edge;
442442

443443
if (!this.allNeighborsIndex[edge.source][edge.target])
444-
this.allNeighborsIndex[edge.source][edge.target] = {};
444+
this.allNeighborsIndex[edge.source][edge.target] = Object.create(null);
445445
this.allNeighborsIndex[edge.source][edge.target][edge.id] = edge;
446446

447447
if (!this.allNeighborsIndex[edge.target][edge.source])
448-
this.allNeighborsIndex[edge.target][edge.source] = {};
448+
this.allNeighborsIndex[edge.target][edge.source] = Object.create(null);
449449
this.allNeighborsIndex[edge.target][edge.source][edge.id] = edge;
450450

451451
// Keep counts up to date:
@@ -778,7 +778,7 @@
778778
* *******
779779
*/
780780
if (typeof sigma !== 'undefined') {
781-
sigma.classes = sigma.classes || {};
781+
sigma.classes = sigma.classes || Object.create(null);
782782
sigma.classes.graph = graph;
783783
} else if (typeof exports !== 'undefined') {
784784
if (typeof module !== 'undefined' && module.exports)

‎test/unit.graph.js

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ test('Basic manipulation', function() {
145145
'Adding an already existing node throws an error.'
146146
);
147147

148+
myGraph.addNode({ id: 'prototype' }).addNode({ id: 'constructor' });
149+
ok(
150+
myGraph.nodes('prototype') && myGraph.nodes('constructor'),
151+
'"constructor" and "prototype" are valid node IDs.'
152+
);
153+
myGraph.dropNode('prototype').dropNode('constructor');
148154

149155

150156

0 commit comments

Comments
 (0)
Please sign in to comment.