allow directional / undirectional
This commit is contained in:
		| @@ -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 | ||||
|       : ''}`; | ||||
|   } | ||||
|   | ||||
| @@ -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)); | ||||
|   } | ||||
| } | ||||
|   | ||||
							
								
								
									
										10
									
								
								test/Edge.ts
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								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 (); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user