diff --git a/lib/Edge.ts b/lib/Edge.ts index 2ba92ca..638b293 100644 --- a/lib/Edge.ts +++ b/lib/Edge.ts @@ -6,10 +6,12 @@ export class Edge { public target: string; public style?: EdgeStyles; public color?: Color; + private _directional: boolean; - public constructor (origin: string, target: string) { + public constructor (origin: string, target: string, directional: boolean) { this.origin = origin; this.target = target; + this._directional = directional; } // eslint-disable-next-line @typescript-eslint/naming-convention @@ -23,7 +25,9 @@ export class Edge { const attr_string = ` [${attributes.map ((v) => `${v.name}="${v.value}"`) .join (',')}]`; - return `${this.origin} -> ${this.target}${attributes.length > 0 + return `${this.origin} -${ + this._directional ? '>' : '-' + } ${this.target}${attributes.length > 0 ? attr_string : ''}`; } diff --git a/lib/Graph.ts b/lib/Graph.ts index 6582c77..43707e8 100644 --- a/lib/Graph.ts +++ b/lib/Graph.ts @@ -18,12 +18,13 @@ export class Graph extends Element { public edges: Array = []; public style?: GraphStyles; public color?: Color; + public directional = true; // eslint-disable-next-line @typescript-eslint/naming-convention public toString (): string { const header = this.parent ? `subgraph cluster_${this.full_name}` - : `digraph ${this.full_name}`; + : `${this.directional ? 'di' : ''}graph ${this.full_name}`; const attributes = []; if (this.color) attributes.push ({ name: 'color', value: this.color.toString () }); @@ -85,6 +86,6 @@ export class Graph extends Element { } public add_edge (origin: string, target: string): void { - this.edges.push (new Edge (origin, target)); + this.edges.push (new Edge (origin, target, this.directional)); } } diff --git a/test/Edge.ts b/test/Edge.ts index 9cd51c9..0daa371 100644 --- a/test/Edge.ts +++ b/test/Edge.ts @@ -2,7 +2,15 @@ import test from 'ava'; import { Edge, Color, EdgeStyles } from '../lib'; test ('serialize', (t) => { - const e = new Edge ('foo', 'bar'); + const e = new Edge ('foo', 'bar', false); + e.color = Color.white; + e.style = EdgeStyles.dashed; + const serialized = e.toString (); + t.is (serialized, 'foo -- bar [style="dashed",color="#ffffff"]'); +}); + +test ('serialize directional', (t) => { + const e = new Edge ('foo', 'bar', true); e.color = Color.white; e.style = EdgeStyles.dashed; const serialized = e.toString ();