Skip to content

Commit 04cdbf8

Browse files
authored
Merge pull request #21 from giraffesyo/master
Add weights to serialization
2 parents 2f1fbf4 + 220c8e0 commit 04cdbf8

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Serializes the graph. Returns an object with the following properties.
166166
* `links` An array of objects representing edges, each with the following properties.
167167
* `source` The node identifier string of the source node (**u**).
168168
* `target` The node identifier string of the target node (**v**).
169+
* `weight` The weight of the edge between the source and target nodes.
169170

170171
Here's example code for serializing a graph.
171172

@@ -186,8 +187,8 @@ The following will be the value of `serialized`.
186187
{ "id": "c" }
187188
],
188189
"links": [
189-
{ "source": "a", "target": "b" },
190-
{ "source": "b", "target": "c" }
190+
{ "source": "a", "target": "b", "weight": 1 },
191+
{ "source": "b", "target": "c", "weight": 1 }
191192
]
192193
}
193194
```

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ module.exports = function Graph(serialized){
311311
adjacent(source).forEach(function (target){
312312
serialized.links.push({
313313
source: source,
314-
target: target
314+
target: target,
315+
weight: getEdgeWeight(source, target)
315316
});
316317
});
317318
});
@@ -322,7 +323,7 @@ module.exports = function Graph(serialized){
322323
// Deserializes the given serialized graph.
323324
function deserialize(serialized){
324325
serialized.nodes.forEach(function (node){ addNode(node.id); });
325-
serialized.links.forEach(function (link){ addEdge(link.source, link.target); });
326+
serialized.links.forEach(function (link){ addEdge(link.source, link.target, link.weight); });
326327
return graph;
327328
}
328329

0 commit comments

Comments
 (0)