@dxos/graph 0.7.5-main.499c70c
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/LICENSE +8 -0
- package/README.md +3 -0
- package/dist/lib/browser/index.mjs +212 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +233 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +214 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/buidler.d.ts +16 -0
- package/dist/types/src/buidler.d.ts.map +1 -0
- package/dist/types/src/graph.d.ts +30 -0
- package/dist/types/src/graph.d.ts.map +1 -0
- package/dist/types/src/graph.test.d.ts +2 -0
- package/dist/types/src/graph.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +41 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +60 -0
- package/src/buidler.ts +62 -0
- package/src/graph.test.ts +56 -0
- package/src/graph.ts +127 -0
- package/src/index.ts +7 -0
- package/src/types.ts +42 -0
- package/src/util.ts +17 -0
package/src/graph.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { create } from '@dxos/live-object';
|
|
6
|
+
|
|
7
|
+
import { Graph, type GraphNode, type GraphEdge } from './types';
|
|
8
|
+
import { removeElements } from './util';
|
|
9
|
+
|
|
10
|
+
// TODO(burdon): Traversal/visitor.
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Typed reactive object graph.
|
|
14
|
+
*/
|
|
15
|
+
export class ReadonlyGraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> {
|
|
16
|
+
protected readonly _graph: Graph;
|
|
17
|
+
|
|
18
|
+
constructor({ nodes = [], edges = [] }: Partial<Graph> = {}) {
|
|
19
|
+
this._graph = create(Graph, { nodes, edges });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
toJSON() {
|
|
23
|
+
// Sort to enable stable comparisons.
|
|
24
|
+
this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
25
|
+
this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
26
|
+
return JSON.parse(JSON.stringify(this._graph));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get graph(): Graph {
|
|
30
|
+
return this._graph;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get nodes(): Node[] {
|
|
34
|
+
return this._graph.nodes as Node[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get edges(): Edge[] {
|
|
38
|
+
return this._graph.edges as Edge[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getNode(id: string): Node | undefined {
|
|
42
|
+
return this.nodes.find((node) => node.id === id);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getNodes({ type }: Partial<Node>): Node[] {
|
|
46
|
+
return this.nodes.filter((node) => !type || type === node.type);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
getEdge(id: string): Edge | undefined {
|
|
50
|
+
return this.edges.find((edge) => edge.id === id);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getEdges({ type, source, target }: Partial<Edge>): Edge[] {
|
|
54
|
+
return this.edges.filter(
|
|
55
|
+
(edge) =>
|
|
56
|
+
(!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class GraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> extends ReadonlyGraphModel<
|
|
62
|
+
Node,
|
|
63
|
+
Edge
|
|
64
|
+
> {
|
|
65
|
+
clear(): this {
|
|
66
|
+
this._graph.nodes.length = 0;
|
|
67
|
+
this._graph.edges.length = 0;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
addGraph(graph: GraphModel<Node, Edge>): this {
|
|
72
|
+
this.addNodes(graph.nodes);
|
|
73
|
+
this.addEdges(graph.edges);
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
addGraphs(graphs: GraphModel<Node, Edge>[]): this {
|
|
78
|
+
graphs.forEach((graph) => {
|
|
79
|
+
this.addNodes(graph.nodes);
|
|
80
|
+
this.addEdges(graph.edges);
|
|
81
|
+
});
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
addNode(node: Node): this {
|
|
86
|
+
this._graph.nodes.push(node);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
addNodes(nodes: Node[]): this {
|
|
91
|
+
nodes.forEach((node) => this.addNode(node));
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
addEdge(edge: Edge): this {
|
|
96
|
+
this._graph.edges.push(edge);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
addEdges(edges: Edge[]): this {
|
|
101
|
+
edges.forEach((edge) => this.addEdge(edge));
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// TODO(burdon): Return graph.
|
|
106
|
+
|
|
107
|
+
removeNode(id: string): GraphModel<Node, Edge> {
|
|
108
|
+
const nodes = removeElements(this._graph.nodes, (node) => node.id === id);
|
|
109
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);
|
|
110
|
+
return new GraphModel<Node, Edge>({ nodes, edges });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
removeNodes(ids: string[]): GraphModel<Node, Edge> {
|
|
114
|
+
const graphs = ids.map((id) => this.removeNode(id));
|
|
115
|
+
return new GraphModel<Node, Edge>().addGraphs(graphs);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
removeEdge(id: string): GraphModel<Node, Edge> {
|
|
119
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.id === id);
|
|
120
|
+
return new GraphModel<Node, Edge>({ edges });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
removeEdges(ids: string[]): GraphModel<Node, Edge> {
|
|
124
|
+
const graphs = ids.map((id) => this.removeEdge(id));
|
|
125
|
+
return new GraphModel<Node, Edge>().addGraphs(graphs);
|
|
126
|
+
}
|
|
127
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { S } from '@dxos/echo-schema';
|
|
6
|
+
|
|
7
|
+
// Prior art:
|
|
8
|
+
// - https://graphology.github.io (TS, tree-shakable, multiple packages for extensions)
|
|
9
|
+
// - https://github.com/dagrejs/graphlib (mature, extensive)
|
|
10
|
+
// - https://github.com/avoidwork/tiny-graph
|
|
11
|
+
|
|
12
|
+
export const BaseGraphNode = S.Struct({
|
|
13
|
+
id: S.String,
|
|
14
|
+
type: S.optional(S.String),
|
|
15
|
+
data: S.optional(S.Any),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;
|
|
19
|
+
export type GraphNode<Data extends object | void = void> = BaseGraphNode & { data: Data };
|
|
20
|
+
|
|
21
|
+
export const BaseGraphEdge = S.Struct({
|
|
22
|
+
id: S.String,
|
|
23
|
+
type: S.optional(S.String),
|
|
24
|
+
source: S.String,
|
|
25
|
+
target: S.String,
|
|
26
|
+
data: S.optional(S.Any),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;
|
|
30
|
+
export type GraphEdge<Data extends object | void = void> = BaseGraphEdge & { data: Data };
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Generic graph abstraction.
|
|
34
|
+
*/
|
|
35
|
+
export const Graph = S.Struct({
|
|
36
|
+
nodes: S.mutable(S.Array(BaseGraphNode)),
|
|
37
|
+
edges: S.mutable(S.Array(BaseGraphEdge)),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export type Graph = S.Schema.Type<typeof Graph>;
|
|
41
|
+
|
|
42
|
+
export const emptyGraph: Graph = { nodes: [], edges: [] };
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// TODO(burdon): GraphBuilder.
|
|
6
|
+
|
|
7
|
+
// TODO(burdon): Move to @dxos/live-object?
|
|
8
|
+
export const removeElements = <T>(array: T[], predicate: (element: T, index: number) => boolean): T[] => {
|
|
9
|
+
const deleted: T[] = [];
|
|
10
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
11
|
+
if (predicate(array[i], i)) {
|
|
12
|
+
deleted.push(...array.splice(i, 1));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return deleted;
|
|
17
|
+
};
|