From ce6e119a8d53c9b7d723446457bedc7fd0a97e48 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 24 Apr 2020 12:21:26 +0200 Subject: [PATCH] fix spacing --- lib/Element.ts | 4 +++- lib/Graph.ts | 25 +++++++++++++++++-------- test/Graph.ts | 18 ++++++++---------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/Element.ts b/lib/Element.ts index bc87ca4..aef2349 100644 --- a/lib/Element.ts +++ b/lib/Element.ts @@ -2,7 +2,9 @@ export class Element { public name: string; public parent: string; public get full_name (): string { - return `${this.parent}_${this.name}`; + if (this.parent) + return `${this.parent}_${this.name}`; + return this.name; } public constructor (name: string, parent = '') { diff --git a/lib/Graph.ts b/lib/Graph.ts index 4c75d1d..ec50de9 100644 --- a/lib/Graph.ts +++ b/lib/Graph.ts @@ -10,16 +10,25 @@ export class Graph extends Element { // eslint-disable-next-line @typescript-eslint/naming-convention public toString (level = 0): string { - return `subgraph cluster_${this.full_name} { - ${this.children.map ((c) => c.toString (level + 1)) - .join ('\n ')} + const header = this.parent + ? `subgraph cluster_${this.full_name}` + : `digraph ${this.full_name}`; + let children = `\n ${this.children.map ((c) => c.toString (level + 1)) + .join ('\n ')}\n`; + let nodes = `\n ${this.nodes.map ((c) => c.toString ()) + .join ('\n ')}\n`; + let edges = `\n ${this.edges.map ((c) => c.toString ()) + .join ('\n ')}\n`; - ${this.nodes.map ((c) => c.toString ()) - .join ('\n ')} + if (children === '\n \n') + children = ''; + if (nodes === '\n \n') + nodes = ''; + if (edges === '\n \n') + edges = ''; - ${this.edges.map ((c) => c.toString ()) - .join ('\n ')} -}`.replace (/\n/gu, `\n${' '.repeat (level)}`) + return `${header} {${children}${nodes}${edges}}` + .replace (/\n/gu, `\n${' '.repeat (level)}`) .replace (/^\s+$/gmu, ''); } diff --git a/test/Graph.ts b/test/Graph.ts index 62459b6..d045186 100644 --- a/test/Graph.ts +++ b/test/Graph.ts @@ -1,23 +1,21 @@ import test from 'ava'; import { Graph, Node } from '../lib'; -const result = `subgraph cluster_bar_foo { - subgraph cluster_bar_foo_baz { - - bar_foo_baz_asd - +const result = `digraph foo { + subgraph cluster_foo_baz { + foo_baz_asd } - bar_foo_baz - bar_foo_foo + foo_baz + foo_foo - bar_foo_foo -> bar_foo_baz + foo_foo -> foo_baz }`; test ('serialize', (t) => { - const g = new Graph ('foo', 'bar'); + const g = new Graph ('foo'); - t.is (g.full_name, 'bar_foo'); + t.is (g.full_name, 'foo'); g.add_graph (() => { const graph = new Graph ('baz'); graph.add_node (() => new Node ('asd'));