more layout engines

This commit is contained in:
Timo Hocker 2020-04-28 11:26:21 +02:00
parent 6eb3dac397
commit da6c26e8e0
8 changed files with 24 additions and 2 deletions

View File

@ -7,6 +7,7 @@ const fs = require ('fs');
const child_process = require ('child_process'); const child_process = require ('child_process');
const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8')); const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8'));
[ [
,, pkg.version ,, pkg.version
] = process.argv; ] = process.argv;

View File

@ -17,6 +17,7 @@ export class Edge {
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
public toString (): string { public toString (): string {
const attributes = []; const attributes = [];
if (this.style) if (this.style)
attributes.push ({ name: 'style', value: this.style.toString () }); attributes.push ({ name: 'style', value: this.style.toString () });
if (this.color) if (this.color)

View File

@ -14,6 +14,7 @@ export class Element {
public constructor (name: string, parent = '') { public constructor (name: string, parent = '') {
const regex = /^[a-z_][a-z_0-9]+$/iu; const regex = /^[a-z_][a-z_0-9]+$/iu;
if (!regex.test (name)) if (!regex.test (name))
throw new Error ('invalid name specified'); throw new Error ('invalid name specified');
this.name = name; this.name = name;

View File

@ -1,4 +1,10 @@
export enum GraphLayouts { export enum GraphLayouts {
neato= 'neato', neato = 'neato',
dot= 'dot' dot = 'dot',
circo = 'circo',
fdp = 'fdp',
sfdp = 'sfdp',
osage = 'osage',
twopi = 'twopi',
patchwork = 'patchwork'
} }

View File

@ -20,6 +20,7 @@ export class Node extends Element {
const mapped_columns = this.table_contents const mapped_columns = this.table_contents
.map ((val) => `<td>${val.join ('</td><td>')}</td>`); .map ((val) => `<td>${val.join ('</td><td>')}</td>`);
return `<table>\n <tr>${ return `<table>\n <tr>${
mapped_columns.join ('</tr>\n <tr>') mapped_columns.join ('</tr>\n <tr>')
}</tr>\n</table>`; }</tr>\n</table>`;
@ -28,6 +29,7 @@ export class Node extends Element {
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
public toString (): string { public toString (): string {
const attributes = []; const attributes = [];
if (this.label || this.is_table) { if (this.label || this.is_table) {
attributes.push ({ attributes.push ({
name: 'label', name: 'label',
@ -51,6 +53,7 @@ export class Node extends Element {
'"', '"',
'"' '"'
]; ];
return `${v.name}=${d[0]}${v.value}${d[1]}`; return `${v.name}=${d[0]}${v.value}${d[1]}`;
}) })
.join (', '); .join (', ');

View File

@ -4,6 +4,7 @@ import { Color } from '../lib';
test ('serialize', (t) => { test ('serialize', (t) => {
const wh = Color.white; const wh = Color.white;
const tr = Color.transparent; const tr = Color.transparent;
t.is (wh.toString (), '#ffffff'); t.is (wh.toString (), '#ffffff');
t.is (tr.toString (), '#00000000'); t.is (tr.toString (), '#00000000');
}); });

View File

@ -3,16 +3,20 @@ import { Edge, Color, EdgeStyles } from '../lib';
test ('serialize', (t) => { test ('serialize', (t) => {
const e = new Edge ('foo', 'bar', false); const e = new Edge ('foo', 'bar', false);
e.color = Color.white; e.color = Color.white;
e.style = EdgeStyles.dashed; e.style = EdgeStyles.dashed;
const serialized = e.toString (); const serialized = e.toString ();
t.is (serialized, 'foo -- bar [style="dashed",color="#ffffff"]'); t.is (serialized, 'foo -- bar [style="dashed",color="#ffffff"]');
}); });
test ('serialize directional', (t) => { test ('serialize directional', (t) => {
const e = new Edge ('foo', 'bar', true); const e = new Edge ('foo', 'bar', true);
e.color = Color.white; e.color = Color.white;
e.style = EdgeStyles.dashed; e.style = EdgeStyles.dashed;
const serialized = e.toString (); const serialized = e.toString ();
t.is (serialized, 'foo -> bar [style="dashed",color="#ffffff"]'); t.is (serialized, 'foo -> bar [style="dashed",color="#ffffff"]');
}); });

View File

@ -11,16 +11,19 @@ const serialized_table = `bar_foo [label=<<table>
test ('serialize simple', (t) => { test ('serialize simple', (t) => {
const g = new Node ('foo', 'bar', 'baz'); const g = new Node ('foo', 'bar', 'baz');
g.color = Color.green; g.color = Color.green;
g.style = NodeStyles.dashed; g.style = NodeStyles.dashed;
const serialized = g.toString (); const serialized = g.toString ();
t.is (g.full_name, 'bar_foo'); t.is (g.full_name, 'bar_foo');
t.is (serialized, serialized_simple); t.is (serialized, serialized_simple);
}); });
test ('serialize table', (t) => { test ('serialize table', (t) => {
const g = new Node ('foo', 'bar', 'baz'); const g = new Node ('foo', 'bar', 'baz');
g.color = Color.green; g.color = Color.green;
g.style = NodeStyles.invisible; g.style = NodeStyles.invisible;
@ -44,6 +47,7 @@ test ('serialize table', (t) => {
g.is_table = true; g.is_table = true;
const serialized = g.toString (); const serialized = g.toString ();
t.is (g.full_name, 'bar_foo'); t.is (g.full_name, 'bar_foo');
t.is (serialized, serialized_table); t.is (serialized, serialized_table);
}); });
@ -51,6 +55,7 @@ test ('serialize table', (t) => {
test ('adhere to naming convention', (t) => { test ('adhere to naming convention', (t) => {
t.throws (() => { t.throws (() => {
const n = new Node ('invalid.name', 'parent'); const n = new Node ('invalid.name', 'parent');
return n.toString (); return n.toString ();
}, { message: 'invalid name specified' }); }, { message: 'invalid name specified' });
}); });