-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.js
167 lines (148 loc) · 4.76 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";
var R = require('require-parts')('ramda', 'src', ["partial", "map", "sort", "keys", "mapObjIndexed", "concat", "values", "assocPath", "reduce", "slice", "path", "defaultTo", "join", "flatten", "pipe", "flip"]);
function structEscape(s) {
return s.replace(/[^a-zA-Z0-9]/g, '_');
}
function stringEscape(s) {
return s.replace(/[^a-zA-Z0-9]/g, function(match) {
return "\\" + match;
});
}
function writeSubGraphField(tablename, fieldname) {
return "<" + structEscape(tablename) + "__" + structEscape(fieldname) + ">" + stringEscape(fieldname);
}
function writeTable(tabledata, tablename) {
var lines = ["subgraph cluster" + structEscape(tablename) + " {"];
var fields = R.join(
"|",
R.map(
R.partial(writeSubGraphField, tablename),
R.sort(
function(a, b) {
if (a == 'id') { return -1; }
if (b == 'id') { return 1; }
return 0;
},
R.keys(tabledata)
)
)
);
lines.push(' label = "' + stringEscape(tablename) + '";');
lines.push(' struct' + structEscape(tablename) + ' [label="{' + fields + '}",shape=record];');
lines.push("}");
return lines;
}
function findLinks(struct) {
var r = [];
R.mapObjIndexed(function(table, tablename) {
return R.mapObjIndexed(function(field, fieldname) {
var l, links, current = [];
links = R.defaultTo([], R.path(['links'], field));
R.map(function(link) {
l = link.target.split(".");
if (l.length < 2) {
l.push("id");
}
current = R.concat([tablename, fieldname], l);
if (link.hasOwnProperty('diaprops')) {
current.push(link.diaprops);
}
r.push(current);
}, links);
}, table);
}, struct);
return r;
}
function addLinkFields(struct) {
var links = findLinks(struct);
return R.reduce(function(myStruct, link) {
return R.assocPath(
R.slice(2, 4, link),
R.defaultTo(null, R.path(R.slice(2, 4, link), myStruct)),
myStruct
);
}, struct, links);
}
function writeLink(linkSpec) {
function asLineProp(v, k) {
return k + '=' + v;
}
function sorter(a, b) {
if (a < b) { return -1; }
return 1;
}
var propsStr = '';
if (linkSpec[4]) {
propsStr = R.pipe(
R.mapObjIndexed(asLineProp),
R.values,
R.sort(sorter),
R.join(', '),
R.concat(' ['),
R.flip(R.concat)("]")
)(linkSpec[4]);
}
return R.join(' -> ', [
R.join(':', ['struct' + structEscape(linkSpec[0]), structEscape(linkSpec[0]) + '__' + structEscape(linkSpec[1])]),
R.join(':', ['struct' + structEscape(linkSpec[2]), structEscape(linkSpec[2]) + '__' + structEscape(linkSpec[3])])
]) + propsStr;
}
function getDotSrc(struct) {
var finalStruct = addLinkFields(struct);
var inner = R.map(function(s) { return " " + s; }, R.flatten([
R.values(R.mapObjIndexed(writeTable, finalStruct)),
R.map(writeLink, findLinks(finalStruct))
]));
inner.unshift('graph [ranksep = 1]');
return R.flatten(["digraph db { ", inner, '}']);
}
function transform1(struct) {
return R.mapObjIndexed(function(table) {
return R.mapObjIndexed(function(field) {
if (typeof field == 'string') {
return { links: field.split(/, */g) };
}
return field;
}, table);
}, struct);
}
function transform2(struct) {
function getNewVal(link) {
if (typeof link == 'string') {
return { target: link };
}
return link;
}
return R.mapObjIndexed(function(table) {
return R.mapObjIndexed(function(field) {
if (field === null) { return null; }
var r = R.assocPath(
['links'],
R.map(
getNewVal,
R.defaultTo([], R.path(['links'], field))
),
field
);
if (field.hasOwnProperty('link')) {
if (!r.links) { r.links = []; }
r.links.push(getNewVal(r.link));
delete r.link;
}
return r;
}, table);
}, struct);
}
function transform(struct) {
return transform2(transform1(struct));
}
transform.transform1 = transform1;
transform.transform2 = transform2;
module.exports = {
writeTable: writeTable,
addLinkFields: addLinkFields,
findLinks: findLinks,
writeLink: writeLink,
getDotSrc: getDotSrc,
transform: transform
};