This repository has been archived by the owner on Feb 11, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
circular.js
136 lines (114 loc) · 2.73 KB
/
circular.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
var _ = require('lodash');
var jptr = require('reftools/lib/jptr.js');
var common = require('./common.js');
function dump(defs,title,logger) {
if (defs.length>0) logger.log(title);
for (var d in defs) {
var def = defs[d];
logger.log(def.ref+' '+def.seen+' '+def.children.length);
for (var c in def.children) {
logger.log(' '+def.children[c].ref);
}
}
}
function topoSort(src,options) {
// https://en.wikipedia.org/wiki/Topological_sorting
var defs = [];
var logger = new common.logger(options.verbose);
common.recurse(src,{},function(obj,key,state){
if ((key == '$ref') && (typeof obj[key] === 'string')) {
if (obj[key] == state.path) {
logger.write(' Direct circular reference!');
}
var entry = {};
var found = false;
for (var d in defs) {
var def = defs[d];
if (def.ref == obj) {
found = true;
def.seen++;
entry = def;
}
if (found) break;
}
if (!found) {
entry.ref = obj[key];
entry.seen = 1;
entry.children = [];
defs.push(entry);
}
var ref = obj[key];
var restart = jptr.jptr(src,ref);
var parent = state.parent;
var newState = {};
common.recurse(restart,newState,function(obj,key,state) {
if ((key == '$ref') && (typeof obj[key] === 'string')) {
var child = {};
child.ref = obj[key];
var found = false;
for (var c in entry.children) {
var compare = entry.children[c];
if (compare.ref === obj[key]) {
found = true;
}
}
if (!found) {
entry.children.push(child);
}
if (child.ref == entry.ref) {
entry.seen++;
}
}
});
}
});
var changes = 1;
while (changes>0) {
changes = 0;
for (var d in defs) {
var def = defs[d];
if ((def.seen<=0) || (def.children.length<=0)) {
// remove all references to 'empty' this $ref as children
for (var dd in defs) {
var compare = defs[dd];
_.remove(compare.children, function(c){
var result = (c.ref == def.ref);
if (result) {
def.seen--;
changes++;
}
return result;
});
}
// remove the $ref itself
defs.splice(d,1);
changes++;
}
}
}
dump(defs,'Circular refs:',logger);
return defs;
}
module.exports = {
getCircularRefs : topoSort,
isCircular : function (circles,ref) {
for (var c in circles) {
var circle = circles[c];
for (var cc in circle.children) {
var child = circle.children[cc];
if ((circle.ref === ref) && (child.ref === ref)) return true;
}
}
return false;
},
hasCircles : function (circles,ref) {
for (var c in circles) {
var circle = circles[c];
for (var cc in circle.children) {
var child = circle.children[cc];
if (child.ref === ref) return true;
}
}
return false;
}
};