easier formatting

This commit is contained in:
Timo Hocker 2020-04-24 14:05:58 +02:00
parent 3c00d4f4af
commit c57af9bf57
3 changed files with 32 additions and 31 deletions

View File

@ -1,15 +1,20 @@
export class Element { export class Element {
public name: string; public name: string;
public parent: string; protected parent_name: string;
public get full_name (): string { public get full_name (): string {
if (this.parent) if (this.parent_name)
return `${this.parent}_${this.name}`; return `${this.parent_name}_${this.name}`;
return this.name; return this.name;
} }
public get parent (): string {
return this.parent_name;
}
public constructor (name: string, parent = '') { public constructor (name: string, parent = '') {
this.name = name; this.name = name;
this.parent = parent; this.parent_name = parent;
} }
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention

View File

@ -32,27 +32,24 @@ export class Graph extends Element {
.replace (/^\s+$/gmu, ''); .replace (/^\s+$/gmu, '');
} }
public add_node (constructor: () => Node): void { public add_node (constructor: ((g: Node) => void) | string): void {
const node = constructor (); if (typeof constructor === 'string') {
node.parent = this.full_name; this.nodes.push (new Node (constructor, this.full_name, constructor));
return;
}
const node = new Node ('unnamed', this.full_name);
constructor (node);
this.nodes.push (node); this.nodes.push (node);
} }
public add_graph (constructor: () => Graph): void { public add_graph (constructor: ((g: Graph) => void) | string): void {
const graph = constructor (); if (typeof constructor === 'string') {
graph.parent = this.full_name; this.children.push (new Graph (constructor, this.full_name));
graph.update_parent (); return;
this.children.push (graph);
}
public update_parent (): void {
for (const node of this.nodes)
node.parent = this.full_name;
for (const graph of this.children) {
graph.parent = this.full_name;
graph.update_parent ();
} }
const graph = new Graph ('unnamed', this.full_name);
constructor (graph);
this.children.push (graph);
} }
public add_edge (origin: string, target: string): void { public add_edge (origin: string, target: string): void {

View File

@ -1,13 +1,13 @@
import test from 'ava'; import test from 'ava';
import { Graph, Node } from '../lib'; import { Graph } from '../lib';
const result = `digraph foo { const result = `digraph foo {
subgraph cluster_foo_baz { subgraph cluster_foo_baz {
foo_baz_asd foo_baz_asd [label=<asd>]
} }
foo_baz foo_baz [label=<baz>]
foo_foo foo_foo [label=<foo>]
foo_foo -> foo_baz foo_foo -> foo_baz
}`; }`;
@ -16,14 +16,13 @@ test ('serialize', (t) => {
const g = new Graph ('foo'); const g = new Graph ('foo');
t.is (g.full_name, 'foo'); t.is (g.full_name, 'foo');
g.add_graph (() => { g.add_graph ((graph) => {
const graph = new Graph ('baz'); graph.name = 'baz';
graph.add_node (() => new Node ('asd')); graph.add_node ('asd');
return graph;
}); });
g.add_node (() => new Node ('baz')); g.add_node ('baz');
g.add_node (() => new Node ('foo')); g.add_node ('foo');
g.add_edge ('foo', 'baz'); g.add_edge ('foo', 'baz');
const serialized = g.toString (); const serialized = g.toString ();