ts-graphviz 1.0.1 → 1.2.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.
package/README.ja.md CHANGED
@@ -101,6 +101,8 @@ const dot = toDot(G);
101
101
  <details>
102
102
  <summary>高度な使い方</summary>
103
103
 
104
+ ##### カスタム・クラス 🤖
105
+
104
106
  クラスを継承することで独自の実装を加えることもできます。
105
107
 
106
108
  ```typescript
@@ -114,9 +116,9 @@ class MyCustomDigraph extends Digraph {
114
116
  }
115
117
  }
116
118
  class MyCustomNode extends Node {
117
- constructor(id: number) {
118
- super(`node${id}`, {
119
- [_.label]: `This is Custom Node ${id}`
119
+ constructor(id: string) {
120
+ super(`node_${id}`, {
121
+ [_.label]: `This is Custom Node ${id}`,
120
122
  });
121
123
  }
122
124
  }
@@ -124,39 +126,98 @@ class MyCustomNode extends Node {
124
126
  class MyCustomEdge extends Edge {
125
127
  constructor(targets: EdgeTargetTuple) {
126
128
  super(targets, {
127
- [_.label]: 'This is Custom Edge'
129
+ [_.label]: 'This is Custom Edge',
128
130
  });
129
131
  }
130
132
  }
131
133
 
132
134
  const digraph = new MyCustomDigraph();
133
- const node1 = new MyCustomNode(1);
134
- const node2 = new MyCustomNode(2);
135
+ const node1 = new MyCustomNode('A');
136
+ const node2 = new MyCustomNode('B');
135
137
  const edge = new MyCustomEdge([node1, node2]);
136
138
  digraph.addNode(node1);
137
139
  digraph.addNode(node2);
138
140
  digraph.addEdge(edge);
139
- const dot = toDot(g);
141
+ const dot = toDot(digraph);
140
142
  // digraph "G" {
141
143
  // label = "This is Custom Digraph";
142
- // "node1" [
143
- // label = "This is Custom Node 1",
144
+ // "node_A" [
145
+ // label = "This is Custom Node A";
144
146
  // ];
145
- // "node2" [
146
- // label = "This is Custom Node 2",
147
+ // "node_B" [
148
+ // label = "This is Custom Node B";
147
149
  // ];
148
- // "node1" -> "node2" [
149
- // label = "This is Custom Edge",
150
+ // "node_A" -> "node_B" [
151
+ // label = "This is Custom Edge";
150
152
  // ];
151
153
  // }
152
154
  ```
153
155
 
