Fix: correctly optimize 'key=A&key!=B' into 'key=A'

This commit is contained in:
Pieter Vander Vennet 2023-05-16 01:33:58 +02:00
parent 201f5446f8
commit d8a5f3c2be
2 changed files with 36 additions and 10 deletions

View file

@ -243,7 +243,9 @@ export class And extends TagsFilter {
properties[opt.key] = opt.value
}
}
for (const opt of optimized) {
for (let i = 0; i < optimized.length; i++){
const opt = optimized[i];
if (opt instanceof Tag) {
const k = opt.key
const v = properties[k]
@ -264,7 +266,23 @@ export class And extends TagsFilter {
if (v === undefined) {
continue
}
if (v !== opt.value) {
if(opt.invert){
// We should _not_ match this value
// If 'v' is given, we already know what value it should be
// If 'v' is the not-expected value, we have a conflict and return false
// Otherwise, we can safely drop this value
const doesMatch = (typeof opt.value === "string" && v === opt.value) ||
(v.match(<RegExp> opt.value) !== null)
if(doesMatch){
// We have a conflict as 'opt' is inverted
return false
}else{
optimized.splice(i, 1)
i--
}
}else if (v !== opt.value) {
// detected an internal conflict
return false
}

View file

@ -1,10 +1,10 @@
import { TagsFilter } from "../../../Logic/Tags/TagsFilter"
import { And } from "../../../Logic/Tags/And"
import { Tag } from "../../../Logic/Tags/Tag"
import { TagUtils } from "../../../Logic/Tags/TagUtils"
import { Or } from "../../../Logic/Tags/Or"
import { RegexTag } from "../../../Logic/Tags/RegexTag"
import { describe, expect, it } from "vitest"
import {TagsFilter} from "../../../Logic/Tags/TagsFilter"
import {And} from "../../../Logic/Tags/And"
import {Tag} from "../../../Logic/Tags/Tag"
import {TagUtils} from "../../../Logic/Tags/TagUtils"
import {Or} from "../../../Logic/Tags/Or"
import {RegexTag} from "../../../Logic/Tags/RegexTag"
import {describe, expect, it} from "vitest"
describe("Tag optimalization", () => {
describe("And", () => {
@ -71,6 +71,14 @@ describe("Tag optimalization", () => {
expect(TagUtils.toString(opt)).toBe("amenity=binoculars&bicycle=yes")
})
it("should correctly optimize key=A&key!=B into key=A", () => {
const t = new And([new Tag("shop", "sports"), new RegexTag("shop", "mall", true)])
const opt = t.optimize()
expect(typeof opt !== "boolean").true
expect(TagUtils.toString(<TagsFilter>opt)).toBe("shop=sports")
})
it("should optimize nested ORs", () => {
const filter = TagUtils.Tag({
or: [
@ -263,7 +271,7 @@ describe("Tag optimalization", () => {
or: [
"club=climbing",
{
and: ["sport=climbing", { or: ["club~*", "office~*"] }],
and: ["sport=climbing", {or: ["club~*", "office~*"]}],
},
{
and: [