This repository was archived by the owner on Dec 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusecase.ts
More file actions
61 lines (61 loc) · 1.96 KB
/
usecase.ts
File metadata and controls
61 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { RootClusterAttributes } from "./types.ts";
import { Digraph, Graph, RootCluster } from "./model/root_clusters.ts";
/**
* Type indicating that it is a constructor of T.
* @hidden
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Type<T> = new (...args: any[]) => T;
interface CreateRootFunction<R extends RootCluster> {
(
id?: string,
attributes?: RootClusterAttributes,
callback?: (g: R) => void,
): R;
(attributes?: RootClusterAttributes, callback?: (g: R) => void): R;
(id?: string, callback?: (g: R) => void): R;
(callback?: (g: R) => void): R;
}
/** @hidden */
function builder<R extends RootCluster>(
cls: Type<R>,
strictMode = false,
): CreateRootFunction<R> {
function createRoot(
id?: string,
attributes?: RootClusterAttributes,
callback?: (g: R) => void,
): R;
function createRoot(
attributes?: RootClusterAttributes,
callback?: (g: R) => void,
): R;
function createRoot(id?: string, callback?: (g: R) => void): R;
function createRoot(callback?: (g: R) => void): R;
function createRoot(...args: unknown[]): R {
const id = args.find((arg): arg is string => typeof arg === "string");
const attributes = args.find((arg): arg is RootClusterAttributes =>
typeof arg === "object"
);
const callback = args.find((arg): arg is (g: R) => void =>
typeof arg === "function"
);
const g = new cls(id, attributes, strictMode);
if (typeof callback === "function") {
callback(g);
}
return g;
}
return createRoot;
}
/** API for creating directional graph objects. */
export const digraph = builder(Digraph);
/** API for creating omnidirectional graph objects. */
export const graph = builder(Graph);
/** Provides a strict mode API. */
export const strict = {
/** API for creating directional graph objects in strict mode. */
digraph: builder(Digraph, true),
/** API for creating omnidirectional graph objects in strict mode. */
graph: builder(Graph, true),
};