Skip to content

Commit 9078337

Browse files
authored
feat: enable non-array values in "match" method (#81)
1 parent c5685b5 commit 9078337

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

docs/object-graph.doc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@ Returns all nodes that match with the provided shape.
137137

138138
| Name | Type | Default | Description |
139139
| --- | --- | --- | --- |
140-
| shape | `Partial<Record<keyof NodeValue, Array<unknown>>>` | - | __Required.__ The shape of the nodes to return from the object graph. |
140+
| shape | `Partial<Record<keyof NodeValue, unknown>>` | - | __Required.__ The shape of the nodes to return from the object graph. |

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
@@ -1,6 +1,6 @@
11
{
22
"name": "@kasnix/structured-objects",
3-
"version": "1.4.2",
3+
"version": "1.5.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/luckasnix/structured-objects.git"

src/object-graph.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,23 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
204204
* @description Returns all nodes that match with the provided shape.
205205
* @since 1.0.0
206206
*/
207-
public match(shape: Partial<Record<keyof NodeValue, Array<unknown>>>): Array<NodeValue> {
207+
public match(shape: Partial<Record<keyof NodeValue, unknown>>): Array<NodeValue> {
208208
if (!shape) {
209209
throw new Error("Provide a value for the 'shape' parameter");
210210
}
211211
const matchedNodes: Array<NodeValue> = new Array();
212212
for (const [_, nodeValue] of this.nodes) {
213-
const shapeEntries = Object.entries(shape) as Array<[keyof NodeValue, Array<unknown>]>;
213+
const shapeEntries = Object.entries(shape) as Array<[keyof NodeValue, unknown]>;
214214
const hasMatched = shapeEntries.every((shapeEntry) => {
215-
if (shapeEntry[1] === undefined) {
215+
const shapePropKey = shapeEntry[0];
216+
const shapePropValue = shapeEntry[1];
217+
if (shapePropValue === undefined) {
216218
return true;
217219
}
218-
return shapeEntry[1].includes(nodeValue[shapeEntry[0]]);
220+
const shapePropValueArray: Array<unknown> = Array.isArray(shapePropValue)
221+
? shapePropValue
222+
: [shapePropValue];
223+
return shapePropValueArray.includes(nodeValue[shapePropKey]);
219224
});
220225
if (hasMatched) {
221226
matchedNodes.push(nodeValue);

tests/object-graph.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe("valuesOf()", () => {
236236
});
237237

238238
describe("match()", () => {
239-
test("gets all nodes that match with the provided shape", () => {
239+
test("gets all nodes that match the provided shape", () => {
240240
const colorsToMatch: Color[] = ["red", "blue"];
241241
const sizesToMatch: Size[] = ["small", "medium"];
242242
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
@@ -253,6 +253,19 @@ describe("match()", () => {
253253
}
254254
});
255255

256+
test("gets all nodes that match a shape with non-array values", () => {
257+
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
258+
259+
const matchedShirts = shirtsGraph.match({
260+
color: "red",
261+
size: "small",
262+
});
263+
264+
expect(matchedShirts).toHaveLength(1);
265+
expect(matchedShirts[0].color).toBe("red");
266+
expect(matchedShirts[0].size).toBe("small");
267+
});
268+
256269
test("gets matched nodes ignoring undefined value in the shape", () => {
257270
const colorsToMatch: Color[] = ["yellow", "green"];
258271
const shirtsGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

0 commit comments

Comments
 (0)