22import { attribute } from "../attribute.ts" ;
33import {
44 ClusterSubgraphAttributes ,
5- Compass ,
65 EdgeAttributes ,
7- EdgeTarget ,
8- EdgeTargetLike ,
9- EdgeTargets ,
10- EdgeTargetsLike ,
6+ EdgeTargetLikeTuple ,
7+ EdgeTargetTuple ,
118 ICluster ,
129 IClusterCommonAttributes ,
1310 IEdge ,
@@ -16,13 +13,9 @@ import {
1613 NodeAttributes ,
1714} from "../types.ts" ;
1815import { Attributes , AttributesBase } from "./attributes_base.ts" ;
19- import {
20- isEdgeTarget ,
21- isEdgeTargetLike ,
22- isEdgeTargetsLike ,
23- Node ,
24- } from "./nodes.ts" ;
16+ import { Node } from "./nodes.ts" ;
2517import { Edge } from "./edges.ts" ;
18+ import { isNodeRefGroupLike , toNodeRef , toNodeRefGroup } from "./utils.ts" ;
2619/**
2720 * Base class for clusters.
2821 * @hidden
@@ -165,49 +158,19 @@ export abstract class Cluster<T extends string> extends AttributesBase<T>
165158 }
166159 /** Create Edge and add it to the cluster. */
167160 public createEdge (
168- targets : ( EdgeTargetLike | EdgeTargetsLike ) [ ] ,
161+ targets : EdgeTargetLikeTuple ,
169162 attributes ?: EdgeAttributes ,
170163 ) : IEdge {
171- const edge = new Edge (
172- targets . map ( (
173- t ,
174- ) => ( isEdgeTargetsLike ( t )
175- ? this . toEdgeTargets ( t )
176- : this . toEdgeTarget ( t ) )
177- ) ,
178- attributes ,
179- ) ;
164+ const ts = targets . map ( (
165+ t ,
166+ ) => ( isNodeRefGroupLike ( t )
167+ ? toNodeRefGroup ( t )
168+ : toNodeRef ( t ) )
169+ ) as EdgeTargetTuple ;
170+ const edge = new Edge ( ts , attributes ) ;
180171 this . objects . edges . add ( edge ) ;
181172 return edge ;
182173 }
183- /** @hidden */
184- private toEdgeTarget ( target : EdgeTargetLike ) : EdgeTarget {
185- if ( isEdgeTarget ( target ) ) {
186- return target ;
187- }
188- const [ id , port , compass ] = target . split ( ":" ) ;
189- const n = this . getNode ( id ) ;
190- if ( n !== undefined ) {
191- if ( port && ( compass === undefined || Compass . is ( compass ) ) ) {
192- return n . port ( { port, compass } ) ;
193- }
194- return n ;
195- }
196- if ( Compass . is ( compass ) ) {
197- return { id, port, compass } ;
198- }
199- return { id, port } ;
200- }
201- /** @hidden */
202- private toEdgeTargets ( targets : EdgeTargetsLike ) : EdgeTargets {
203- if (
204- targets . length < 2 &&
205- ( isEdgeTargetLike ( targets [ 0 ] ) && isEdgeTargetLike ( targets [ 1 ] ) ) === false
206- ) {
207- throw Error ( "EdgeTargets must have at least 2 elements." ) ;
208- }
209- return targets . map ( ( t ) => this . toEdgeTarget ( t ) ) ;
210- }
211174 /**
212175 * Create a subgraph by specifying its id (or get it if it already exists).
213176 *
@@ -450,7 +413,7 @@ export abstract class Cluster<T extends string> extends AttributesBase<T>
450413 * @param callback Callbacks for manipulating created or retrieved edge.
451414 */
452415 public edge (
453- targets : EdgeTargetLike [ ] ,
416+ targets : EdgeTargetLikeTuple ,
454417 callback ?: ( edge : IEdge ) => void ,
455418 ) : IEdge ;
456419 /**
@@ -481,7 +444,7 @@ export abstract class Cluster<T extends string> extends AttributesBase<T>
481444 * @param callback Callbacks for manipulating created or retrieved edge.
482445 */
483446 public edge (
484- targets : EdgeTargetLike [ ] ,
447+ targets : EdgeTargetLikeTuple ,
485448 attributes : EdgeAttributes ,
486449 callback ?: ( edge : IEdge ) => void ,
487450 ) : IEdge ;
@@ -510,11 +473,11 @@ export abstract class Cluster<T extends string> extends AttributesBase<T>
510473 */
511474 public edge ( attributes : EdgeAttributes ) : void ;
512475 public edge (
513- firstArg : EdgeTargetLike [ ] | EdgeAttributes ,
476+ firstArg : EdgeTargetLikeTuple | EdgeAttributes ,
514477 ...args : unknown [ ]
515478 ) : IEdge | void {
516479 if ( Array . isArray ( firstArg ) ) {
517- const targets = [ ... firstArg ] ;
480+ const targets = firstArg ;
518481 const attributes = args . find ( ( arg : unknown ) : arg is EdgeAttributes =>
519482 typeof arg === "object"
520483 ) ;
@@ -574,7 +537,7 @@ export class Subgraph
574537 super ( ) ;
575538 this . id = args . find ( ( arg ) : arg is string => typeof arg === "string" ) ;
576539 const attributes = args . find ( ( arg ) : arg is ClusterSubgraphAttributes =>
577- typeof arg === "object"
540+ typeof arg === "object" && arg !== null
578541 ) ;
579542 if ( attributes !== undefined ) {
580543 this . apply ( attributes ) ;
0 commit comments