diff --git a/lib/classes/GraphStream.ts b/lib/classes/GraphStream.ts index 6bcc1ff..33430b3 100644 --- a/lib/classes/GraphStream.ts +++ b/lib/classes/GraphStream.ts @@ -2,7 +2,10 @@ /* eslint-disable no-inline-comments */ import { Transform } from 'stream'; import { GraphStreamJSON } from '../interfaces/GraphStreamJSON'; -import { GraphStreamCommand } from '../enums/GraphStreamCommand'; +import { + GraphStreamCommand, + translate_command +} from '../enums/GraphStreamCommand'; import { validate_name } from '../Helper'; interface Stringable { @@ -12,7 +15,7 @@ interface Stringable { export class GraphStream extends Transform { private _path: string[] = []; - private _state = ''; + private _state: GraphStreamCommand | '' = ''; private _directional = false; public get path (): string { @@ -24,7 +27,7 @@ export class GraphStream extends Transform { } private expect_state (instr: GraphStreamCommand): void { - const states = []; + const states: (GraphStreamCommand|'')[] = []; if ([ 'cug', 'cdg' @@ -50,9 +53,11 @@ export class GraphStream extends Transform { break; } if (!states.includes (this._state)) { - throw new Error (`invalid state to execute command ${instr} - expected: ${states.join (', ')} - actual: ${this._state}`); + throw new Error (`invalid state to execute command ${ + translate_command (instr)} + expected: ${states.map ((s) => translate_command (s)) + .join (', ')} + actual: ${translate_command (this._state)}`); } } diff --git a/lib/enums/GraphStreamCommand.ts b/lib/enums/GraphStreamCommand.ts index d5e7eab..c1fef6a 100644 --- a/lib/enums/GraphStreamCommand.ts +++ b/lib/enums/GraphStreamCommand.ts @@ -1,11 +1,28 @@ /* eslint-disable line-comment-position */ /* eslint-disable no-inline-comments */ -export type GraphStreamCommand = - 'cn'| // create node - 'en'| // end node - 'cug'| // create unordered graph - 'cdg'| // create directional graph - 'csg'| // create subgraph - 'eg'| // end graph - 'at'| // add attributes - 'ce' // create edge +type GraphStreamCommand = + 'cn'| + 'en'| + 'cug'| + 'cdg'| + 'csg'| + 'eg'| + 'at'| + 'ce' + +function translate_command (cmd: GraphStreamCommand|''): string { + const translations = { + cn: 'create node', + en: 'end node', + cug: 'create unordered graph', + cdg: 'create directional graph', + csg: 'create subgraph', + eg: 'end graph', + at: 'attributes', + ce: 'create edge', + '': 'start' + }; + return translations[cmd]; +} + +export { GraphStreamCommand, translate_command };