graph-dynamic 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/json.js ADDED
@@ -0,0 +1,80 @@
1
+ var Graph = require("./graph");
2
+
3
+ module.exports = {
4
+ write: write,
5
+ read: read
6
+ };
7
+
8
+ /**
9
+ * Creates a JSON representation of the graph that can be serialized to a string with
10
+ * JSON.stringify. The graph can later be restored using json.read.
11
+ */
12
+ function write(g) {
13
+ var json = {
14
+ options: {
15
+ directed: g.isDirected(),
16
+ multigraph: g.isMultigraph(),
17
+ compound: g.isCompound()
18
+ },
19
+ nodes: writeNodes(g),
20
+ edges: writeEdges(g)
21
+ };
22
+
23
+ if (g.graph() !== undefined) {
24
+ json.value = structuredClone(g.graph());
25
+ }
26
+ return json;
27
+ }
28
+
29
+ function writeNodes(g) {
30
+ return g.nodes().map(function(v) {
31
+ var nodeValue = g.node(v);
32
+ var parent = g.parent(v);
33
+ var node = { v: v };
34
+ if (nodeValue !== undefined) {
35
+ node.value = nodeValue;
36
+ }
37
+ if (parent !== undefined) {
38
+ node.parent = parent;
39
+ }
40
+ return node;
41
+ });
42
+ }
43
+
44
+ function writeEdges(g) {
45
+ return g.edges().map(function(e) {
46
+ var edgeValue = g.edge(e);
47
+ var edge = { v: e.v, w: e.w };
48
+ if (e.name !== undefined) {
49
+ edge.name = e.name;
50
+ }
51
+ if (edgeValue !== undefined) {
52
+ edge.value = edgeValue;
53
+ }
54
+ return edge;
55
+ });
56
+ }
57
+
58
+ /**
59
+ * Takes JSON as input and returns the graph representation.
60
+ *
61
+ * @example
62
+ * var g2 = graphlib.json.read(JSON.parse(str));
63
+ * g2.nodes();
64
+ * // ['a', 'b']
65
+ * g2.edges()
66
+ * // [ { v: 'a', w: 'b' } ]
67
+ */
68
+ function read(json) {
69
+ var g = new Graph(json.options).setGraph(json.value);
70
+ json.nodes.forEach(function(entry) {
71
+ g.setNode(entry.v, entry.value);
72
+ if (entry.parent) {
73
+ g.setParent(entry.v, entry.parent);
74
+ }
75
+ });
76
+ json.edges.forEach(function(entry) {
77
+ g.setEdge({ v: entry.v, w: entry.w, name: entry.name }, entry.value);
78
+ });
79
+ return g;
80
+ }
package/lib/version.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = '2.2.5-pre';
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "graph-dynamic",
3
+ "version": "1.0.0",
4
+ "description": "A directed and undirected multi-graph library",
5
+ "author": "Graph Node Organization",
6
+ "license": "MIT",
7
+ "main": "index.js",
8
+ "scripts": {
9
+ "lint": "make lint",
10
+ "test": "make test"
11
+ },
12
+ "files": [
13
+ "index.js",
14
+ "index.d.ts",
15
+ "dist/",
16
+ "lib/"
17
+ ],
18
+ "engines": {
19
+ "node": ">=20.0.0"
20
+ },
21
+ "types": "index.d.ts",
22
+ "keywords": [
23
+ "graph",
24
+ "algorithms"
25
+ ],
26
+ "devDependencies": {
27
+ "benchmark": "2.1.4",
28
+ "browserify": "16.5.1",
29
+ "chai": "^4.3.6",
30
+ "eslint": "8.35.0",
31
+ "jshint": "2.13.5",
32
+ "jshint-stylish": "2.2.1",
33
+ "karma": "6.4.1",
34
+ "karma-chrome-launcher": "3.1.0",
35
+ "karma-mocha": "2.0.1",
36
+ "karma-requirejs": "1.1.0",
37
+ "karma-safari-launcher": "1.0.0",
38
+ "mocha": "10.1.0",
39
+ "nyc": "^15.1.0",
40
+ "requirejs": "2.3.7",
41
+ "seedrandom": "3.0.5",
42
+ "semver": "7.5.4",
43
+ "sprintf": "0.1.5",
44
+ "uglify-js": "3.17.4"
45
+ },
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/Graph-Node-Org/graph-dynamic"
49
+ }
50
+ }