easier formatting
This commit is contained in:
parent
3c00d4f4af
commit
c57af9bf57
@ -1,15 +1,20 @@
|
||||
export class Element {
|
||||
public name: string;
|
||||
public parent: string;
|
||||
protected parent_name: string;
|
||||
|
||||
public get full_name (): string {
|
||||
if (this.parent)
|
||||
return `${this.parent}_${this.name}`;
|
||||
if (this.parent_name)
|
||||
return `${this.parent_name}_${this.name}`;
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public get parent (): string {
|
||||
return this.parent_name;
|
||||
}
|
||||
|
||||
public constructor (name: string, parent = '') {
|
||||
this.name = name;
|
||||
this.parent = parent;
|
||||
this.parent_name = parent;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
|
31
lib/Graph.ts
31
lib/Graph.ts
@ -32,27 +32,24 @@ export class Graph extends Element {
|
||||
.replace (/^\s+$/gmu, '');
|
||||
}
|
||||
|
||||
public add_node (constructor: () => Node): void {
|
||||
const node = constructor ();
|
||||
node.parent = this.full_name;
|
||||
public add_node (constructor: ((g: Node) => void) | string): void {
|
||||
if (typeof constructor === 'string') {
|
||||
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);
|
||||
}
|
||||
|
||||
public add_graph (constructor: () => Graph): void {
|
||||
const graph = constructor ();
|
||||
graph.parent = this.full_name;
|
||||
graph.update_parent ();
|
||||
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 ();
|
||||
public add_graph (constructor: ((g: Graph) => void) | string): void {
|
||||
if (typeof constructor === 'string') {
|
||||
this.children.push (new Graph (constructor, this.full_name));
|
||||
return;
|
||||
}
|
||||
const graph = new Graph ('unnamed', this.full_name);
|
||||
constructor (graph);
|
||||
this.children.push (graph);
|
||||
}
|
||||
|
||||
public add_edge (origin: string, target: string): void {
|
||||
|
@ -1,13 +1,13 @@
|
||||
import test from 'ava';
|
||||
import { Graph, Node } from '../lib';
|
||||
import { Graph } from '../lib';
|
||||
|
||||
const result = `digraph foo {
|
||||
subgraph cluster_foo_baz {
|
||||
foo_baz_asd
|
||||
foo_baz_asd [label=<asd>]
|
||||
}
|
||||
|
||||
foo_baz
|
||||
foo_foo
|
||||
foo_baz [label=<baz>]
|
||||
foo_foo [label=<foo>]
|
||||
|
||||
foo_foo -> foo_baz
|
||||
}`;
|
||||
@ -16,14 +16,13 @@ test ('serialize', (t) => {
|
||||
const g = new Graph ('foo');
|
||||
|
||||
t.is (g.full_name, 'foo');
|
||||
g.add_graph (() => {
|
||||
const graph = new Graph ('baz');
|
||||
graph.add_node (() => new Node ('asd'));
|
||||
return graph;
|
||||
g.add_graph ((graph) => {
|
||||
graph.name = 'baz';
|
||||
graph.add_node ('asd');
|
||||
});
|
||||
|
||||
g.add_node (() => new Node ('baz'));
|
||||
g.add_node (() => new Node ('foo'));
|
||||
g.add_node ('baz');
|
||||
g.add_node ('foo');
|
||||
g.add_edge ('foo', 'baz');
|
||||
|
||||
const serialized = g.toString ();
|
||||
|
Loading…
x
Reference in New Issue
Block a user