Add substituting tag, remove some old code

This commit is contained in:
Pieter Vander Vennet 2021-03-29 01:20:06 +02:00
parent 120832f241
commit cd1171e678
8 changed files with 112 additions and 31 deletions

View file

@ -46,14 +46,6 @@ export class And extends TagsFilter {
return allChoices;
}
substituteValues(tags: any): TagsFilter {
const newChoices = [];
for (const c of this.and) {
newChoices.push(c.substituteValues(tags));
}
return new And(newChoices);
}
asHumanString(linkToWiki: boolean, shorten: boolean) {
return this.and.map(t => t.asHumanString(linkToWiki, shorten)).join("&");
}

View file

@ -30,14 +30,6 @@ export class Or extends TagsFilter {
return choices;
}
substituteValues(tags: any): TagsFilter {
const newChoices = [];
for (const c of this.or) {
newChoices.push(c.substituteValues(tags));
}
return new Or(newChoices);
}
asHumanString(linkToWiki: boolean, shorten: boolean) {
return this.or.map(t => t.asHumanString(linkToWiki, shorten)).join("|");
}

View file

@ -0,0 +1,59 @@
import {TagsFilter} from "./TagsFilter";
/**
* The substituting-tag uses the tags of a feature a variables and replaces them.
*
* e.g. key:={other_key}_{ref} will match an object that has at least 'key'.
* If {other_key} is _not_ defined, it will not be substituted.
*
* The 'key' is always fixed and should not contain substitutions.
* This cannot be used to query features
*/
export default class SubstitutingTag implements TagsFilter {
private readonly _key: string;
private readonly _value: string;
constructor(key: string, value: string) {
this._key = key;
this._value = value;
}
private static substituteString(template: string, dict: any) {
for (const k in dict) {
template = template.replace(new RegExp("\\{" + k + "\\}", 'g'), dict[k])
}
return template;
}
asHumanString(linkToWiki: boolean, shorten: boolean) {
return this._key + ":=" + this._value;
}
asOverpass(): string[] {
throw "A variable with substitution can not be used to query overpass"
}
isEquivalent(other: TagsFilter): boolean {
if (!(other instanceof SubstitutingTag)) {
return false;
}
return other._key === this._key && other._value === this._value;
}
isUsableAsAnswer(): boolean {
return true;
}
matchesProperties(properties: any): boolean {
const value = properties[this._key];
if (value === undefined || value === "") {
return false;
}
const expectedValue = SubstitutingTag.substituteString(this._value, properties);
return value === expectedValue;
}
usedKeys(): string[] {
return [this._key];
}
}

View file

@ -2,8 +2,6 @@ export abstract class TagsFilter {
abstract asOverpass(): string[]
abstract substituteValues(tags: any): TagsFilter;
abstract isUsableAsAnswer(): boolean;
abstract isEquivalent(other: TagsFilter): boolean;
@ -13,12 +11,5 @@ export abstract class TagsFilter {
abstract asHumanString(linkToWiki: boolean, shorten: boolean);
abstract usedKeys(): string[];
public matches(tags: { k: string, v: string }[]) {
const properties = {};
for (const kv of tags) {
properties[kv.k] = kv.v;
}
return this.matchesProperties(properties);
}
}