ts-graphviz 1.8.2 → 2.0.0

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +307 -0
  2. package/README.md +40 -13
  3. package/lib/adapter.cjs +1 -0
  4. package/lib/adapter.d.ts +1 -0
  5. package/lib/adapter.js +1 -0
  6. package/lib/ast.cjs +1 -0
  7. package/lib/ast.d.ts +1 -0
  8. package/lib/ast.js +1 -0
  9. package/lib/attribute.d.ts +2 -0
  10. package/lib/from-dot.d.ts +10 -0
  11. package/lib/model-factory-builder.d.ts +7 -0
  12. package/lib/model-factory.d.ts +6 -0
  13. package/lib/to-dot.d.ts +7 -0
  14. package/lib/ts-graphviz.cjs +1 -0
  15. package/lib/ts-graphviz.d.ts +7 -0
  16. package/lib/ts-graphviz.js +55 -0
  17. package/lib/types.d.ts +14 -0
  18. package/package.json +53 -96
  19. package/SECURITY.md +0 -5
  20. package/lib/adapter/browser/index.cjs +0 -12
  21. package/lib/adapter/browser/index.d.ts +0 -15
  22. package/lib/adapter/browser/index.js +0 -9
  23. package/lib/adapter/deno/mod.d.ts +0 -13
  24. package/lib/adapter/deno/mod.js +0 -36
  25. package/lib/adapter/node/index.cjs +0 -45
  26. package/lib/adapter/node/index.d.ts +0 -15
  27. package/lib/adapter/node/index.js +0 -42
  28. package/lib/adapter/types/index.cjs +0 -1
  29. package/lib/adapter/types/index.d.ts +0 -134
  30. package/lib/adapter/types/index.js +0 -0
  31. package/lib/adapter/utils/index.cjs +0 -58
  32. package/lib/adapter/utils/index.d.ts +0 -11
  33. package/lib/adapter/utils/index.js +0 -56
  34. package/lib/ast/index.cjs +0 -5775
  35. package/lib/ast/index.d.ts +0 -946
  36. package/lib/ast/index.js +0 -5770
  37. package/lib/common/index.cjs +0 -58
  38. package/lib/common/index.d.ts +0 -4981
  39. package/lib/common/index.js +0 -58
  40. package/lib/core/index.cjs +0 -359
  41. package/lib/core/index.d.ts +0 -347
  42. package/lib/core/index.js +0 -364
  43. package/lib/index.cjs +0 -23
  44. package/lib/index.d.ts +0 -3
  45. package/lib/index.js +0 -2
  46. package/lib/utils/index.cjs +0 -41
  47. package/lib/utils/index.d.ts +0 -34
  48. package/lib/utils/index.js +0 -37