156
+ ##### Models Context API ( `with` メソッド) 🧅
157
+
158
+ あなたは _Models Context API_ をつかうことで、Graphの内部で生成されるオブジェクトもカスタムクラスにすることができます。
159
+
160
+
161
+ `GraphBaseModel` の実装である `Digraph`, `Graph`, `Subgraph` が持つ `with` メソッドは、 カスタムモデルを事前定義するために提供されています。
162
+
163
+ ```typescript
164
+ const g = new Digraph();
165
+ g.with({
166
+ Node: MyCustomNode,
167
+ Edge: MyCustomEdge,
168
+ });
169
+ const a = g.createNode('A'); // MyCustomNode
170
+ const b = g.createNode('B'); // MyCustomNode
171
+ g.createEdge([a, b]); // MyCustomEdge
172
+ const dot = toDot(g);
173
+ // digraph {
174
+ // "node_A" [
175
+ // label = "This is Custom Node A";
176
+ // ];
177
+ // "node_B" [
178
+ // label = "This is Custom Node B";
179
+ // ];
180
+ // "node_A" -> "node_B" [
181
+ // label = "This is Custom Edge";
182
+ // ];
183
+ // }
184
+ ```
185
+
186
+ ##### `fromDot` 関数 ⏪
187
+
188
+ > この関数のステータスは ![beta](https://img.shields.io/badge/-beta-orange) です。
189
+
190
+ このライブラリを使用するメインシナリオは `toDot` 関数を使用することにありますが、逆方向の変換もサポートしています。
191
+
192
+ **DOT** を **Model** に変換により、コードの一部をDOT言語で記述することができます。
193
+
194
+
195
+ ```typescript
196
+ const G = fromDot(
197
+ `digraph {
198
+ node_A [
199
+ label = "This is a Label of Node A";
200
+ ];
201
+ }`,
202
+ );
203
+
204
+ G.edge(['node_A', 'node_B']);
205
+
206
+ const dot = toDot(G)
207
+ // digraph {
208
+ // "node_A" [
209
+ // label = "This is a Label of Node A";
210
+ // ];
211
+ // "node_A" -> "node_B";
212
+ // }
213
+ ```
214
+
154
215
  </details>
155
216
 
156
217
 
157
218
  #### 宣言的な API 😎
158
219
 
159
- `Graph` や `Digraph` を作成する際に、より **DOT** 言語に近い記法を提供するために _Builder Functuion_ を使うことができます。
220
+ `Graph` や `Digraph` を作成する際に、より **DOT** 言語に近い記法を提供するために _Model Factory_ を使うことができます。
160
221
 
161
222
  **Model** にも宣言的な API を用意しており、一貫して宣言的なパラダイムを選択することもできます。
162
223
 
@@ -219,6 +280,48 @@ const dot = toDot(G);
219
280
  > // }
220
281
  > ```
221
282
 
283
+
284
+ <details>
285
+ <summary>高度な使い方</summary>
286
+
287
+ ##### Models Context API ( `withContext` 関数 ) 💈
288
+
289
+
290
+ `withContext` 関数は、 _Model Factory_ 関数を返します。
291
+
292
+ この _Model Factory_ は、 `Digraph` や `Graph` など、 `RootGraphModel` をカスタムクラスに置き換える手段を提供します。
293
+
294
+ これのAPIにより、宣言的APIとカスタムクラスを統合する手段を提供します。
295
+
296
+ ```typescript
297
+ const { digraph } = withContext({
298
+ Digraph: MyCustomDigraph,
299
+ Node: MyCustomNode,
300
+ Edge: MyCustomEdge,
301
+ });
302
+
303
+ const G = digraph((g) => {
304
+ const a = g.node('A'); // MyCustomNode
305
+ const b = g.node('B'); // MyCustomNode
306
+ g.edge([a, b]); // MyCustomEdge
307
+ });
308
+ const dot = toDot(g);
309
+ // digraph "G" {
310
+ // label = "This is Custom Digraph";
311
+ // "node_A" [
312
+ // label = "This is Custom Node A";
313
+ // ];
314
+ // "node_B" [
315
+ // label = "This is Custom Node B";
316
+ // ];
317
+ // "node_A" -> "node_B" [
318
+ // label = "This is Custom Edge";
319
+ // ];
320
+ // }
321
+ ```
322
+
323
+ </details>
324
+
222
325
  ### `ts-graphviz/ast` モジュール 🔢
223
326
 
224
327
  > このパッケージのステータスは ![beta](https://img.shields.io/badge/-beta-orange) です。
@@ -230,10 +333,11 @@ const dot = toDot(G);
230
333
  状態遷移図で記載している通り、下記の関数を提供しています。
231
334
 
232
335
  - **Model** から **AST** に変換する `fromModel` 関数
336
+ - **AST** から **Model** に変換する `toModel` 関数
233
337
  - **AST** から **DOT** に変換する `stringify` 関数
234
338
  - **DOT** から **AST** に変換する `parse` 関数
235
339
 
236
- > **Note** 上記の図からわかるように、`ts-graphviz` パッケージで提供している `toDot` 関数は、 `fromModel` と `stringify` の合成関数です。
340
+ > **Note** 上記の図からわかるように、`ts-graphviz` パッケージで提供している `toDot` 関数は、 `fromModel` と `stringify` の合成関数です。また、`fromDot` 関数は、 `parse` と `toModel` の合成関数です。
237
341
 
238
342
  詳しい利用方法は整備中です。
239
343
  TypeScriptの型定義を参考にしてください。
package/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/d6485f9858ed4b3e8ef76611a2896bc4)](https://app.codacy.com/gh/ts-graphviz/ts-graphviz?utm_source=github.com&utm_medium=referral&utm_content=ts-graphviz/ts-graphviz&utm_campaign=Badge_Grade_Settings)
1
2
  [![GitHub Action](https://github.com/kamiazya/ts-graphviz/workflows/NodeCI/badge.svg)](https://github.com/kamiazya/ts-graphviz/actions?workflow=NodeCI)
2
3
  [![npm version](https://badge.fury.io/js/ts-graphviz.svg)](https://badge.fury.io/js/ts-graphviz)
3
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
@@ -101,6 +102,8 @@ const dot = toDot(G);
101
102
  <details>
102
103
  <summary>Advanced Usage</summary>
103
104
 
105
+ ##### Custom Class 🤖
106
+
104
107
  You can also add your own implementation by inheriting from the class.
105
108
 
106
109
  ```typescript
@@ -114,9 +117,9 @@ class MyCustomDigraph extends Digraph {
114
117
  }
115
118
  }
