Compare commits

...

4 Commits

Author SHA1 Message Date
cef87ba4a5 lint
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-25 14:17:58 +01:00
78556c6c98 node shape
Some checks failed
continuous-integration/drone/push Build is failing
2023-03-25 14:12:10 +01:00
019647b04a formatting, update
Some checks failed
continuous-integration/drone/push Build is failing
2021-05-02 11:30:21 +02:00
fa8e0d25c6 bump version
All checks were successful
continuous-integration/drone/push Build is passing
2020-10-05 20:16:10 +02:00
13 changed files with 893 additions and 1443 deletions

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.4.0
Allow specifying shapes for nodes
## 1.3.0 ## 1.3.0
Stream: Stream:

View File

@ -1,6 +1,6 @@
# @sapphirecode/graphviz-builder # @sapphirecode/graphviz-builder
version: 1.3.x version: 1.4.x
constructing graphviz files using an easy typescript interface constructing graphviz files using an easy typescript interface

View File

@ -5,20 +5,13 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020 * Created by Timo Hocker <timo@scode.ovh>, May 2020
*/ */
import { GraphStyles, NodeStyles } from '../enums/Styles'; import { GraphStyles } from '../enums/Styles';
import { GraphLayouts } from '../enums/GraphLayouts'; import { GraphLayouts } from '../enums/GraphLayouts';
import { Element } from './Element'; import { Element } from './Element';
import { Edge } from './Edge'; import { Edge } from './Edge';
import { Node } from './Node'; import { Node } from './Node';
import { Color } from './Color'; import { Color } from './Color';
interface NodeOptions {
name: string;
label: string;
style: NodeStyles;
color: Color;
}
export class Graph extends Element { export class Graph extends Element {
public children: Array<Graph> = []; public children: Array<Graph> = [];
public nodes: Array<Node> = []; public nodes: Array<Node> = [];
@ -83,7 +76,7 @@ export class Graph extends Element {
.replace (/^\s+$/gmu, ''); .replace (/^\s+$/gmu, '');
} }
public add_node (constructor: ((n: Node) => void) | string): string { public add_node (constructor: string | ((n: Node) => void)): string {
const node = new Node ('unnamed', this.full_name); const node = new Node ('unnamed', this.full_name);
if (typeof constructor === 'string') { if (typeof constructor === 'string') {
@ -96,7 +89,7 @@ export class Graph extends Element {
return node.full_name; return node.full_name;
} }
public add_graph (constructor: ((g: Graph) => void) | string): string { public add_graph (constructor: string | ((g: Graph) => void)): string {
const graph = new Graph ('unnamed', this.full_name); const graph = new Graph ('unnamed', this.full_name);
graph.directional = this.directional; graph.directional = this.directional;

View File

@ -42,7 +42,7 @@ export class GraphStream extends Transform {
return ' '.repeat (this._path.length); return ' '.repeat (this._path.length);
} }
private finish_node ():void { private finish_node (): void {
if ( if (
[ [
'ce', 'ce',
@ -168,7 +168,7 @@ export class GraphStream extends Transform {
// this.write ({ type: 'en', args: [] }); // this.write ({ type: 'en', args: [] });
} }
public create_graph (name: string, type: 'u'|'d'|'s' = 's'): void { public create_graph (name: string, type: 'd' | 's' | 'u' = 's'): void {
const instr_type = `c${type}g` as GraphStreamCommand; const instr_type = `c${type}g` as GraphStreamCommand;
this.write ({ type: instr_type, args: [ validate_name (name) ] }); this.write ({ type: instr_type, args: [ validate_name (name) ] });
} }

View File

@ -6,6 +6,7 @@
*/ */
import { NodeStyles } from '../enums/Styles'; import { NodeStyles } from '../enums/Styles';
import { NodeShapes } from '../enums/Shapes';
import { Element } from './Element'; import { Element } from './Element';
import { Color } from './Color'; import { Color } from './Color';
@ -15,6 +16,7 @@ export class Node extends Element {
public table_contents?: Array<Array<string>>; public table_contents?: Array<Array<string>>;
public style?: NodeStyles; public style?: NodeStyles;
public color?: Color; public color?: Color;
public shape?: NodeShapes;
public constructor (name: string, parent: string, label?: string) { public constructor (name: string, parent: string, label?: string) {
super (name, parent); super (name, parent);
@ -47,6 +49,8 @@ export class Node extends Element {
} }
if (this.style) if (this.style)
attributes.push ({ name: 'style', value: this.style.toString () }); attributes.push ({ name: 'style', value: this.style.toString () });
if (this.shape)
attributes.push ({ name: 'shape', value: this.shape.toString () });
if (this.color) if (this.color)
attributes.push ({ name: 'color', value: this.color.toString () }); attributes.push ({ name: 'color', value: this.color.toString () });

View File

@ -6,11 +6,4 @@
*/ */
export type GraphLayouts = export type GraphLayouts =
'neato' 'circo' | 'dot' | 'fdp' | 'neato' | 'osage' | 'patchwork' | 'sfdp' | 'twopi'
| 'dot'
| 'circo'
| 'fdp'
| 'sfdp'
| 'osage'
| 'twopi'
| 'patchwork'

View File

@ -8,13 +8,7 @@
/* eslint-disable line-comment-position */ /* eslint-disable line-comment-position */
/* eslint-disable no-inline-comments */ /* eslint-disable no-inline-comments */
type GraphStreamCommand = type GraphStreamCommand =
'cn'| 'at' | 'cdg' | 'ce' | 'cn' | 'csg' | 'cug' | 'eg'
'cug'|
'cdg'|
'csg'|
'eg'|
'at'|
'ce'
function translate_command (cmd: GraphStreamCommand|''): string { function translate_command (cmd: GraphStreamCommand|''): string {
const translations = { const translations = {

14
lib/enums/Shapes.ts Normal file
View File

@ -0,0 +1,14 @@
type NodeShapes =
'' | 'assembly' | 'box' | 'box3d' | 'cds' | 'circle' | 'component'
| 'cylinder' | 'diamond' | 'doublecircle' | 'doubleoctagon' | 'egg'
| 'ellipse' | 'fivepoverhang' | 'folder' | 'hexagon' | 'house'
| 'insulator' | 'invhouse' | 'invtrapezium' | 'invtriangle' | 'larrow'
| 'lpromoter' | 'Mcircle' | 'Mdiamond' | 'Msquare' | 'none' | 'note'
| 'noverhang' | 'octagon' | 'oval' | 'parallelogram' | 'pentagon'
| 'plain' | 'plaintext' | 'point' | 'polygon' | 'primersite' | 'promoter'
| 'proteasesite' | 'proteinstab' | 'rarrow' | 'rect' | 'rectangle'
| 'restrictionsite' | 'ribosite' | 'rnastab' | 'rpromoter' | 'septagon'
| 'signature' | 'square' | 'star' | 'tab' | 'terminator' | 'threepoverhang'
| 'trapezium' | 'triangle' | 'tripleoctagon' | 'underline' | 'utr'
export { NodeShapes };

View File

@ -6,34 +6,15 @@
*/ */
type EdgeStyles = type EdgeStyles =
'' '' | 'bold' | 'dashed' | 'dotted' | 'solid'
|'solid'
|'dashed'
|'dotted'
|'bold'
type NodeStyles = type NodeStyles =
'' '' | 'bold' | 'dashed' | 'diagonals' | 'dotted'
|'solid' | 'filled' | 'invis' | 'rounded' | 'solid' | 'striped' | 'wedged'
|'dashed'
|'dotted'
|'bold'
|'rounded'
|'diagonals'
|'filled'
|'striped'
|'wedged'
|'invis'
type GraphStyles = type GraphStyles =
'solid' 'bold' | 'dashed' | 'dotted' | 'filled' | 'rounded' | 'solid' | 'striped'
| 'dashed'
| 'dotted'
| 'bold'
| 'rounded'
| 'filled'
| 'striped'
export { EdgeStyles, NodeStyles, GraphStyles }; export { EdgeStyles, NodeStyles, GraphStyles };

View File

@ -1,7 +1,7 @@
{ {
"name": "@sapphirecode/graphviz-builder", "name": "@sapphirecode/graphviz-builder",
"main": "dist/lib/index.js", "main": "dist/lib/index.js",
"version": "1.3.3", "version": "1.4.0",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",
"email": "timo@scode.ovh" "email": "timo@scode.ovh"

View File

@ -8,7 +8,8 @@
import { Node, Color } from '../../lib'; import { Node, Color } from '../../lib';
const serialized_simple const serialized_simple
= 'bar_foo [label="baz", style="dashed", color="#00ff00"]'; = 'bar_foo [label="baz", style="dashed",'
+ ' shape="tripleoctagon", color="#00ff00"]';
const serialized_table = `bar_foo [label=<<table> const serialized_table = `bar_foo [label=<<table>
<tr><td>foo</td><td>bar</td><td>baz</td></tr> <tr><td>foo</td><td>bar</td><td>baz</td></tr>
<tr><td>bar</td><td>baz</td><td>foo</td></tr> <tr><td>bar</td><td>baz</td><td>foo</td></tr>
@ -21,6 +22,7 @@ describe ('node', () => {
g.color = Color.green; g.color = Color.green;
g.style = 'dashed'; g.style = 'dashed';
g.shape = 'tripleoctagon';
const serialized = g.toString (); const serialized = g.toString ();

View File

@ -43,7 +43,7 @@ const complex = `digraph foo {
// eslint-disable-next-line max-lines-per-function // eslint-disable-next-line max-lines-per-function
describe ('stream', () => { describe ('stream', () => {
it ('stream graph', () => new Promise ((resolve) => { it ('stream graph', () => new Promise<void> ((resolve) => {
let output = ''; let output = '';
const stream = (new GraphStream); const stream = (new GraphStream);
stream.on ('data', (data) => { stream.on ('data', (data) => {
@ -60,7 +60,7 @@ describe ('stream', () => {
})); }));
// eslint-disable-next-line max-statements // eslint-disable-next-line max-statements
it ('complex stream graph', () => new Promise ((resolve) => { it ('complex stream graph', () => new Promise<void> ((resolve) => {
let output = ''; let output = '';
const stream = (new GraphStream); const stream = (new GraphStream);
stream.on ('data', (data) => { stream.on ('data', (data) => {
@ -111,31 +111,31 @@ describe ('stream', () => {
* ce eg, cn, csg, ce, at cug, cdg * ce eg, cn, csg, ce, at cug, cdg
*/ */
const cn = (g:GraphStream) => { const cn = (g: GraphStream) => {
g.create_node ('foo'); g.create_node ('foo');
}; };
const cug = (g:GraphStream) => { const cug = (g: GraphStream) => {
g.create_graph ('foo', 'u'); g.create_graph ('foo', 'u');
}; };
const cdg = (g:GraphStream) => { const cdg = (g: GraphStream) => {
g.create_graph ('foo', 'd'); g.create_graph ('foo', 'd');
}; };
const csg = (g:GraphStream) => { const csg = (g: GraphStream) => {
g.create_graph ('foo', 's'); g.create_graph ('foo', 's');
}; };
const eg = (g:GraphStream) => { const eg = (g: GraphStream) => {
g.end_graph (); g.end_graph ();
}; };
const at = (g:GraphStream) => { const at = (g: GraphStream) => {
g.attributes ({ color: 'red' }); g.attributes ({ color: 'red' });
}; };
const ce = (g:GraphStream) => { const ce = (g: GraphStream) => {
g.create_edge ('foo', 'bar'); g.create_edge ('foo', 'bar');
}; };
const combinations = [ const combinations = [
{ {
primary: (g:GraphStream) => { primary: (g: GraphStream) => {
cug (g); cug (g);
cn (g); cn (g);
}, },
@ -159,7 +159,7 @@ describe ('stream', () => {
] ]
}, },
{ {
primary: (g:GraphStream) => { primary: (g: GraphStream) => {
cug (g); cug (g);
csg (g); csg (g);
}, },
@ -169,7 +169,7 @@ describe ('stream', () => {
] ]
}, },
{ {
primary: (g:GraphStream) => { primary: (g: GraphStream) => {
cug (g); cug (g);
csg (g); csg (g);
eg (g); eg (g);
@ -181,7 +181,7 @@ describe ('stream', () => {
] ]
}, },
{ {
primary: (g:GraphStream) => { primary: (g: GraphStream) => {
cug (g); cug (g);
at (g); at (g);
}, },
@ -192,7 +192,7 @@ describe ('stream', () => {
] ]
}, },
{ {
primary: (g:GraphStream) => { primary: (g: GraphStream) => {
cug (g); cug (g);
ce (g); ce (g);
}, },

2217
yarn.lock

File diff suppressed because it is too large Load Diff