This commit is contained in:
Timo Hocker 2020-05-07 15:09:25 +02:00
parent ac3c66bc1c
commit ebd8936dae

View File

@ -3,6 +3,7 @@
import { Transform } from 'stream'; import { Transform } from 'stream';
import { GraphStreamJSON } from '../interfaces/GraphStreamJSON'; import { GraphStreamJSON } from '../interfaces/GraphStreamJSON';
import { GraphStreamCommand } from '../enums/GraphStreamCommand'; import { GraphStreamCommand } from '../enums/GraphStreamCommand';
import { validate_name } from '../Helper';
interface Stringable { interface Stringable {
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
@ -119,24 +120,26 @@ export class GraphStream extends Transform {
return super.write (JSON.stringify (instr), 'utf-8'); return super.write (JSON.stringify (instr), 'utf-8');
} }
public create_node (name: string): boolean { public create_node (name: string): string {
return this.write ({ type: 'cn', args: [ name ] }); const node_name = validate_name (name);
this.write ({ type: 'cn', args: [ node_name ] });
return `${this.path}_${node_name}`;
} }
public end_node (): boolean { public end_node (): void {
return this.write ({ type: 'en', args: [] }); this.write ({ type: 'en', args: [] });
} }
public create_graph (name: string, type: 'u'|'d'|'s' = 's'): boolean { public create_graph (name: string, type: 'u'|'d'|'s' = 's'): void {
const instr_type = `c${type}g` as GraphStreamCommand; const instr_type = `c${type}g` as GraphStreamCommand;
return this.write ({ type: instr_type, args: [ name ] }); this.write ({ type: instr_type, args: [ validate_name (name) ] });
} }
public end_graph (): boolean { public end_graph (): void {
return this.write ({ type: 'eg', args: [] }); this.write ({ type: 'eg', args: [] });
} }
public attributes (attrs: Record<string, Stringable>): boolean { public attributes (attrs: Record<string, Stringable>): void {
const solved = []; const solved = [];
for (const attr of Object.keys (attrs)) { for (const attr of Object.keys (attrs)) {
const val = attrs[attr].toString (); const val = attrs[attr].toString ();
@ -145,11 +148,11 @@ export class GraphStream extends Transform {
else else
solved.push (`${attr} = "${val}"`); solved.push (`${attr} = "${val}"`);
} }
return this.write ({ type: 'at', args: solved }); this.write ({ type: 'at', args: solved });
} }
public create_edge (origin: string, target: string): boolean { public create_edge (origin: string, target: string): void {
return this.write ({ this.write ({
type: 'ce', type: 'ce',
args: [ args: [
origin, origin,