Passed
Push — master ( 8fd42c...888e42 )
by Kolja
01:04
created

jgfJsonDecorator.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
const _ = require('lodash');
2
const check = require('check-types');
3
const { JgfGraph } = require('./jgfGraph');
4
const { JgfMultiGraph } = require('./jgfMultiGraph');
5
6
/**
7
 * Transforms graphs or multigraphs to json or vice versa.
8
 *
9
 * Note that this is just called decorator for semantic reasons and does not follow the GoF decorator design pattern.
10
 */
11
class JgfJsonDecorator {
12
13
    // static fromJson(json) {
14
    //     // todo: return graph or multigraph depending on data
15
    // }
16
17
    static _guardAgainstInvalidGraphObject(graph) {
18
        if (!check.instance(graph, JgfGraph) && !check.instance(graph, JgfMultiGraph)) {
19
            throw new Error('JgfJsonDecorator can only decorate graphs or multigraphs.');
20
        }
21
    }
22
23
    static toJson(graph) {
24
        this._guardAgainstInvalidGraphObject(graph);
25
26
        let normalizedGraph = this._normalizeToMultiGraph(graph);
27
        let allGraphsJson = [];
28
29
        _.each(normalizedGraph.graphs, (singleGraph) => {
30
31
            let singleGraphJson = {
32
                type: singleGraph.type,
33
                label: singleGraph.label,
34
                directed: singleGraph.directed,
35
                metadata: singleGraph.metadata,
36
                nodes: [],
37
                edges: [],
38
            };
39
40
            this._nodesToJson(singleGraph, singleGraphJson);
41
            this._edgesToJson(singleGraph, singleGraphJson);
42
43
            allGraphsJson.push(singleGraphJson);
44
        });
45
46
        return allGraphsJson;
47
    }
48
49
    /**
50
     * @param {JgfGraph} graph
51
     * @param {object} json
52
     */
53
    static _edgesToJson(graph, json) {
54
        _.each(graph.edges, (edge) => {
55
            json.edges.push({
56
                source: edge.source,
57
                target: edge.target,
58
                relation: edge.relation,
59
                label: edge.label,
60
                metadata: edge.metadata,
61
                directed: edge.directed,
62
            });
63
        });
64
    }
65
66
    /**
67
     * @param {JgfGraph} graph
68
     * @param {object} json
69
     */
70
    static _nodesToJson(graph, json) {
71
        _.each(graph.nodes, (node) => {
72
            json.nodes.push({
73
                id: node.id,
74
                label: node.label,
75
                metadata: node.metadata,
76
            });
77
        });
78
    }
79
80
    static _normalizeToMultiGraph(graph) {
81
        let normalizedGraph = graph;
82
        if (check.instance(graph, JgfGraph)) {
83
            normalizedGraph = new JgfMultiGraph();
84
            normalizedGraph.addGraph(graph);
85
        }
86
87
        return normalizedGraph;
88
    }
89
}
90
91
module.exports = {
92
    JgfJsonDecorator,
93
};