Improve optimizations

This commit is contained in:
Pieter Vander Vennet 2024-07-29 19:05:10 +02:00
parent 82b25bd2a6
commit d98816212d
2 changed files with 14 additions and 6 deletions

View file

@ -137,15 +137,13 @@ export class Tag extends TagsFilter {
* new Tag("key","value").shadows(new And([new Tag("x","y"), new RegexTag("a","b", true)]) // => false
*/
shadows(other: TagsFilter): boolean {
if (other["key"] !== this.key) {
return false
}
if (other instanceof Tag) {
// Other.key === this.key
return other.value === this.value
return other.key === this.key && other.value === this.value
}
if (other instanceof RegexTag) {
return other.matchesProperties({ [this.key]: this.value })
if(other.key === this.key || !other.invert){
return other.matchesProperties({ [this.key]: this.value })
}
}
return false
}

View file

@ -181,6 +181,16 @@ describe("Tag optimalization", () => {
const q = new And([new Tag("key", "value"), new RegexTag("key", /value/, true)])
expect(q.optimize()).toBe(false)
})
it("should optimize regexes in the key", () => {
const spec = TagUtils.Tag({
and: [
"service:bicycle:retail=yes",
"service:bicycle:.+~~yes"
]
})
const tag = <TagsFilter> spec.optimize()
expect(tag.asJson()).to.eq("service:bicycle:retail=yes")
})
})
describe("Or", () => {