Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle undefined values in "match" method #69

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: handle undefined values in "match" method
  • Loading branch information
luckasnix committed Feb 14, 2025
commit 3e6fd8dc6bf48485432e56aa90951e0d75cab75b
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kasnix/structured-objects",
"version": "1.2.1",
"version": "1.2.2",
"repository": {
"type": "git",
"url": "git+https://github.com/luckasnix/structured-objects.git"
Expand Down
3 changes: 3 additions & 0 deletions src/object-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
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) {
Expand Down
23 changes: 23 additions & 0 deletions tests/object-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,27 @@ describe("match()", () => {
expect(matchedShirt.size).oneOf(sizesToMatch);
}
});

test("gets matched nodes ignoring undefined value in the shape", () => {
const colorsToMatch: Color[] = ["yellow", "green"];
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

const matchedShirts = shirtsObjectGraph.match({
color: colorsToMatch,
size: undefined,
});

expect(matchedShirts).toHaveLength(3);
for (const matchedShirt of matchedShirts) {
expect(matchedShirt.color).oneOf(colorsToMatch);
}
});

test("gets all nodes for an empty shape", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

const matchedShirts = shirtsObjectGraph.match({});

expect(matchedShirts).toHaveLength(8);
});
});