@@ -1,347 +0,0 @@
1
- import {
2
- Attribute,
3
- AttributeKey,
4
- Attributes,
5
- AttributesObject,
6
- AttributesEntities,
7
- AttributeListKind,
8
- AttributeListModel,
9
- AttributesGroupModel,
10
- GraphBaseModel,
11
- GraphCommonAttributes,
12
- NodeModel,
13
- EdgeModel,
14
- SubgraphModel,
15
- ModelsContext,
16
- SubgraphAttributesObject,
17
- NodeAttributesObject,
18
- EdgeTargetLikeTuple,
19
- EdgeAttributesObject,
20
- GraphAttributeKey,
21
- RootGraphModel,
22
- GraphAttributesObject,
23
- EdgeTargetTuple,
24
- EdgeAttributeKey,
25
- NodeAttributeKey,
26
- Port,
27
- ForwardRefNode,
28
- SubgraphAttributeKey,
29
- ClusterSubgraphAttributeKey,
30
- DotObjectModel,
31
- } from '../common/index.js';
32
- import { ConvertFromModelOptions, PrintOptions, ParseOptions, ConvertToModelOptions } from '../ast/index.js';
33
-
34
- /**
35
- * @group Attribute
36
- *
37
- * @deprecated Use {@link Attribute.keys} instead.
38
- */
39
- type AttributeKeyDict = Attribute.keys;
40
- /**
41
- * @group Attribute
42
- */
43
- declare const attribute: Attribute.keys;
44
-
45
- /**
46
- * Base class for DOT objects.
47
- * @group Models
48
- */
49
- declare abstract class DotObject {}
50
-
51
- /**
52
- * Base class for DOT objects with attributes.
53
- * @group Models
54
- */
55
- declare abstract class AttributesBase<T extends AttributeKey> extends DotObject implements Attributes<T> {
56
- #private;
57
- constructor(attributes?: AttributesObject<T>);
58
- get values(): ReadonlyArray<[T, Attribute<T>]>;
59
- get size(): number;
60
- get<K extends T>(key: K): Attribute<K> | undefined;
61
- set<K extends T>(key: K, value: Attribute<K>): void;
62
- delete(key: T): void;
63
- apply(attributes: AttributesObject<T> | AttributesEntities<T>): void;
64
- clear(): void;
65
- }
66
-
67
- /**
68
- * A set of attribute values for any object.
69
- * @group Models
70
- */
71
- declare class AttributeList<K extends AttributeListKind, T extends AttributeKey = AttributeKey>
72
- extends AttributesBase<T>
73
- implements AttributeListModel<K, T>
74
- {
75
- readonly $$kind: K;
76
- get $$type(): 'AttributeList';
77
- comment?: string;
78
- constructor($$kind: K, attributes?: AttributesObject<T>);
79
- }
80
-
81
- /**
82
- * A set of attribute values for any object.
83
- * @group Models
84
- */
85
- declare class AttributesGroup<T extends AttributeKey = AttributeKey>
86
- extends AttributesBase<T>
87
- implements AttributesGroupModel<T>
88
- {
89
- comment?: string;
90
- }
91
-
92
- /**
93
- * Base class for Graph objects.
94
- * @group Models
95
- */
96
- declare abstract class GraphBase<T extends AttributeKey> extends AttributesBase<T> implements GraphBaseModel<T> {
97
- #private;
98
- readonly id?: string;
99
- comment?: string;
100
- readonly attributes: Readonly<GraphCommonAttributes>;
101
- get nodes(): ReadonlyArray<NodeModel>;
102
- get edges(): ReadonlyArray<EdgeModel>;
103
- get subgraphs(): ReadonlyArray<SubgraphModel>;
104
- with(models: Partial<ModelsContext>): void;
105
- addNode(node: NodeModel): void;
106
- addEdge(edge: EdgeModel): void;
107
- addSubgraph(subgraph: SubgraphModel): void;
108
- existNode(nodeId: string): boolean;
109
- existEdge(edge: EdgeModel): boolean;
110
- existSubgraph(subgraph: SubgraphModel): boolean;
111
- createSubgraph(id?: string, attributes?: SubgraphAttributesObject): SubgraphModel;
112
- createSubgraph(attributes?: SubgraphAttributesObject): SubgraphModel;
113
- removeNode(node: NodeModel | string): void;
114
- removeEdge(edge: EdgeModel): void;
115
- removeSubgraph(subgraph: SubgraphModel): void;
116
- createNode(id: string, attributes?: NodeAttributesObject): NodeModel;
117
- getSubgraph(id: string): SubgraphModel | undefined;
118
- getNode(id: string): NodeModel | undefined;
119
- createEdge(targets: EdgeTargetLikeTuple, attributes?: EdgeAttributesObject): EdgeModel;
120
- subgraph(id: string, callback?: (subgraph: SubgraphModel) => void): SubgraphModel;
121
- subgraph(
122
- id: string,
123
- attributes: SubgraphAttributesObject,
124
- callback?: (subgraph: SubgraphModel) => void,
125
- ): SubgraphModel;
126
- subgraph(attributes: SubgraphAttributesObject, callback?: (subgraph: SubgraphModel) => void): SubgraphModel;
127
- subgraph(callback?: (subgraph: SubgraphModel) => void): SubgraphModel;
128
- node(id: string, callback?: (node: NodeModel) => void): NodeModel;
129
- node(id: string, attributes: NodeAttributesObject, callback?: (node: NodeModel) => void): NodeModel;
130
- node(attributes: NodeAttributesObject): void;
131
- edge(targets: EdgeTargetLikeTuple, callback?: (edge: EdgeModel) => void): EdgeModel;
132
- edge(targets: EdgeTargetLikeTuple, attributes: EdgeAttributesObject, callback?: (edge: EdgeModel) => void): EdgeModel;
133
- edge(attributes: EdgeAttributesObject): void;
134
- graph(attributes: SubgraphAttributesObject): void;
135
- }
136
-
137
- /**
138
- * Base class representing a root graph(digraph, graph).
139
- * @group Models
140
- */
141
- declare abstract class RootGraph extends GraphBase<GraphAttributeKey> implements RootGraphModel {
142
- get $$type(): 'Graph';
143
- readonly id?: string;
144
- abstract readonly directed: boolean;
145
- strict: boolean;
146
- constructor(id?: string, attributes?: GraphAttributesObject);
147
- constructor(id?: string, strict?: boolean, attributes?: GraphAttributesObject);
148
- constructor(strict?: boolean, attributes?: GraphAttributesObject);
149
- constructor(attributes?: GraphAttributesObject);
150
- }
151
-
152
- /**
153
- * DOT object class representing a digraph.
154
- * @group Models
155
- */
156
- declare class Digraph extends RootGraph {
157
- get directed(): boolean;
158
- }
159
-
160
- /**
161
- * DOT object class representing a edge.
162
- * @group Models
163
- */
164
- declare class Edge extends DotObject implements EdgeModel {
165
- readonly targets: EdgeTargetTuple;
166
- get $$type(): 'Edge';
167
- comment?: string;
168
- readonly attributes: AttributesGroupModel<EdgeAttributeKey>;
169
- constructor(targets: EdgeTargetTuple, attributes?: EdgeAttributesObject);
170
- }
171
-
172
- /**
173
- * DOT object class representing a graph.
174
- * @group Models
175
- */
176
- declare class Graph extends RootGraph {
177
- get directed(): boolean;
178
- }
179
-
180
- /**
181
- * DOT object class representing a node.
182
- * @group Models
183
- */
184
- declare class Node extends DotObject implements NodeModel {
185
- readonly id: string;
186
- get $$type(): 'Node';
187
- comment?: string;
188
- readonly attributes: AttributesGroup<NodeAttributeKey>;
189
- constructor(id: string, attributes?: NodeAttributesObject);
190
- port(port: string | Partial<Port>): ForwardRefNode;
191
- }
192
-
193
- /**
194
- * DOT object class representing a subgraph.
195
- * @group Models
196
- */
197
- declare class Subgraph extends GraphBase<SubgraphAttributeKey | ClusterSubgraphAttributeKey> implements SubgraphModel {
198
- get $$type(): 'Subgraph';
199
- readonly id?: string;
200
- constructor(id?: string, attributes?: SubgraphAttributesObject);
201
- constructor(attributes?: SubgraphAttributesObject);
202
- isSubgraphCluster(): boolean;
203
- }
204
-
205
- /**
206
- * ModelFactory is an interface that provides a way to create a {@link RootGraphModel} object.
207
- *
208
- * @param id - Optional string parameter that specifies the id of the {@link RootGraphModel} object.
209
- * @param attributes - Optional GraphAttributesObject parameter that specifies the attributes of the {@link RootGraphModel} object.
210
- * @param callback - Optional callback function that takes a {@link RootGraphModel} object as a parameter.
211
- *
212
- * @returns {@link RootGraphModel} - Returns a {@link RootGraphModel} object.
213
- * @group Model Factory
214
- */
215
- interface ModelFactory {
216
- (id?: string, attributes?: GraphAttributesObject, callback?: (g: RootGraphModel) => void): RootGraphModel;
217
- (attributes?: GraphAttributesObject, callback?: (g: RootGraphModel) => void): RootGraphModel;
218
- (id?: string, callback?: (g: RootGraphModel) => void): RootGraphModel;
219
- (callback?: (g: RootGraphModel) => void): RootGraphModel;
220
- }
221
- /**
222
- * @group Model Factory
223
- */
224
- interface ModelFactories {
225
- /**
226
- * API for creating directional graph objects.
227
- */
228
- digraph: ModelFactory;
229
- /**
230
- * API for creating omnidirectional graph objects.
231
- */
232
- graph: ModelFactory;
233
- }
234
- /**
235
- * @group Model Factory
236
- */
237
- interface ModelFactoriesWithStrict extends ModelFactories {
238
- strict: ModelFactories;
239
- }
240
-
241
- /**
242
- * digraph is a factory for creating Digraph objects.
243
- * @group Model Factory
244
- */
245
- declare const digraph: ModelFactory;
246
- /**
247
- * graph is a factory for creating Graph objects.
248
- * @group Model Factory
249
- */
250
- declare const graph: ModelFactory;
251
- /**
252
- * Provides a strict mode API.
253
- * @group Model Factory
254
- */
255
- declare const strict: ModelFactories;
256
- /**
257
- * withContext creates a {@link ModelFactoriesWithStrict} object with the given context.
258
- *
259
- * @param models - An object containing the models to be used in the context.
260
- *
261
- * @returns A ModelFactoriesWithStrict object containing the factories. * @group Model Factory
262
- */
263
- declare function withContext(models: Partial<ModelsContext>): ModelFactoriesWithStrict;
264
-
265
- /**
266
- * This interface provides options for converting a model to DOT.
267
- * @group Convert Model to DOT
268
- * @alpha
269
- */
270
- interface ToDotOptions {
271
- /**
272
- * Options for converting the model to DOT.
273
- */
274
- convert?: ConvertFromModelOptions;
275
- /**
276
- * Options for printing DOT.
277
- */
278
- print?: PrintOptions;
279
- }
280
- /**
281
- * Convert Model to DOT string.
282
- *
283
- * @group Convert Model to DOT
284
- *
285
- * @param model Dot Object Model, like {@link Digraph}, {@link Graph}, {@link Subgraph}, {@link Node}, and {@link Edge}
286
- * @param options Optional options for the conversion.
287
- * @returns DOT string
288
- */
289
- declare function toDot(model: DotObjectModel, options?: ToDotOptions): string;
290
-
291
- /**
292
- * This interface provides options for converting DOT to a model.
293
- * @group Convert DOT to Model
294
- * @alpha
295
- */
296
- interface FromDotOptions<T extends 'Dot' | 'Graph' | 'Node' | 'Edge' | 'Subgraph'> {
297
- /**
298
- * Options for parsing DOT.
299
- */
300
- parse?: ParseOptions<T>;
301
- /**
302
- * Options for converting the parsed DOT to a model.
303
- */
304
- convert?: ConvertToModelOptions;
305
- }
306
- /**
307
- * fromDot is a function that converts a DOT string to a model.
308
- *
309
- * @group Convert DOT to Model
310
- *
311
- * @param dot The DOT string to convert.
312
- * @param options Options for converting the DOT string to a model.
313
- * @returns A model of type {@link RootGraphModel}, {@link NodeModel}, {@link EdgeModel}, or {@link SubgraphModel},
314
- * depending on the type specified in the options.
315
- * @beta
316
- */
317
- declare function fromDot(dot: string, options?: FromDotOptions<'Dot' | 'Graph'>): RootGraphModel;
318
- declare function fromDot(dot: string, options?: FromDotOptions<'Node'>): NodeModel;
319
- declare function fromDot(dot: string, options?: FromDotOptions<'Edge'>): EdgeModel;
320
- declare function fromDot(dot: string, options?: FromDotOptions<'Subgraph'>): SubgraphModel;
321
-
322
- export {
323
- AttributeKeyDict,
324
- AttributeList,
325
- AttributesBase,
326
- AttributesGroup,
327
- Digraph,
328
- DotObject,
329
- Edge,
330
- FromDotOptions,
331
- Graph,
332
- GraphBase,
333
- ModelFactories,
334
- ModelFactoriesWithStrict,
335
- ModelFactory,
336
- Node,
337
- RootGraph,
338
- Subgraph,
339
- ToDotOptions,
340
- attribute,
341
- digraph,
342
- fromDot,
343
- graph,
344
- strict,
345
- toDot,
346
- withContext,
347
- };
package/lib/core/index.js DELETED
@@ -1,364 +0,0 @@
1
- import {
2
- RootModelsContext,
3
- createModelsContext,
4
- isNodeRefGroupLike,
5
- toNodeRefGroup,
6
- toNodeRef,
7
- isNodeRefLike,
8
- } from '../common/index.js';
9
- import { fromModel, stringify, parse, toModel } from '../ast/index.js';
10
-
11
- const attribute = new Proxy(Object.freeze({}), {
12
- get: (_, key) => key,
13
- });
14
-
15
- class DotObject {}
16
-
17
- class AttributesBase extends DotObject {
18
- #attrs = new Map();
19
- constructor(attributes) {
20
- super();
21
- if (attributes !== undefined) {
22
- this.apply(attributes);
23
- }
24
- }
25
- get values() {
26
- return Array.from(this.#attrs.entries());
27
- }
28
- get size() {
29
- return this.#attrs.size;
30
- }
31
- get(key) {
32
- return this.#attrs.get(key);
33
- }
34
- set(key, value) {
35
- if (value !== null && value !== undefined) {
36
- this.#attrs.set(key, value);
37
- }
38
- }
39
- delete(key) {
40
- this.#attrs.delete(key);
41
- }
42
- apply(attributes) {
43
- const entries = Array.isArray(attributes) ? attributes : Object.entries(attributes);
44
- for (const [key, value] of entries) {
45
- this.set(key, value);
46
- }
47
- }
48
- clear() {
49
- this.#attrs.clear();
50
- }
51
- }
52
-
53
- class AttributeList extends AttributesBase {
54
- $$kind;
55
- get $$type() {
56
- return 'AttributeList';
57
- }
58
- comment;
59
- constructor($$kind, attributes) {
60
- super(attributes);
61
- this.$$kind = $$kind;
62
- }
63
- }
64
-
65
- class GraphBase extends AttributesBase {
66
- #models = RootModelsContext;
67
- id;
68
- comment;
69
- attributes = Object.freeze({
70
- graph: new AttributeList('Graph'),
71
- edge: new AttributeList('Edge'),
72
- node: new AttributeList('Node'),
73
- });
74
- get nodes() {
75
- return Array.from(this.#objects.nodes.values());
76
- }
77
- get edges() {
78
- return Array.from(this.#objects.edges.values());
79
- }
80
- get subgraphs() {
81
- return Array.from(this.#objects.subgraphs.values());
82
- }
83
- #objects = {
84
- nodes: new Map(),
85
- edges: new Set(),
86
- subgraphs: new Set(),
87
- };
88
- with(models) {
89
- this.#models = createModelsContext(models);
90
- }
91
- addNode(node) {
92
- this.#objects.nodes.set(node.id, node);
93
- }
94
- addEdge(edge) {
95
- this.#objects.edges.add(edge);
96
- }
97
- addSubgraph(subgraph) {
98
- this.#objects.subgraphs.add(subgraph);
99
- }
100
- existNode(nodeId) {
101
- return this.#objects.nodes.has(nodeId);
102
- }
103
- existEdge(edge) {
104
- return this.#objects.edges.has(edge);
105
- }
106
- existSubgraph(subgraph) {
107
- return this.#objects.subgraphs.has(subgraph);
108
- }
109
- createSubgraph(...args) {
110
- const subgraph = new this.#models.Subgraph(...args);
111
- subgraph.with(this.#models);
112
- this.addSubgraph(subgraph);
113
- return subgraph;
114
- }
115
- removeNode(node) {
116
- this.#objects.nodes.delete(typeof node === 'string' ? node : node.id);
117
- }
118
- removeEdge(edge) {
119
- this.#objects.edges.delete(edge);
120
- }
121
- removeSubgraph(subgraph) {
122
- this.#objects.subgraphs.delete(subgraph);
123
- }
124
- createNode(id, attributes) {
125
- const node = new this.#models.Node(id, attributes);
126
- this.addNode(node);
127
- return node;
128
- }
129
- getSubgraph(id) {
130
- return Array.from(this.#objects.subgraphs.values()).find((subgraph) => subgraph.id === id);
131
- }
132
- getNode(id) {
133
- return this.#objects.nodes.get(id);
134
- }
135
- createEdge(targets, attributes) {
136
- const ts = targets.map((t) => (isNodeRefGroupLike(t) ? toNodeRefGroup(t) : toNodeRef(t)));
137
- const edge = new this.#models.Edge(ts, attributes);
138
- this.addEdge(edge);
139
- return edge;
140
- }
141
- subgraph(...args) {
142
- const id = args.find((arg) => typeof arg === 'string');
143
- const attributes = args.find((arg) => typeof arg === 'object' && arg !== null);
144
- const callback = args.find((arg) => typeof arg === 'function');
145
- const subgraph = id ? this.getSubgraph(id) ?? this.createSubgraph(id) : this.createSubgraph();
146
- if (attributes !== undefined) {
147
- subgraph.apply(attributes);
148
- }
149
- if (callback !== undefined) {
150
- callback(subgraph);
151
- }
152
- return subgraph;
153
- }
154
- node(firstArg, ...args) {
155
- if (typeof firstArg === 'string') {
156
- const id = firstArg;
157
- const attributes = args.find((arg) => typeof arg === 'object' && arg !== null);
158
- const callback = args.find((arg) => typeof arg === 'function');
159
- const node = this.getNode(id) ?? this.createNode(id);
160
- if (attributes !== undefined) {
161
- node.attributes.apply(attributes);
162
- }
163
- if (callback !== undefined) {
164
- callback(node);
165
- }
166
- return node;
167
- }
168
- if (typeof firstArg === 'object' && firstArg !== null) {
169
- this.attributes.node.apply(firstArg);
170
- }
171
- }
172
- edge(firstArg, ...args) {
173
- if (Array.isArray(firstArg)) {
174
- const targets = firstArg;
175
- const attributes = args.find((arg) => typeof arg === 'object');
176
- const callback = args.find((arg) => typeof arg === 'function');
177
- const edge = this.createEdge(targets, attributes);
178
- if (callback !== undefined) {
179
- callback(edge);
180
- }
181
- return edge;
182
- }
183
- if (typeof firstArg === 'object' && firstArg !== null) {
184
- this.attributes.edge.apply(firstArg);
185
- }
186
- }
187
- graph(attributes) {
188
- this.attributes.graph.apply(attributes);
189
- }
190
- }
191
-
192
- class RootGraph extends GraphBase {
193
- get $$type() {
194
- return 'Graph';
195
- }
196
- id;
197
- strict;
198
- constructor(...args) {
199
- super();
200
- this.id = args.find((arg) => typeof arg === 'string');
201
- this.strict = args.find((arg) => typeof arg === 'boolean') ?? false;
202
- const attributes = args.find((arg) => typeof arg === 'object' && arg !== null);
203
- if (attributes !== undefined) {
204
- this.apply(attributes);
205
- }
206
- }
207
- }
208
-
209
- class Digraph extends RootGraph {
210
- get directed() {
211
- return true;
212
- }
213
- }
214
-
215
- class Graph extends RootGraph {
216
- get directed() {
217
- return false;
218
- }
219
- }
220
-
221
- class Subgraph extends GraphBase {
222
- get $$type() {
223
- return 'Subgraph';
224
- }
225
- id;
226
- constructor(...args) {
227
- super();
228
- this.id = args.find((arg) => typeof arg === 'string');
229
- const attributes = args.find((arg) => typeof arg === 'object' && arg !== null);
230
- if (attributes !== undefined) {
231
- this.apply(attributes);
232
- }
233
- }
234
- isSubgraphCluster() {
235
- if (typeof this.id === 'string') {
236
- return this.id.startsWith('cluster');
237
- }
238
- return false;
239
- }
240
- }
241
-
242
- class AttributesGroup extends AttributesBase {
243
- comment;
244
- }
245
-
246
- class Node extends DotObject {
247
- id;
248
- get $$type() {
249
- return 'Node';
250
- }
251
- comment;
252
- attributes;
253
- constructor(id, attributes) {
254
- super();
255
- this.id = id;
256
- this.attributes = new AttributesGroup(attributes);
257
- }
258
- port(port) {
259
- if (typeof port === 'string') {
260
- return { id: this.id, port };
261
- }
262
- return { id: this.id, ...port };
263
- }
264
- }
265
-
266
- class Edge extends DotObject {
267
- targets;
268
- get $$type() {
269
- return 'Edge';
270
- }
271
- comment;
272
- attributes;
273
- constructor(targets, attributes) {
274
- super();
275
- this.targets = targets;
276
- if (targets.length < 2 && (isNodeRefLike(targets[0]) && isNodeRefLike(targets[1])) === false) {
277
- throw Error('The element of Edge target is missing or not satisfied as Edge target.');
278
- }
279
- this.attributes = new AttributesGroup(attributes);
280
- }
281
- }
282
-
283
- Object.assign(RootModelsContext, {
284
- Graph,
285
- Digraph,
286
- Subgraph,
287
- Node,
288
- Edge,
289
- });
290
-
291
- function ModelFactoryBuilder(directed, strictMode) {
292
- return (...args) => {
293
- const G = directed ? this.Digraph : this.Graph;
294
- const id = args.find((arg) => typeof arg === 'string');
295
- const attributes = args.find((arg) => typeof arg === 'object');
296
- const callback = args.find((arg) => typeof arg === 'function');
297
- const g = new G(id, strictMode, attributes);
298
- g.with(this);
299
- if (typeof callback === 'function') {
300
- callback(g);
301
- }
302
- return g;
303
- };
304
- }
305
- function createModelFactories(strict, context = RootModelsContext) {
306
- return Object.freeze({
307
- digraph: ModelFactoryBuilder.call(context, true, strict),
308
- graph: ModelFactoryBuilder.call(context, false, strict),
309
- });
310
- }
311
-
312
- const noStrict = createModelFactories(false);
313
- const digraph = noStrict.digraph;
314
- const graph = noStrict.graph;
315
- const strict = createModelFactories(true);
316
- function withContext(models) {
317
- const context = createModelsContext(models);
318
- return Object.freeze({
319
- ...createModelFactories(false, context),
320
- strict: createModelFactories(true, context),
321
- });
322
- }
323
-
324
- function toDot(model, options) {
325
- const ast = fromModel(model, options?.convert);
326
- return stringify(ast, options?.print);
327
- }
328
-
329
- function fromDot(dot, options) {
330
- const ast = parse(dot, options?.parse);
331
- if (
332
- Array.isArray(ast) ||
333
- ast.type === 'Attribute' ||
334
- ast.type === 'AttributeList' ||
335
- ast.type === 'Comment' ||
336
- ast.type === 'NodeRef' ||
337
- ast.type === 'NodeRefGroup' ||
338
- ast.type === 'Literal'
339
- ) {
340
- throw new Error();
341
- }
342
- return toModel(ast, options?.convert);
343
- }
344
-
345
- export {
346
- AttributeList,
347
- AttributesBase,
348
- AttributesGroup,
349
- Digraph,
350
- DotObject,
351
- Edge,
352
- Graph,
353
- GraphBase,
354
- Node,
355
- RootGraph,
356
- Subgraph,
357
- attribute,
358
- digraph,
359
- fromDot,
360
- graph,
361
- strict,
362
- toDot,
363
- withContext,
364
- };