Skip to content

Commit

Permalink
fix idempotence in $and
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Jun 9, 2024
1 parent 75e214f commit 999c2d0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
50 changes: 49 additions & 1 deletion applications/__tests__/resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ describe("resolver", () => {
$and: ["string", "number"],
})
).toEqual("undefined")

expect(resolveAndMerge({$and: []})).toEqual("undefined")

expect(
resolveAndMerge({$and: [{az: "string"}, {array: "string"}]})
).toEqual("undefined")

expect(resolveAndMerge({$and: {}})).toEqual("undefined")

expect(resolveAndMerge({$and: [42, true]})).toEqual("undefined")
})

Expand All @@ -33,6 +37,7 @@ describe("resolver", () => {
"bukh",
"vidh",
])

expect(resolveAndMerge([["az", "bukh"], ["vidh"]])).toEqual([
"az",
"bukh",
Expand All @@ -51,11 +56,54 @@ describe("resolver", () => {
test("distributivity in nested ORs in $and", () => {
expect(
resolveAndMerge({
$and: [[{az: "string"}, {bukh: "string"}], [{vidh: "string"}]],
$and: [{az: "string"}, [{bukh: "string"}, {vidh: "string"}]],
})
).toEqual([
{az: "string", bukh: "string"},
{az: "string", vidh: "string"},
])

expect(
resolveAndMerge({
$and: [[{az: "string"}], [{bukh: "string"}, {vidh: "string"}]],
})
).toEqual([
{az: "string", bukh: "string"},
{az: "string", vidh: "string"},
])

// TODO: fix to avoid excessive results
expect(
resolveAndMerge({
$and: [
[{az: "string"}, {bukh: "string"}],
[{az: "string"}, {vidh: "string"}],
],
})
).toEqual([
{az: "string"},
{az: "string", vidh: "string"}, // <-- excessive
{az: "string", bukh: "string"}, // <-- excessive
{bukh: "string", vidh: "string"},
])
})

// TODO: implement this
test.skip("idempotence in ORs", () => {
expect(resolveAndMerge(["az", "az"])).toEqual("az")
})

test("idempotence in $and", () => {
expect(
resolveAndMerge({
$and: ["az", "az"],
})
).toEqual("az")

expect(
resolveAndMerge({
$and: [{az: "string"}, {bukh: "string"}, {az: "string"}],
})
).toEqual({az: "string", bukh: "string"})
})
})
3 changes: 3 additions & 0 deletions applications/x-types-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const deepMergeTwo = (first, second) => {
if (second === "any") {
return first
}
if (first === second) {
return first
}

if (isPrimitive(first) || isPrimitive(second)) {
console.error(
Expand Down

0 comments on commit 999c2d0

Please sign in to comment.