116
119
  class MyCustomNode extends Node {
117
- constructor(id: number) {
118
- super(`node${id}`, {
119
- [_.label]: `This is Custom Node ${id}`
120
+ constructor(id: string) {
121
+ super(`node_${id}`, {
122
+ [_.label]: `This is Custom Node ${id}`,
120
123
  });
121
124
  }
122
125
  }
@@ -124,30 +127,88 @@ class MyCustomNode extends Node {
124
127
  class MyCustomEdge extends Edge {
125
128
  constructor(targets: EdgeTargetTuple) {
126
129
  super(targets, {
127
- [_.label]: 'This is Custom Edge'
130
+ [_.label]: 'This is Custom Edge',
128
131
  });
129
132
  }
130
133
  }
131
134
 
132
135
  const digraph = new MyCustomDigraph();
133
- const node1 = new MyCustomNode(1);
134
- const node2 = new MyCustomNode(2);
136
+ const node1 = new MyCustomNode('A');
137
+ const node2 = new MyCustomNode('B');
135
138
  const edge = new MyCustomEdge([node1, node2]);
136
139
  digraph.addNode(node1);
137
140
  digraph.addNode(node2);
138
141
  digraph.addEdge(edge);
139
- const dot = toDot(g);
142
+ const dot = toDot(digraph);
140
143
  // digraph "G" {
141
144
  // label = "This is Custom Digraph";
142
- // "node1" [
143
- // label = "This is Custom Node 1",
145
+ // "node_A" [
146
+ // label = "This is Custom Node A";
147
+ // ];
148
+ // "node_B" [
149
+ // label = "This is Custom Node B";
150
+ // ];
151
+ // "node_A" -> "node_B" [
152
+ // label = "This is Custom Edge";
153
+ // ];
154
+ // }
155
+ ```
156
+
157
+ ##### Models Context API ( `with` method) 🧅
158
+
159
+ You can also use the _Models Context API_ to create custom classes for objects generated inside of Graph.
160
+
161
+
162
+ The `with` methods of `Digraph`, `Graph`, and `Subgraph`, which are implementations of `GraphBaseModel`, are provided to predefine custom models.
163
+
164
+ ```typescript
165
+ const g = new Digraph();
166
+ g.with({
167
+ Node: MyCustomNode,
168
+ Edge: MyCustomEdge,
169
+ });
170
+ const a = g.createNode('A'); // MyCustomNode
171
+ const b = g.createNode('B'); // MyCustomNode
172
+ g.createEdge([a, b]); // MyCustomEdge
173
+ const dot = toDot(g);
174
+ // digraph {
175
+ // "node_A" [
176
+ // label = "This is Custom Node A";
177
+ // ];
178
+ // "node_B" [
179
+ // label = "This is Custom Node B";
144
180
  // ];
145
- // "node2" [
146
- // label = "This is Custom Node 2",
181
+ // "node_A" -> "node_B" [
182
+ // label = "This is Custom Edge";
147
183
  // ];
148
- // "node1" -> "node2" [
149
- // label = "This is Custom Edge",
184
+ // }
185
+ ```
186
+
187
+ ##### `fromDot` function ⏪
188
+
189
+ > The status of this function is ! [beta](https://img.shields.io/badge/-beta-orange).
190
+
191
+ The main scenario for using this library is to use the `toDot` function, but it also supports conversions in the reverse direction.
192
+
193
+ By converting **DOT** to **Model**, a portion of the code can be written in the DOT language.
194
+
195
+ ```typescript
196
+ const G = fromDot(
197
+ `digraph {
198
+ node_A [
199
+ label = "This is a Label of Node A";
200
+ ];
201
+ }`,
202
+ );
203
+
204
+ G.edge(['node_A', 'node_B']);
205
+
206
+ const dot = toDot(G)
207
+ // digraph {
208
+ // "node_A" [
209
+ // label = "This is a Label of Node A";
150
210
  // ];
211
+ // "node_A" -> "node_B";
151
212
  // }
152
213
  ```
153
214
 
@@ -155,7 +216,7 @@ const dot = toDot(g);
155
216
 
156
217
  #### Declarative API 😎
157
218
 
158
- When creating `Graph` or `Digraph`, you can use _Builder Function_ to provide a notation more similar to the **DOT** language.
219
+ When creating `Graph` or `Digraph`, you can use _Model Factory_ to provide a notation more similar to the **DOT** language.
159
220
 
160
221
  **Model** also has a declarative API, so you can consistently choose a declarative paradigm.
161
222
 
@@ -219,6 +280,48 @@ const dot = toDot(G);
219
280
  > // }
220
281
  > ```
221
282
 
283
+
284
+ <details>
285
+ <summary>Advanced Usage</summary>
286
+
287
+ ##### Models Context API ( `withContext` function ) 💈
288
+
289
+
290
+ The `withContext` function returns a _Model Factory_ function.
291
+
292
+ This _Model Factory_ provides a means to replace `RootGraphModel` with a custom class, such as `Digraph` or `Graph`.
293
+
294
+ This API provides a way to integrate declarative APIs and custom classes.
295
+
296
+ ```typescript
297
+ const { digraph } = withContext({
298
+ Digraph: MyCustomDigraph,
299
+ Node: MyCustomNode,
300
+ Edge: MyCustomEdge,
301
+ });
302
+
303
+ const G = digraph((g) => {
304
+ const a = g.node('A'); // MyCustomNode
305
+ const b = g.node('B'); // MyCustomNode
306
+ g.edge([a, b]); // MyCustomEdge
307
+ });
308
+ const dot = toDot(g);
309
+ // digraph "G" {
310
+ // label = "This is Custom Digraph";
311
+ // "node_A" [
312
+ // label = "This is Custom Node A";
313
+ // ];
314
+ // "node_B" [
315
+ // label = "This is Custom Node B";
316
+ // ];
317
+ // "node_A" -> "node_B" [
318
+ // label = "This is Custom Edge";
319
+ // ];
320
+ // }
321
+ ```
322
+
323
+ </details>
324
+
222
325
  ### `ts-graphviz/ast` Module 🔢
223
326
 
224
327
  > This module status is ![beta](https://img.shields.io/badge/-beta-orange).
@@ -230,10 +333,11 @@ An API is provided to handle ASTs for advanced use.
230
333
  The following functions are provided as described in the state transition diagram.
231
334
 
232
335
  - The `fromModel` function converts **Model** to **AST**.
336
+ - The `toModel` function converts **AST** to **Model**.
233
337
  - The `stringify` function converts **AST** to **DOT**.
234
338
  - The `parse` function to convert from **DOT** to **AST**.
235
339
 
236
- > **Note** As you can see from the above figure, the `toDot` function provided by the `ts-graphviz` package is a composite function of `fromModel` and `stringify`.
340
+ > **Note** As you can see from the above figure, the `toDot` function provided by the `ts-graphviz` package is a composite of `fromModel` and `stringify`. The `fromDot` function is a composite of `parse` and `toModel`.
237
341
 
238
342
  Detailed usage is TODO.
239
343
  Please refer to the TypeScript type definition.