-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-graph.ts
182 lines (169 loc) · 5.16 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
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 0.1.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 0.1.0
*/
public get length() {
return this.nodes.size;
}
/**
* @description Returns a node of the object graph.
* @since 0.1.0
*/
public get(nodeKey: string) {
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 all nodes of the object graph.
* @since 0.1.0
*/
public getAll() {
return Array.from(this.nodes.values());
}
/**
* @description Returns a copy of the original object graph.
* @since 0.1.0
*/
public copy() {
return new ObjectGraph(Array.from(this.nodes.values()), this.keyExtractor);
}
/**
* @description Adds a node to the object graph.
* @since 0.1.0
*/
public add(nodeValue: NodeValue) {
if (!nodeValue) {
throw new Error("Provide a value for the 'nodeValue' parameter");
}
const nodeKey = this.keyExtractor(nodeValue);
if (this.nodes.get(nodeKey)) {
console.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 0.1.0
*/
public toAdded(nodeValue: NodeValue) {
const copiedObjectGraph = this.copy();
copiedObjectGraph.add(nodeValue);
return copiedObjectGraph;
}
/**
* @description Updates a node in the object graph.
* @since 0.1.0
*/
public update(nodeValue: NodeValue) {
if (!nodeValue) {
throw new Error("Provide a value for the 'nodeValue' parameter");
}
const nodeKey = this.keyExtractor(nodeValue);
if (!this.nodes.get(nodeKey)) {
console.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 0.1.0
*/
public toUpdated(nodeValue: NodeValue) {
const copiedObjectGraph = this.copy();
copiedObjectGraph.update(nodeValue);
return copiedObjectGraph;
}
/**
* @description Removes a node from the object graph.
* @since 0.1.0
*/
public remove(nodeKey: string) {
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");
}
if (!this.nodes.get(nodeKey)) {
console.error("A node with this key does not exist in this object graph");
}
this.nodes.delete(nodeKey);
}
/**
* @description Returns a copy of the original object graph with a received node removed.
* @since 0.1.0
*/
public toRemoved(nodeKey: string) {
const copiedObjectGraph = this.copy();
copiedObjectGraph.remove(nodeKey);
return copiedObjectGraph;
}
/**
* @description Returns all values of the provided property.
* @since 0.1.0
*/
public valuesOf(propertyKey: keyof NodeValue) {
if (!propertyKey) {
throw new Error("Provide a value for the 'propertyKey' parameter");
}
if (typeof propertyKey !== "string") {
throw new TypeError("The parameter 'propertyKey' must be a string");
}
const propertyValues = new Set();
for (const [_, nodeValue] of this.nodes) {
propertyValues.add(nodeValue[propertyKey]);
}
return Array.from(propertyValues);
}
/**
* @description Returns all nodes that match with the provided shape.
* @since 0.1.0
*/
public match(shape: Partial<Record<keyof NodeValue, Array<unknown>>>) {
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) => {
return shapeEntry[1].includes(nodeValue[shapeEntry[0]]);
});
if (hasMatched) {
matchedNodes.push(nodeValue);
}
}
return Array.from(matchedNodes);
}
}