allow directional / undirectional

This commit is contained in:
Timo Hocker 2020-04-28 08:39:57 +02:00
parent 6bdbda9e30
commit 373285ec46
3 changed files with 18 additions and 5 deletions

View File

@ -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
: ''}`;
}

View File

@ -18,12 +18,13 @@ export class Graph extends Element {
public edges: Array<Edge> = [];
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));
}
}

View File

@ -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 ();