From c57af9bf57bab9b78d21d877b557fac0bd2d9701 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 24 Apr 2020 14:05:58 +0200 Subject: [PATCH] easier formatting --- lib/Element.ts | 13 +++++++++---- lib/Graph.ts | 31 ++++++++++++++----------------- test/Graph.ts | 19 +++++++++---------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/lib/Element.ts b/lib/Element.ts index aef2349..d380bfc 100644 --- a/lib/Element.ts +++ b/lib/Element.ts @@ -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 diff --git a/lib/Graph.ts b/lib/Graph.ts index ec50de9..05f0969 100644 --- a/lib/Graph.ts +++ b/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 { diff --git a/test/Graph.ts b/test/Graph.ts index d045186..2319e48 100644 --- a/test/Graph.ts +++ b/test/Graph.ts @@ -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=] } - foo_baz - foo_foo + foo_baz [label=] + foo_foo [label=] 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 ();