• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphty-org / graphty-element / 19792929756

30 Nov 2025 02:57AM UTC coverage: 86.308% (+3.9%) from 82.377%
19792929756

push

github

apowers313
docs: fix stories for chromatic

3676 of 4303 branches covered (85.43%)

Branch coverage included in aggregate %.

17 of 17 new or added lines in 2 files covered. (100.0%)

1093 existing lines in 30 files now uncovered.

17371 of 20083 relevant lines covered (86.5%)

7075.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.85
/src/algorithms/Algorithm.ts
1
import {set as deepSet} from "lodash";
3✔
2

3
import {AdHocData, SuggestedStylesConfig, SuggestedStylesProvider} from "../config";
4
import {Edge} from "../Edge";
5
import {Graph} from "../Graph";
6

7
type AlgorithmClass = new (g: Graph) => Algorithm;
8
const algorithmRegistry = new Map<string, AlgorithmClass>();
3✔
9

10
// algorithmResults layout:
11
// {
12
//     node: {
13
//         id: {
14
//             namespace: {
15
//                 algorithm: {
16
//                     result: unknown
17
//                 }
18
//             }
19
//         }
20
//     },
21
//     edge: {
22
//         id: {
23
//             namespace: {
24
//                 algorithm: {
25
//                     result: unknown
26
//                 }
27
//             }
28
//         }
29
//     },
30
//     graph: {
31
//         namespace: {
32
//             algorithm: {
33
//                 result: unknown
34
//             }
35
//         }
36
//     }
37
// }
38

39
export abstract class Algorithm {
3✔
40
    static type: string;
41
    static namespace: string;
42
    static suggestedStyles?: SuggestedStylesProvider;
43
    protected graph: Graph;
44

45
    constructor(g: Graph) {
3✔
46
        this.graph = g;
143✔
47
    }
143✔
48

49
    get type(): string {
3✔
50
        return (this.constructor as typeof Algorithm).type;
14,435✔
51
    }
14,435✔
52

53
    get namespace(): string {
3✔
54
        return (this.constructor as typeof Algorithm).namespace;
14,435✔
55
    }
14,435✔
56

57
    get results(): AdHocData {
3✔
58
        const algorithmResults = {} as AdHocData;
13✔
59

60
        // Node results
61
        for (const n of this.graph.getDataManager().nodes.values()) {
13✔
62
            deepSet(algorithmResults, `node.${n.id}`, n.algorithmResults);
554✔
63
        }
554✔
64

65
        // Edge results
66
        for (const e of this.graph.getDataManager().edges.values()) {
13✔
67
            const edgeKey = `${e.srcId}:${e.dstId}`;
1,790✔
68
            deepSet(algorithmResults, `edge.${edgeKey}`, e.algorithmResults);
1,790✔
69
        }
1,790✔
70

71
        // Graph results
72
        const dm = this.graph.getDataManager();
13✔
73
        if (dm.graphResults) {
13✔
74
            algorithmResults.graph = dm.graphResults;
11✔
75
        }
11✔
76

77
        return algorithmResults;
13✔
78
    }
13✔
79

80
    abstract run(g: Graph): Promise<void>;
81

82
    #createPath(resultName: string): string[] {
3✔
83
        const ret: string[] = [];
14,222✔
84

85
        ret.push("algorithmResults");
14,222✔
86
        ret.push(this.namespace);
14,222✔
87
        ret.push(this.type);
14,222✔
88
        ret.push(resultName);
14,222✔
89

90
        return ret;
14,222✔
91
    }
14,222✔
92

93
    addNodeResult(nodeId: number | string, resultName: string, result: unknown): void {
3✔
94
        const p = this.#createPath(resultName);
11,012✔
95
        const n = this.graph.getDataManager().nodes.get(nodeId);
11,012✔
96
        if (!n) {
11,012!
97
            throw new Error(`couldn't find nodeId '${nodeId}' while trying to run algorithm '${this.type}'`);
×
UNCOV
98
        }
×
99

100
        deepSet(n, p, result);
11,012✔
101
        // XXX: THIS IS WHERE I LEFT OFF
102
        // replace algorithmResults with graph.nodes; set result on each node.algorithmResult
103
    }
11,012✔
104

105
    addEdgeResult(edge: Edge, resultName: string, result: unknown): void {
3✔
106
        const p = this.#createPath(resultName);
3,210✔
107
        deepSet(edge, p, result);
3,210✔
108
    }
3,210✔
109

110
    addGraphResult(resultName: string, result: unknown): void {
3✔
111
        const dm = this.graph.getDataManager();
213✔
112
        dm.graphResults ??= {} as AdHocData;
213✔
113

114
        const path = [this.namespace, this.type, resultName];
213✔
115
        deepSet(dm.graphResults, path, result);
213✔
116
    }
213✔
117

118
    static register<T extends AlgorithmClass>(cls: T): T {
3✔
119
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
        const t: string = (cls as any).type;
3,632✔
121
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
        const ns: string = (cls as any).namespace;
3,632✔
123
        algorithmRegistry.set(`${ns}:${t}`, cls);
3,632✔
124
        return cls;
3,632✔
125
    }
3,632✔
126

127
    static get(g: Graph, namespace: string, type: string): Algorithm | null {
3✔
128
        const SourceClass = algorithmRegistry.get(`${namespace}:${type}`);
66✔
129
        if (SourceClass) {
66✔
130
            return new SourceClass(g);
66✔
131
        }
66!
132

UNCOV
133
        return null;
×
134
    }
66✔
135

136
    static getClass(namespace: string, type: string): (AlgorithmClass & typeof Algorithm) | null {
3✔
137
        return algorithmRegistry.get(`${namespace}:${type}`) as (AlgorithmClass & typeof Algorithm) | null ?? null;
75✔
138
    }
75✔
139

140
    /**
141
     * Check if this algorithm has suggested styles
142
     */
143
    static hasSuggestedStyles(): boolean {
3✔
144
        return !!this.suggestedStyles;
82✔
145
    }
82✔
146

147
    /**
148
     * Get suggested styles for this algorithm
149
     */
150
    static getSuggestedStyles(): SuggestedStylesConfig | null {
3✔
151
        return this.suggestedStyles ? this.suggestedStyles() : null;
220✔
152
    }
220✔
153
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc