-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-graph.ts
226 lines (210 loc) · 6.74 KB
/
object-graph.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
export class ObjectGraph<NodeValue extends Record<string, unknown>> {
private nodes: Map<string, NodeValue>;
private keyExtractor: (nodeValue: NodeValue) => string;
/**
* @description Returns an instance of ObjectGraph.
* @since 1.0.0
*/
constructor(nodeValues: Array<NodeValue>, keyExtractor: (nodeValue: NodeValue) => string) {
if (!nodeValues) {
throw new Error("Provide a value for the 'nodeValues' parameter");
}
if (!keyExtractor) {
throw new Error("Provide a value for the 'keyExtractor' parameter");
}
this.nodes = new Map();
this.keyExtractor = keyExtractor;
if (nodeValues.length > 0) {
for (const nodeValue of nodeValues) {
this.nodes.set(this.keyExtractor(nodeValue), nodeValue);
}
}
}
/**
* @description Returns the length of the object graph.
* @since 1.0.0
* @deprecated Since version 1.2.0. Will be removed in version 2.0.0. Use "size" instead.
*/
public get length(): number {
return this.nodes.size;
}
/**
* @description Returns the size of the object graph.
* @since 1.2.0
*/
public get size(): number {
return this.nodes.size;
}
/**
* @description Returns an iterator object that contains the keys of the object graph.
* @since 1.0.0
*/
public keys(): IterableIterator<string> {
return this.nodes.keys();
}
/**
* @description Returns an iterator object that contains the values of the object graph.
* @since 1.0.0
*/
public values(): IterableIterator<NodeValue> {
return this.nodes.values();
}
/**
* @description Returns a node of the object graph.
* @since 1.0.0
*/
public get(nodeKey: string): NodeValue | undefined {
if (!nodeKey) {
throw new Error("Provide a value for the 'nodeKey' parameter");
}
if (typeof nodeKey !== "string") {
throw new TypeError("The parameter 'nodeKey' must be a string");
}
const nodeValue = this.nodes.get(nodeKey);
if (!nodeValue) {
console.error("A node with this key does not exist in the object graph");
}
return nodeValue;
}
/**
* @description Returns a copy of the original object graph.
* @since 1.0.0
*/
public copy(): ObjectGraph<NodeValue> {
return new ObjectGraph(Array.from(this.nodes.values()), this.keyExtractor);
}
/**
* @description Returns a subgraph of the original object graph.
* @since 1.3.0
*/
public subgraph(nodeKeys: Array<string>): ObjectGraph<NodeValue> {
if (!nodeKeys) {
throw new Error("Provide a value for the 'nodeKeys' parameter");
}
if (!Array.isArray(nodeKeys)) {
throw new TypeError("The parameter 'nodeKeys' must be an array");
}
if (nodeKeys.length === 0) {
throw new Error("The parameter 'nodeKeys' must contain at least one item");
}
const subgraph = new ObjectGraph([], this.keyExtractor);
for (const [nodeKey, nodeValue] of this.nodes) {
if (nodeKeys.includes(nodeKey)) {
subgraph.add(nodeValue);
}
}
return subgraph;
}
/**
* @description Adds a node to the object graph.
* @since 1.0.0
*/
public add(nodeValue: NodeValue): void {
if (!nodeValue) {
throw new Error("Provide a value for the 'nodeValue' parameter");
}
const nodeKey = this.keyExtractor(nodeValue);
if (this.nodes.get(nodeKey)) {
throw new Error("A node with the same key already exists in the object graph");
}
this.nodes.set(nodeKey, nodeValue);
}
/**
* @description Returns a copy of the original object graph with a received node added.
* @since 1.0.0
*/
public toAdded(nodeValue: NodeValue): ObjectGraph<NodeValue> {
const copiedGraph = this.copy();
copiedGraph.add(nodeValue);
return copiedGraph;
}
/**
* @description Updates a node in the object graph.
* @since 1.0.0
*/
public update(nodeValue: NodeValue): void {
if (!nodeValue) {
throw new Error("Provide a value for the 'nodeValue' parameter");
}
const nodeKey = this.keyExtractor(nodeValue);
if (!this.nodes.get(nodeKey)) {
throw new Error("A node with the provided key does not exist in the object graph");
}
this.nodes.set(nodeKey, nodeValue);
}
/**
* @description Returns a copy of the original object graph with a received node updated.
* @since 1.0.0
*/
public toUpdated(nodeValue: NodeValue): ObjectGraph<NodeValue> {
const copiedGraph = this.copy();
copiedGraph.update(nodeValue);
return copiedGraph;
}
/**
* @description Removes a node from the object graph.
* @since 1.0.0
*/
public remove(nodeKey: string): void {
if (!nodeKey) {
throw new Error("Provide a value for the 'nodeKey' parameter");
}
if (typeof nodeKey !== "string") {
throw new TypeError("The parameter 'nodeKey' must be a string");
}
this.nodes.delete(nodeKey);
}
/**
* @description Returns a copy of the original object graph with a received node removed.
* @since 1.0.0
*/
public toRemoved(nodeKey: string): ObjectGraph<NodeValue> {
const copiedGraph = this.copy();
copiedGraph.remove(nodeKey);
return copiedGraph;
}
/**
* @description Returns a list of unique values for a specified property across selected nodes in the object graph. If no selection is made, it operates on the entire graph.
* @since 1.0.0
*/
public valuesOf<NodeValueKey extends keyof NodeValue>(
nodeValueKey: NodeValueKey,
nodeKeys?: Array<string>,
): Array<NodeValue[NodeValueKey]> {
if (!nodeValueKey) {
throw new Error("Provide a value for the 'nodeValueKey' parameter");
}
if (typeof nodeValueKey !== "string") {
throw new TypeError("The parameter 'nodeValueKey' must be a string");
}
const propertyValues: Set<NodeValue[NodeValueKey]> = new Set();
const graphNodes = nodeKeys ? this.subgraph(nodeKeys).nodes : this.nodes;
for (const [_, nodeValue] of graphNodes) {
propertyValues.add(nodeValue[nodeValueKey as NodeValueKey]);
}
return Array.from(propertyValues);
}
/**
* @description Returns all nodes that match with the provided shape.
* @since 1.0.0
*/
public match(shape: Partial<Record<keyof NodeValue, Array<unknown>>>): Array<NodeValue> {
if (!shape) {
throw new Error("Provide a value for the 'shape' parameter");
}
const matchedNodes: Array<NodeValue> = new Array();
for (const [_, nodeValue] of this.nodes) {
const shapeEntries = Object.entries(shape) as Array<[keyof NodeValue, Array<unknown>]>;
const hasMatched = shapeEntries.every((shapeEntry) => {
if (shapeEntry[1] === undefined) {
return true;
}
return shapeEntry[1].includes(nodeValue[shapeEntry[0]]);
});
if (hasMatched) {
matchedNodes.push(nodeValue);
}
}
return matchedNodes;
}
}