Fix Tag.matchesProperties and RegexTag.matchesProperties, they do match on definedProperties now (and are faster)

This commit is contained in:
Pieter Vander Vennet 2021-10-10 20:14:06 +02:00
parent 30bc620827
commit 15ff38a098
3 changed files with 51 additions and 21 deletions

View file

@ -47,6 +47,11 @@ export class RegexTag extends TagsFilter {
} }
matchesProperties(tags: any): boolean { matchesProperties(tags: any): boolean {
if(typeof this.key === "string"){
const value = tags[this.key] ?? ""
return RegexTag.doesMatch(value, this.value) != this.invert;
}
for (const key in tags) { for (const key in tags) {
if (key === undefined) { if (key === undefined) {
continue; continue;

View file

@ -23,26 +23,14 @@ export class Tag extends TagsFilter {
matchesProperties(properties: any): boolean { matchesProperties(properties: any): boolean {
for (const propertiesKey in properties) { const foundValue = properties[this.key]
if (!properties.hasOwnProperty(propertiesKey)) { if (foundValue === undefined && (this.value === "" || this.value === undefined)) {
continue
}
if (this.key === propertiesKey) {
const value = properties[propertiesKey];
if (value === undefined) {
continue
}
return value === this.value;
}
}
// The tag was not found // The tag was not found
if (this.value === "") {
// and it shouldn't be found! // and it shouldn't be found!
return true; return true;
} }
return false; return foundValue === this.value;
} }
asOverpass(): string[] { asOverpass(): string[] {

View file

@ -9,6 +9,7 @@ import {Tag} from "../Logic/Tags/Tag";
import {And} from "../Logic/Tags/And"; import {And} from "../Logic/Tags/And";
import {TagUtils} from "../Logic/Tags/TagUtils"; import {TagUtils} from "../Logic/Tags/TagUtils";
import TagRenderingConfig from "../Models/ThemeConfig/TagRenderingConfig"; import TagRenderingConfig from "../Models/ThemeConfig/TagRenderingConfig";
import {RegexTag} from "../Logic/Tags/RegexTag";
Utils.runningFromConsole = true; Utils.runningFromConsole = true;
@ -173,7 +174,6 @@ export default class TagSpec extends T {
equal(undefined, tr.GetRenderValue({"foo": "bar"})); equal(undefined, tr.GetRenderValue({"foo": "bar"}));
})], })],
[ [
"Empty match test", "Empty match test",
() => { () => {
@ -214,7 +214,8 @@ export default class TagSpec extends T {
const overpassOrInor = TagUtils.Tag(orInOr).asOverpass() const overpassOrInor = TagUtils.Tag(orInOr).asOverpass()
equal(3, overpassOrInor.length) equal(3, overpassOrInor.length)
} }
], [ ],
[
"Merge touching opening hours", "Merge touching opening hours",
() => { () => {
const oh1: OpeningHour = { const oh1: OpeningHour = {
@ -239,7 +240,8 @@ export default class TagSpec extends T {
equal(r.endHour, 12) equal(r.endHour, 12)
} }
], [ ],
[
"Merge overlapping opening hours", "Merge overlapping opening hours",
() => { () => {
const oh1: OpeningHour = { const oh1: OpeningHour = {
@ -394,7 +396,8 @@ export default class TagSpec extends T {
])); ]));
equal(rules, "Tu 23:00-00:00"); equal(rules, "Tu 23:00-00:00");
}], ["JOIN OH with overflowed hours", () => { }],
["JOIN OH with overflowed hours", () => {
const rules = OH.ToString( const rules = OH.ToString(
OH.MergeTimes([ OH.MergeTimes([
@ -483,7 +486,41 @@ export default class TagSpec extends T {
const tagRendering = new TagRenderingConfig(config, null, "test"); const tagRendering = new TagRenderingConfig(config, null, "test");
equal(true, tagRendering.IsKnown({bottle: "yes"})) equal(true, tagRendering.IsKnown({bottle: "yes"}))
equal(false, tagRendering.IsKnown({})) equal(false, tagRendering.IsKnown({}))
}]]); }],
[
"Tag matches a lazy property",
() => {
const properties = {}
const key = "_key"
Object.defineProperty(properties, key, {
configurable: true,
get: function () {
delete properties[key]
properties[key] = "yes"
return "yes"
}
})
const filter = new Tag("_key", "yes")
T.isTrue(filter.matchesProperties(properties), "Lazy value not matched")
}
],
[
"RegextTag matches a lazy property",
() => {
const properties = {}
const key = "_key"
Object.defineProperty(properties, key, {
configurable: true,
get: function () {
delete properties[key]
properties[key] = "yes"
return "yes"
}
})
const filter = TagUtils.Tag("_key~*")
T.isTrue(filter.matchesProperties(properties), "Lazy value not matched")
}
]]);
} }
} }