ts-graphviz 1.0.0-3 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/core/index.js CHANGED
@@ -1,19 +1,21 @@
1
1
  import { isNodeRefGroupLike, toNodeRefGroup, toNodeRef, isNodeRefLike } from '#lib/common';
2
2
  import { fromModel, stringify } from '#lib/ast';
3
3
 
4
+ /**
5
+ * @group Attribute
6
+ */
4
7
  const attribute = new Proxy(Object.freeze({}), {
5
8
  get: (_, key) => key,
6
9
  });
7
10
 
8
11
  /**
9
- * Classes implemented in the 'ts-graphviz' library are designed to inherit from this class.
10
- */
11
- class GraphvizObject {}
12
- /**
13
- * Classes implemented in the 'ts-graphviz' library that implement the `toDot` method are designed to inherit from this class.
12
+ * Base class for DOT objects.
13
+ * @group Models
14
14
  */
15
- class DotObject extends GraphvizObject {}
15
+ class DotObject {}
16
16
  /**
17
+ * Base class for DOT objects with attributes.
18
+ * @group Models
17
19
  */
18
20
  class AttributesBase extends DotObject {
19
21
  /** @hidden */
@@ -27,15 +29,12 @@ class AttributesBase extends DotObject {
27
29
  get values() {
28
30
  return Array.from(this.#attrs.entries());
29
31
  }
30
- /** The size of the attribute. */
31
32
  get size() {
32
33
  return this.#attrs.size;
33
34
  }
34
- /** The size of the attribute. */
35
35
  get(key) {
36
36
  return this.#attrs.get(key);
37
37
  }
38
- /** Set a value to the attribute. */
39
38
  set(key, value) {
40
39
  if (value !== null && value !== undefined) {
41
40
  this.#attrs.set(key, value);
@@ -56,20 +55,20 @@ class AttributesBase extends DotObject {
56
55
  }
57
56
  /**
58
57
  * A set of attribute values for any object.
58
+ * @group Models
59
59
  */
60
60
  class AttributesGroupModel extends AttributesBase {
61
- /** Comments to include when outputting with toDot. */
62
61
  comment;
63
62
  }
64
63
  /**
65
64
  * A set of attribute values for any object.
65
+ * @group Models
66
66
  */
67
67
  class AttributeList extends AttributesBase {
68
68
  $$kind;
69
69
  get $$type() {
70
70
  return 'AttributeList';
71
71
  }
72
- /** Comments to include when outputting with toDot. */
73
72
  comment;
74
73
  constructor($$kind, attributes) {
75
74
  super(attributes);
@@ -77,73 +76,42 @@ class AttributeList extends AttributesBase {
77
76
  }
78
77
  }
79
78
  /**
80
- * Base class for clusters.
81
- * @hidden
79
+ * Base class for Graph objects.
80
+ * @group Models
82
81
  */
83
82
  class GraphBase extends AttributesBase {
84
- /** Cluster ID */
85
83
  id;
86
- /** Comments to include when outputting with toDot. */
87
84
  comment;
88
- /**
89
- * Nodes in the cluster.
90
- * @hidden
91
- */
92
85
  get nodes() {
93
86
  return Array.from(this.#objects.nodes.values());
94
87
  }
95
- /**
96
- * Edges in the cluster.
97
- * @hidden
98
- */
99
88
  get edges() {
100
89
  return Array.from(this.#objects.edges.values());
101
90
  }
102
- /**
103
- * Subgraphs in the cluster.
104
- * @hidden
105
- */
106
91
  get subgraphs() {
107
92
  return Array.from(this.#objects.subgraphs.values());
108
93
  }
94
+ /** @hidden */
109
95
  #objects = {
110
96
  nodes: new Map(),
111
97
  edges: new Set(),
112
98
  subgraphs: new Set(),
113
99
  };
114
- /**
115
- * Add a Node to the cluster.
116
- */
117
100
  addNode(node) {
118
101
  this.#objects.nodes.set(node.id, node);
119
102
  }
120
- /**
121
- * Add Edge to the cluster.
122
- */
123
103
  addEdge(edge) {
124
104
  this.#objects.edges.add(edge);
125
105
  }
126
- /**
127
- * Add a Subgraph to the cluster.
128
- */
129
106
  addSubgraph(subgraph) {
130
107
  this.#objects.subgraphs.add(subgraph);
131
108
  }
132
- /**
133
- * Check if the Node exists in the cluster.
134
- */
135
109
  existNode(nodeId) {
136
110
  return this.#objects.nodes.has(nodeId);
137
111
  }
138
- /**
139
- * Check if the Edge exists in the cluster.
140
- */
141
112
  existEdge(edge) {
142
113
  return this.#objects.edges.has(edge);
143
114
  }
144
- /**
145
- * Check if the Subgraph exists in the cluster.
146
- */
147
115
  existSubgraph(subgraph) {
148
116
  return this.#objects.subgraphs.has(subgraph);
149
117
  }
@@ -154,50 +122,26 @@ class GraphBase extends AttributesBase {
154
122
  this.#objects.subgraphs.add(graph);
155
123
  return graph;
156
124
  }
157
- /**
158
- * Remove Node from the cluster.
159
- */
160
125
  removeNode(node) {
161
126
  this.#objects.nodes.delete(typeof node === 'string' ? node : node.id);
162
127
  }
163
- /**
164
- * Remove Edge from the cluster.
165
- */
166
128
  removeEdge(edge) {
167
129
  this.#objects.edges.delete(edge);
168
130
  }
169
- /**
170
- * Remove Subgraph from the cluster.
171
- */
172
131
  removeSubgraph(subgraph) {
173
132
  this.#objects.subgraphs.delete(subgraph);
174
133
  }
175
- /**
176
- * Create a Node in the cluster.
177
- */
178
134
  createNode(id, attributes) {
179
135
  const node = new Node(id, attributes);
180
136
  this.#objects.nodes.set(id, node);
181
137
  return node;
182
138
  }
183
- /**
184
- * Get Subgraph in cluster by specifying id.
185
- *
186
- * If there is no Subgraph with the specified id in the cluster, return undefined.
187
- */
188
139
  getSubgraph(id) {
189
140
  return Array.from(this.#objects.subgraphs.values()).find((subgraph) => subgraph.id === id);
190
141
  }
191
- /**EdgeAttributesObject
192
- * Get Node in cluster by specifying id.
193
- *
194
- * @description
195
- * If there is no Node with the specified id in the cluster, return undefined.
196
- */
197
142
  getNode(id) {
198
143
  return this.#objects.nodes.get(id);
199
144
  }
200
- /** Create Edge and add it to the cluster. */
201
145
  createEdge(targets, attributes) {
202
146
  const ts = targets.map((t) => (isNodeRefGroupLike(t) ? toNodeRefGroup(t) : toNodeRef(t)));
203
147
  const edge = new Edge(ts, attributes);
@@ -250,33 +194,13 @@ class GraphBase extends AttributesBase {
250
194
  this.attributes.edge.apply(firstArg);
251
195
  }
252
196
  }
253
- /**
254
- * Set a common attribute for the clusters in the cluster.
255
- *
256
- * ```ts
257
- * const G = digraph('G', (g) => {
258
- * g.graph({
259
- * [attribute.color]: 'red',
260
- * [attribute.label]: 'my label',
261
- * });
262
- * });
263
- *
264
- * console.log(toDot(G));
265
- * // digraph "G" {
266
- * // graph [
267
- * // color = "red",
268
- * // label = "my label",
269
- * // ];
270
- * // }
271
- * ```
272
- * @param attributes Object of attributes to be adapted to the clusters.
273
- */
274
197
  graph(attributes) {
275
198
  this.attributes.graph.apply(attributes);
276
199
  }
277
200
  }
278
201
  /**
279
- * Subgraph object.
202
+ * DOT object class representing a subgraph.
203
+ * @group Models
280
204
  */
281
205
  class Subgraph extends GraphBase {
282
206
  get $$type() {
@@ -296,7 +220,6 @@ class Subgraph extends GraphBase {
296
220
  this.apply(attributes);
297
221
  }
298
222
  }
299
- /** Determines whether the Subgraph is a SubgraphCluster. */
300
223
  isSubgraphCluster() {
301
224
  if (typeof this.id === 'string') {
302
225
  return this.id.startsWith('cluster');
@@ -305,14 +228,14 @@ class Subgraph extends GraphBase {
305
228
  }
306
229
  }
307
230
  /**
308
- * Node object.
231
+ * DOT object class representing a node.
232
+ * @group Models
309
233
  */
310
234
  class Node extends DotObject {
311
235
  id;
312
236
  get $$type() {
313
237
  return 'Node';
314
238
  }
315
- /** Comments to include when outputting with toDot. */
316
239
  comment;
317
240
  attributes;
318
241
  constructor(id, attributes) {
@@ -320,7 +243,6 @@ class Node extends DotObject {
320
243
  this.id = id;
321
244
  this.attributes = new AttributesGroupModel(attributes);
322
245
  }
323
- /** Returns ForwardRefNode with port and compass specified. */
324
246
  port(port) {
325
247
  if (typeof port === 'string') {
326
248
  return { id: this.id, port };
@@ -329,13 +251,14 @@ class Node extends DotObject {
329
251
  }
330
252
  }
331
253
  /**
254
+ * DOT object class representing a edge.
255
+ * @group Models
332
256
  */
333
257
  class Edge extends DotObject {
334
258
  targets;
335
259
  get $$type() {
336
260
  return 'Edge';
337
261
  }
338
- /** Comments to include when outputting with toDot. */
339
262
  comment;
340
263
  attributes;
341
264
  constructor(targets, attributes) {
@@ -348,23 +271,14 @@ class Edge extends DotObject {
348
271
  }
349
272
  }
350
273
  /**
351
- * Base class for RootGraph.
352
- *
274
+ * Base class representing a root graph(digraph, graph).
275
+ * @group Models
353
276
  */
354
277
  class RootGraph extends GraphBase {
355
278
  get $$type() {
356
279
  return 'Graph';
357
280
  }
358
281
  id;
359
- /**
360
- * Strict mode.
361
- *
362
- * @description
363
- * A graph may also be described as strict.
364
- * This forbids the creation of multi-edges, i.e., there can be at most one edge with a given tail node and head node in the directed case.
365
- * For undirected graphs, there can be at most one edge connected to the same two nodes.
366
- * Subsequent edge statements using the same two nodes will identify the edge with the previously defined one and apply any attributes given in the edge statement.
367
- */
368
282
  strict;
369
283
  attributes = Object.freeze({
370
284
  graph: new AttributeList('Graph'),
@@ -381,11 +295,19 @@ class RootGraph extends GraphBase {
381
295
  }
382
296
  }
383
297
  }
298
+ /**
299
+ * DOT object class representing a graph.
300
+ * @group Models
301
+ */
384
302
  class Graph extends RootGraph {
385
303
  get directed() {
386
304
  return false;
387
305
  }
388
306
  }
307
+ /**
308
+ * DOT object class representing a digraph.
309
+ * @group Models
310
+ */
389
311
  class Digraph extends RootGraph {
390
312
  get directed() {
391
313
  return true;
@@ -406,18 +328,42 @@ function builder(directed, strictMode) {
406
328
  return g;
407
329
  };
408
330
  }
409
- /** API for creating directional graph objects. */
331
+ /**
332
+ * API for creating directional graph objects.
333
+ * @group Model Builder
334
+ */
410
335
  const digraph = builder(true, false);
411
- /** API for creating omnidirectional graph objects. */
336
+ /**
337
+ * API for creating omnidirectional graph objects.
338
+ * @group Model Builder
339
+ */
412
340
  const graph = builder(false, false);
413
- /** Provides a strict mode API. */
341
+ /**
342
+ * Provides a strict mode API.
343
+ * @group Model Builder
344
+ */
414
345
  const strict = Object.freeze({
415
- /** API for creating directional graph objects in strict mode. */
346
+ /**
347
+ * API for creating directional graph objects in strict mode.
348
+ * @group Model Builder
349
+ */
416
350
  digraph: builder(true, true),
417
- /** API for creating omnidirectional graph objects in strict mode. */
351
+ /**
352
+ * API for creating omnidirectional graph objects in strict mode.
353
+ * @group Model Builder
354
+ */
418
355
  graph: builder(false, true),
419
356
  });
420
357
 
358
+ /**
359
+ * Convert Model to DOT string.
360
+ *
361
+ * @group Convert Model to DOT
362
+ *
363
+ * @param model Dot Object Model, like {@link Digraph}, {@link Graph}, {@link Node}, and {@link Edge}
364
+ * @param options
365
+ * @returns DOT string
366
+ */
421
367
  function toDot(model, options) {
422
368
  const ast = fromModel(model, options?.convert);
423
369
  return stringify(ast, options?.print);
@@ -432,7 +378,6 @@ export {
432
378
  Edge,
433
379
  Graph,
434
380
  GraphBase,
435
- GraphvizObject,
436
381
  Node,
437
382
  RootGraph,
438
383
  Subgraph,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-graphviz",
3
- "version": "1.0.0-3",
3
+ "version": "1.0.1",
4
4
  "author": "kamiazya <yuki@kamiazya.tech>",
5
5
  "description": "Graphviz library for TypeScript.",
6
6
  "homepage": "https://ts-graphviz.github.io/ts-graphviz/",
@@ -47,7 +47,7 @@
47
47
  "node": ">=14.16"
48
48
  },
49
49
  "scripts": {
50
- "build:peggy": "peggy --plugin ts-pegjs --extra-options-file src/ast/parser/peggy.options.json -o src/ast/parser/index.ts src/ast/parser/dot.peggy",
50
+ "build:peggy": "peggy --plugin ts-pegjs --extra-options-file src/ast/dot-shim/parser/peggy.options.json -o src/ast/dot-shim/parser/_parse.ts src/ast/dot-shim/parser/dot.peggy",
51
51
  "prebuild": "yarn build:peggy",
52
52
  "build": "tsc -p tsconfig.build.json && rollup -c",
53
53
  "postbuild": "prettier --write ./lib/**/index.{js,cjs,d.ts}",
@@ -80,7 +80,7 @@
80
80
  "svgo": "^2.8.0",
81
81
  "ts-jest": "^28.0.7",
82
82
  "ts-pegjs": "^2.1.0",
83
- "typedoc": "^0.22.4",
83
+ "typedoc": "^0.23.15",
84
84
  "typescript": "^4.7.4"
85
85
  }
86
86
  }