-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphNormalizer.ts
More file actions
66 lines (53 loc) · 1.53 KB
/
Copy pathGraphNormalizer.ts
File metadata and controls
66 lines (53 loc) · 1.53 KB
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
import * as crypto from 'crypto';
export interface GraphTree {
node: {
containerName: string;
[key: string]: any;
};
dependencies: any[];
}
export interface NormalizedGraph {
definitions: Record<string, any>;
trees: GraphTree[][];
}
export class GraphNormalizer {
private definitions: Record<string, any>;
constructor(definitions: Record<string, any>) {
this.definitions = definitions;
}
public normalize(tree: GraphTree[]): GraphTree[] {
tree.forEach((node: any) => this.visit(node));
return tree;
}
private visit(obj: any): void {
if (!obj || typeof obj !== 'object') return;
if (Array.isArray(obj)) {
obj.forEach((item) => this.visit(item));
return;
}
if ('returnStructure' in obj) {
this.extractReturnStructure(obj);
}
if (obj.methods && Array.isArray(obj.methods)) {
obj.methods.forEach((method: any) => this.visit(method));
}
if (obj.dependencies) {
this.visit(obj.dependencies);
}
if (obj.node) {
this.visit(obj.node);
}
}
private extractReturnStructure(obj: any): void {
const content = obj.returnStructure;
const contentString = typeof content === 'string' ? content : JSON.stringify(content);
if (contentString.length > 50) {
const hash = crypto.createHash('md5').update(contentString).digest('hex').substring(0, 8); // NOSONAR
const refId = `REF:${hash}`;
if (!this.definitions[refId]) {
this.definitions[refId] = content;
}
obj.returnStructure = refId;
}
}
}