UNPKG

3.81 kBJavaScriptView Raw
1var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
2
3module.exports = function serialize(graph, options) {
4 var conn, getInOutName, getName, i, inPort, input, len, name, namedComponents, outPort, output, process, ref, ref1, ref2, src, srcName, srcPort, srcProcess, tgt, tgtName, tgtPort, tgtProcess;
5 if (options == null) {
6 options = {};
7 }
8 if (typeof(graph) === 'string') {
9 input = JSON.parse(graph);
10 } else {
11 input = graph;
12 }
13 namedComponents = [];
14 output = "";
15 getName = function(name) {
16 if (input.processes[name].metadata != null) {
17 name = input.processes[name].metadata.label;
18 }
19 if (name.indexOf('/') > -1) {
20 name = name.split('/').pop();
21 }
22 return name;
23 };
24 getInOutName = function(name, data) {
25 if ((data.process != null) && (input.processes[data.process].metadata != null)) {
26 name = input.processes[data.process].metadata.label;
27 } else if (data.process != null) {
28 name = data.process;
29 }
30 if (name.indexOf('/') > -1) {
31 name = name.split('/').pop();
32 }
33 return name;
34 };
35 if (input.properties) {
36 if (input.properties.environment && input.properties.environment.type) {
37 output += "# @runtime " + input.properties.environment.type + "\n";
38 }
39 Object.keys(input.properties).forEach(function (prop) {
40 if (!prop.match(/^[a-zA-Z0-9\-_]+$/)) {
41 return;
42 }
43 var propval = input.properties[prop];
44 if (typeof propval !== 'string') {
45 return;
46 }
47 if (!propval.match(/^[a-zA-Z0-9\-_\s\.]+$/)) {
48 return;
49 }
50 output += "# @" + prop + " " + propval + '\n';
51 });
52 }
53 ref = input.inports;
54 for (name in ref) {
55 inPort = ref[name];
56 process = getInOutName(name, inPort);
57 name = input.caseSensitive ? name : name.toUpperCase();
58 inPort.port = input.caseSensitive ? inPort.port : inPort.port.toUpperCase();
59 output += "INPORT=" + process + "." + inPort.port + ":" + name + "\n";
60 }
61 ref1 = input.outports;
62 for (name in ref1) {
63 outPort = ref1[name];
64 process = getInOutName(name, outPort);
65 name = input.caseSensitive ? name : name.toUpperCase();
66 outPort.port = input.caseSensitive ? outPort.port : outPort.port.toUpperCase();
67 output += "OUTPORT=" + process + "." + outPort.port + ":" + name + "\n";
68 }
69 output += "\n";
70 ref2 = input.connections;
71 for (i = 0, len = ref2.length; i < len; i++) {
72 conn = ref2[i];
73 if (conn.data != null) {
74 tgtPort = input.caseSensitive ? conn.tgt.port : conn.tgt.port.toUpperCase();
75 tgtName = conn.tgt.process;
76 tgtProcess = input.processes[tgtName].component;
77 tgt = getName(tgtName);
78 if (indexOf.call(namedComponents, tgtProcess) < 0) {
79 tgt += "(" + tgtProcess + ")";
80 namedComponents.push(tgtProcess);
81 }
82 output += '"' + conn.data + '"' + (" -> " + tgtPort + " " + tgt + "\n");
83 } else {
84 srcPort = input.caseSensitive ? conn.src.port : conn.src.port.toUpperCase();
85 srcName = conn.src.process;
86 srcProcess = input.processes[srcName].component;
87 src = getName(srcName);
88 if (indexOf.call(namedComponents, srcProcess) < 0) {
89 src += "(" + srcProcess + ")";
90 namedComponents.push(srcProcess);
91 }
92 tgtPort = input.caseSensitive ? conn.tgt.port : conn.tgt.port.toUpperCase();
93 tgtName = conn.tgt.process;
94 tgtProcess = input.processes[tgtName].component;
95 tgt = getName(tgtName);
96 if (indexOf.call(namedComponents, tgtProcess) < 0) {
97 tgt += "(" + tgtProcess + ")";
98 namedComponents.push(tgtProcess);
99 }
100 output += src + " " + srcPort + " -> " + tgtPort + " " + tgt + "\n";
101 }
102 }
103 return output;
104}