Skip to content

Commit 9a67d27

Browse files
committed
feat: add "valuesBy" method
1 parent fc8d43e commit 9a67d27

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

docs/object-graph.doc.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ Returns a list of unique values for a specified property across selected nodes i
129129
| propertyKey | `keyof NodeValue` | - | __Required.__ The property key of the node values to return from the object graph. |
130130
| nodeKeys | `Array<string>` | - | The array of nodes keys to define the selected nodes. |
131131

132+
### valuesBy()
133+
134+
Returns all values grouped by property.
135+
132136
### match()
133137

134138
Returns all nodes that match with the provided shape.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@kasnix/structured-objects",
33
"description": "A lightweight and powerful library for manipulating objects in JavaScript and TypeScript across different types of data structures.",
44
"author": "Kasnix",
5-
"version": "1.5.2",
5+
"version": "1.6.0",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/luckasnix/structured-objects.git"

src/object-graph.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
209209
return Array.from(propertyValues);
210210
}
211211

212+
/**
213+
* @description Returns all values grouped by property.
214+
* @since 1.6.0
215+
*/
216+
public valuesBy(): Record<string, Array<unknown>> {
217+
const valuesByProperty: Record<string, Array<unknown>> = {};
218+
for (const [_, nodeValue] of this.nodes) {
219+
for (const [nodeValueKey, nodeValueValue] of Object.entries(nodeValue)) {
220+
if (!valuesByProperty[nodeValueKey]) {
221+
valuesByProperty[nodeValueKey] = [];
222+
}
223+
if (!valuesByProperty[nodeValueKey].includes(nodeValueValue)) {
224+
valuesByProperty[nodeValueKey].push(nodeValueValue);
225+
}
226+
}
227+
}
228+
return valuesByProperty;
229+
}
230+
212231
/**
213232
* @description Returns all nodes that match with the provided shape.
214233
* @since 1.0.0

tests/object-graph.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,23 @@ describe("valuesOf()", () => {
291291
});
292292
});
293293

294+
describe("valuesBy()", () => {
295+
test("gets all values grouped by property", () => {
296+
const shirtsGraph = new ObjectGraph<Shirt>(
297+
shirtsMock,
298+
(shirt) => shirt.sku,
299+
);
300+
301+
const valuesByProperty = shirtsGraph.valuesBy();
302+
303+
expect(valuesByProperty).toEqual({
304+
sku: ["1", "2", "3", "4", "5", "6", "7", "8"],
305+
color: ["red", "yellow", "green", "blue"],
306+
size: ["small", "medium", "large"],
307+
});
308+
});
309+
});
310+
294311
describe("match()", () => {
295312
test("gets all nodes that match the provided shape", () => {
296313
const colorsToMatch: Color[] = ["red", "blue"];

0 commit comments

Comments
 (0)