2020-08-26 00:21:34 +02:00
|
|
|
import {Utils} from "../Utils";
|
2020-07-16 14:21:06 +02:00
|
|
|
|
|
|
|
export abstract class TagsFilter {
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-07-16 14:21:06 +02:00
|
|
|
abstract asOverpass(): string[]
|
2021-01-06 01:11:07 +01:00
|
|
|
|
|
|
|
abstract substituteValues(tags: any): TagsFilter;
|
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
abstract isUsableAsAnswer(): boolean;
|
|
|
|
|
|
|
|
abstract isEquivalent(other: TagsFilter): boolean;
|
2020-07-16 14:21:06 +02:00
|
|
|
|
2021-03-22 02:17:00 +01:00
|
|
|
abstract matchesProperties(properties: any): boolean;
|
2020-08-22 17:33:08 +02:00
|
|
|
|
2020-08-27 18:44:16 +02:00
|
|
|
abstract asHumanString(linkToWiki: boolean, shorten: boolean);
|
2021-02-20 16:48:42 +01:00
|
|
|
|
|
|
|
abstract usedKeys(): string[];
|
2021-03-22 02:17:00 +01:00
|
|
|
|
|
|
|
public matches(tags: {k: string, v: string}[]){
|
|
|
|
const properties = {};
|
|
|
|
for (const kv of tags) {
|
|
|
|
properties[kv.k] = kv.v;
|
|
|
|
}
|
|
|
|
return this.matchesProperties(properties);
|
|
|
|
}
|
2020-07-16 14:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
export class RegexTag extends TagsFilter {
|
2020-09-03 02:05:09 +02:00
|
|
|
private readonly key: RegExp | string;
|
|
|
|
private readonly value: RegExp | string;
|
2020-08-30 01:13:18 +02:00
|
|
|
private readonly invert: boolean;
|
2021-03-14 01:40:35 +01:00
|
|
|
private readonly matchesEmpty: boolean
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2020-09-03 02:05:09 +02:00
|
|
|
constructor(key: string | RegExp, value: RegExp | string, invert: boolean = false) {
|
2020-07-16 14:21:06 +02:00
|
|
|
super();
|
2020-09-03 02:05:09 +02:00
|
|
|
this.key = key;
|
2020-08-30 01:13:18 +02:00
|
|
|
this.value = value;
|
|
|
|
this.invert = invert;
|
2021-03-14 01:40:35 +01:00
|
|
|
this.matchesEmpty = RegexTag.doesMatch("", this.value);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 02:05:09 +02:00
|
|
|
private static doesMatch(fromTag: string, possibleRegex: string | RegExp): boolean {
|
2021-01-06 01:11:07 +01:00
|
|
|
if (typeof possibleRegex === "string") {
|
2020-09-03 02:05:09 +02:00
|
|
|
return fromTag === possibleRegex;
|
|
|
|
}
|
|
|
|
return fromTag.match(possibleRegex) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static source(r: string | RegExp) {
|
|
|
|
if (typeof (r) === "string") {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return r.source;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
|
|
|
asOverpass(): string[] {
|
|
|
|
if (typeof this.key === "string") {
|
|
|
|
return [`['${this.key}'${this.invert ? "!" : ""}~'${RegexTag.source(this.value)}']`];
|
|
|
|
}
|
|
|
|
return [`[~'${this.key.source}'${this.invert ? "!" : ""}~'${RegexTag.source(this.value)}']`];
|
|
|
|
}
|
|
|
|
|
2020-09-08 00:33:05 +02:00
|
|
|
isUsableAsAnswer(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-22 02:17:00 +01:00
|
|
|
|
|
|
|
matchesProperties(tags: any): boolean {
|
|
|
|
for (const key in tags) {
|
|
|
|
if (RegexTag.doesMatch(key, this.key)) {
|
|
|
|
const value = tags[key]
|
|
|
|
return RegexTag.doesMatch(value, this.value) != this.invert;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-22 02:17:00 +01:00
|
|
|
if (this.matchesEmpty) {
|
2021-03-14 01:40:35 +01:00
|
|
|
// The value is 'empty'
|
|
|
|
return !this.invert;
|
|
|
|
}
|
2020-09-05 15:27:35 +02:00
|
|
|
// The matching key was not found
|
|
|
|
return this.invert;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
substituteValues(tags: any): TagsFilter {
|
2020-08-30 01:13:18 +02:00
|
|
|
return this;
|
2020-07-05 18:59:47 +02:00
|
|
|
}
|
2020-09-09 18:42:13 +02:00
|
|
|
|
2020-08-22 17:33:08 +02:00
|
|
|
asHumanString() {
|
2020-09-08 00:33:05 +02:00
|
|
|
if (typeof this.key === "string") {
|
|
|
|
return `${this.key}${this.invert ? "!" : ""}~${RegexTag.source(this.value)}`;
|
|
|
|
}
|
2020-09-11 02:14:16 +02:00
|
|
|
return `${this.key.source}${this.invert ? "!" : ""}~~${RegexTag.source(this.value)}`
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|
2020-09-09 18:42:13 +02:00
|
|
|
|
|
|
|
isEquivalent(other: TagsFilter): boolean {
|
|
|
|
if (other instanceof RegexTag) {
|
|
|
|
return other.asHumanString() == this.asHumanString();
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
if (other instanceof Tag) {
|
2020-09-09 18:42:13 +02:00
|
|
|
return RegexTag.doesMatch(other.key, this.key) && RegexTag.doesMatch(other.value, this.value);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-20 16:48:42 +01:00
|
|
|
|
|
|
|
usedKeys(): string[] {
|
|
|
|
if (typeof this.key === "string") {
|
|
|
|
return [this.key];
|
|
|
|
}
|
|
|
|
throw "Key cannot be determined as it is a regex"
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:16:21 +02:00
|
|
|
|
2020-07-16 14:21:06 +02:00
|
|
|
export class Tag extends TagsFilter {
|
2020-07-30 00:59:08 +02:00
|
|
|
public key: string
|
2020-08-30 01:13:18 +02:00
|
|
|
public value: string
|
2020-07-29 17:16:59 +02:00
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
constructor(key: string, value: string) {
|
2020-07-16 14:21:06 +02:00
|
|
|
super()
|
2020-07-29 13:16:21 +02:00
|
|
|
this.key = key
|
|
|
|
this.value = value
|
2021-01-06 01:11:07 +01:00
|
|
|
if (key === undefined || key === "") {
|
2020-09-26 03:02:19 +02:00
|
|
|
throw "Invalid key: undefined or empty";
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
if (value === undefined) {
|
2020-09-26 03:02:19 +02:00
|
|
|
throw "Invalid value: value is undefined";
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
if (value === "*") {
|
|
|
|
console.warn(`Got suspicious tag ${key}=* ; did you mean ${key}~* ?`)
|
2020-07-29 13:16:21 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:17:00 +01:00
|
|
|
|
|
|
|
matchesProperties(properties: any): boolean {
|
|
|
|
for (const propertiesKey in properties) {
|
|
|
|
if(this.key === propertiesKey){
|
|
|
|
const value = properties[propertiesKey];
|
|
|
|
return value === this.value;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2021-03-22 02:17:00 +01:00
|
|
|
}
|
2020-09-17 13:13:02 +02:00
|
|
|
// The tag was not found
|
2021-01-06 01:11:07 +01:00
|
|
|
if (this.value === "") {
|
2020-09-17 13:13:02 +02:00
|
|
|
// and it shouldn't be found!
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
|
|
|
|
return false;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
asOverpass(): string[] {
|
|
|
|
if (this.value === "") {
|
|
|
|
// NOT having this key
|
2020-08-30 01:13:18 +02:00
|
|
|
return ['[!"' + this.key + '"]'];
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
return [`["${this.key}"="${this.value}"]`];
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:59:47 +02:00
|
|
|
substituteValues(tags: any) {
|
2020-07-29 17:16:59 +02:00
|
|
|
return new Tag(this.key, TagUtils.ApplyTemplate(this.value as string, tags));
|
2020-07-05 18:59:47 +02:00
|
|
|
}
|
2020-08-22 17:33:08 +02:00
|
|
|
|
2020-08-27 18:44:16 +02:00
|
|
|
asHumanString(linkToWiki: boolean, shorten: boolean) {
|
2020-08-30 01:13:18 +02:00
|
|
|
let v = this.value;
|
2020-08-27 18:44:16 +02:00
|
|
|
if (shorten) {
|
|
|
|
v = Utils.EllipsesAfter(v, 25);
|
|
|
|
}
|
2020-08-22 18:57:27 +02:00
|
|
|
if (linkToWiki) {
|
|
|
|
return `<a href='https://wiki.openstreetmap.org/wiki/Key:${this.key}' target='_blank'>${this.key}</a>` +
|
|
|
|
`=` +
|
2020-08-26 00:21:34 +02:00
|
|
|
`<a href='https://wiki.openstreetmap.org/wiki/Tag:${this.key}%3D${this.value}' target='_blank'>${v}</a>`
|
2020-08-22 18:57:27 +02:00
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
return this.key + "=" + v;
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-09-08 00:33:05 +02:00
|
|
|
isUsableAsAnswer(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
isEquivalent(other: TagsFilter): boolean {
|
2021-01-06 01:11:07 +01:00
|
|
|
if (other instanceof Tag) {
|
2020-09-09 18:42:13 +02:00
|
|
|
return this.key === other.key && this.value === other.value;
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
if (other instanceof RegexTag) {
|
2020-09-09 18:42:13 +02:00
|
|
|
other.isEquivalent(this);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-20 16:48:42 +01:00
|
|
|
|
|
|
|
usedKeys(): string[] {
|
|
|
|
return [this.key];
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-29 13:16:21 +02:00
|
|
|
export class Or extends TagsFilter {
|
2020-06-24 00:35:19 +02:00
|
|
|
public or: TagsFilter[]
|
|
|
|
|
|
|
|
constructor(or: TagsFilter[]) {
|
2020-07-16 14:21:06 +02:00
|
|
|
super();
|
2020-06-24 00:35:19 +02:00
|
|
|
this.or = or;
|
|
|
|
}
|
2021-03-22 02:17:00 +01:00
|
|
|
|
|
|
|
matchesProperties(properties: any): boolean {
|
2020-06-24 00:35:19 +02:00
|
|
|
for (const tagsFilter of this.or) {
|
2021-03-22 02:17:00 +01:00
|
|
|
if (tagsFilter.matchesProperties(properties)) {
|
2020-06-24 00:35:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
asOverpass(): string[] {
|
|
|
|
const choices = [];
|
|
|
|
for (const tagsFilter of this.or) {
|
|
|
|
const subChoices = tagsFilter.asOverpass();
|
2021-01-06 01:11:07 +01:00
|
|
|
for (const subChoice of subChoices) {
|
2020-06-24 00:35:19 +02:00
|
|
|
choices.push(subChoice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return choices;
|
|
|
|
}
|
|
|
|
|
2020-07-05 18:59:47 +02:00
|
|
|
substituteValues(tags: any): TagsFilter {
|
|
|
|
const newChoices = [];
|
|
|
|
for (const c of this.or) {
|
|
|
|
newChoices.push(c.substituteValues(tags));
|
|
|
|
}
|
|
|
|
return new Or(newChoices);
|
|
|
|
}
|
2020-08-22 18:57:27 +02:00
|
|
|
|
2020-08-27 18:44:16 +02:00
|
|
|
asHumanString(linkToWiki: boolean, shorten: boolean) {
|
|
|
|
return this.or.map(t => t.asHumanString(linkToWiki, shorten)).join("|");
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-09-08 00:33:05 +02:00
|
|
|
isUsableAsAnswer(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
isEquivalent(other: TagsFilter): boolean {
|
2021-01-06 01:11:07 +01:00
|
|
|
if (other instanceof Or) {
|
2020-09-09 18:42:13 +02:00
|
|
|
|
|
|
|
for (const selfTag of this.or) {
|
|
|
|
let matchFound = false;
|
2021-01-06 01:11:07 +01:00
|
|
|
for (let i = 0; i < other.or.length && !matchFound; i++) {
|
2020-09-09 18:42:13 +02:00
|
|
|
let otherTag = other.or[i];
|
|
|
|
matchFound = selfTag.isEquivalent(otherTag);
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
if (!matchFound) {
|
2020-09-09 18:42:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-20 16:48:42 +01:00
|
|
|
|
|
|
|
usedKeys(): string[] {
|
2021-03-14 01:40:35 +01:00
|
|
|
return [].concat(...this.or.map(subkeys => subkeys.usedKeys()));
|
2021-02-20 16:48:42 +01:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-29 13:16:21 +02:00
|
|
|
export class And extends TagsFilter {
|
2020-06-24 00:35:19 +02:00
|
|
|
public and: TagsFilter[]
|
|
|
|
|
|
|
|
constructor(and: TagsFilter[]) {
|
2020-07-16 14:21:06 +02:00
|
|
|
super();
|
2020-06-24 00:35:19 +02:00
|
|
|
this.and = and;
|
|
|
|
}
|
|
|
|
|
2021-01-06 01:11:07 +01:00
|
|
|
private static combine(filter: string, choices: string[]): string[] {
|
|
|
|
const values = [];
|
|
|
|
for (const or of choices) {
|
|
|
|
values.push(filter + or);
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:17:00 +01:00
|
|
|
matchesProperties(tags: any): boolean {
|
2020-06-24 00:35:19 +02:00
|
|
|
for (const tagsFilter of this.and) {
|
2021-03-22 02:17:00 +01:00
|
|
|
if (!tagsFilter.matchesProperties(tags)) {
|
2020-06-24 00:35:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
asOverpass(): string[] {
|
2020-08-30 01:13:18 +02:00
|
|
|
let allChoices: string[] = null;
|
2020-06-24 00:35:19 +02:00
|
|
|
for (const andElement of this.and) {
|
2020-08-30 01:13:18 +02:00
|
|
|
const andElementFilter = andElement.asOverpass();
|
2020-06-24 00:35:19 +02:00
|
|
|
if (allChoices === null) {
|
|
|
|
allChoices = andElementFilter;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
const newChoices: string[] = [];
|
|
|
|
for (const choice of allChoices) {
|
2020-06-24 00:35:19 +02:00
|
|
|
newChoices.push(
|
2020-08-30 01:13:18 +02:00
|
|
|
...And.combine(choice, andElementFilter)
|
2020-06-24 00:35:19 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
allChoices = newChoices;
|
|
|
|
}
|
|
|
|
return allChoices;
|
|
|
|
}
|
2020-07-05 18:59:47 +02:00
|
|
|
|
|
|
|
substituteValues(tags: any): TagsFilter {
|
|
|
|
const newChoices = [];
|
|
|
|
for (const c of this.and) {
|
|
|
|
newChoices.push(c.substituteValues(tags));
|
|
|
|
}
|
|
|
|
return new And(newChoices);
|
|
|
|
}
|
2020-08-22 17:33:08 +02:00
|
|
|
|
2020-08-27 18:44:16 +02:00
|
|
|
asHumanString(linkToWiki: boolean, shorten: boolean) {
|
|
|
|
return this.and.map(t => t.asHumanString(linkToWiki, shorten)).join("&");
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-09-08 00:33:05 +02:00
|
|
|
isUsableAsAnswer(): boolean {
|
|
|
|
for (const t of this.and) {
|
2021-01-06 01:11:07 +01:00
|
|
|
if (!t.isUsableAsAnswer()) {
|
2020-09-08 00:33:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
isEquivalent(other: TagsFilter): boolean {
|
2020-10-28 11:19:47 +01:00
|
|
|
if (!(other instanceof And)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-09 18:42:13 +02:00
|
|
|
|
2020-10-28 11:19:47 +01:00
|
|
|
for (const selfTag of this.and) {
|
|
|
|
let matchFound = false;
|
|
|
|
for (let i = 0; i < other.and.length && !matchFound; i++) {
|
|
|
|
let otherTag = other.and[i];
|
|
|
|
matchFound = selfTag.isEquivalent(otherTag);
|
|
|
|
}
|
|
|
|
if (!matchFound) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const selfTag of this.and) {
|
|
|
|
let matchFound = false;
|
|
|
|
for (const otherTag of other.and) {
|
|
|
|
matchFound = selfTag.isEquivalent(otherTag);
|
|
|
|
if (matchFound) {
|
|
|
|
break;
|
2020-09-09 18:42:13 +02:00
|
|
|
}
|
2020-10-28 11:19:47 +01:00
|
|
|
}
|
|
|
|
if (!matchFound) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const otherTag of other.and) {
|
|
|
|
let matchFound = false;
|
|
|
|
for (const selfTag of this.and) {
|
|
|
|
matchFound = selfTag.isEquivalent(otherTag);
|
|
|
|
if (matchFound) {
|
|
|
|
break;
|
2020-09-09 18:42:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 11:19:47 +01:00
|
|
|
if (!matchFound) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-09 18:42:13 +02:00
|
|
|
}
|
2020-10-28 11:19:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2020-09-09 18:42:13 +02:00
|
|
|
}
|
2021-02-20 16:48:42 +01:00
|
|
|
|
|
|
|
usedKeys(): string[] {
|
2021-03-14 01:40:35 +01:00
|
|
|
return [].concat(...this.and.map(subkeys => subkeys.usedKeys()));
|
2021-02-20 16:48:42 +01:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:16:21 +02:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
export class TagUtils {
|
2020-07-05 18:59:47 +02:00
|
|
|
static ApplyTemplate(template: string, tags: any): string {
|
|
|
|
for (const k in tags) {
|
|
|
|
while (template.indexOf("{" + k + "}") >= 0) {
|
2020-07-29 19:43:15 +02:00
|
|
|
const escaped = tags[k].replace(/</g, '<').replace(/>/g, '>');
|
|
|
|
template = template.replace("{" + k + "}", escaped);
|
2020-07-05 18:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return template;
|
|
|
|
}
|
2020-08-22 02:12:46 +02:00
|
|
|
|
|
|
|
static KVtoProperties(tags: Tag[]): any {
|
|
|
|
const properties = {};
|
|
|
|
for (const tag of tags) {
|
|
|
|
properties[tag.key] = tag.value
|
|
|
|
}
|
|
|
|
return properties;
|
|
|
|
}
|
2020-09-10 19:33:06 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-27 14:13:37 +01:00
|
|
|
* Given two hashes of {key --> values[]}, makes sure that every neededTag is present in availableTags
|
|
|
|
*/
|
2021-01-06 01:11:07 +01:00
|
|
|
static AllKeysAreContained(availableTags: any, neededTags: any) {
|
2020-10-27 14:13:37 +01:00
|
|
|
for (const neededKey in neededTags) {
|
2021-01-06 01:11:07 +01:00
|
|
|
const availableValues: string[] = availableTags[neededKey]
|
|
|
|
if (availableValues === undefined) {
|
2020-10-27 14:13:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
const neededValues: string[] = neededTags[neededKey];
|
2020-10-27 14:13:37 +01:00
|
|
|
for (const neededValue of neededValues) {
|
2020-10-28 11:19:47 +01:00
|
|
|
if (availableValues.indexOf(neededValue) < 0) {
|
2020-10-27 14:13:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Creates a hash {key --> [values]}, with all the values present in the tagsfilter
|
2021-01-06 01:11:07 +01:00
|
|
|
*
|
2020-09-10 19:33:06 +02:00
|
|
|
* @param tagsFilters
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-01-06 01:11:07 +01:00
|
|
|
static SplitKeys(tagsFilters: TagsFilter[]) {
|
2020-09-10 19:33:06 +02:00
|
|
|
const keyValues = {} // Map string -> string[]
|
2020-10-27 01:01:34 +01:00
|
|
|
tagsFilters = [...tagsFilters] // copy all
|
2020-09-10 19:33:06 +02:00
|
|
|
while (tagsFilters.length > 0) {
|
2020-10-27 01:01:34 +01:00
|
|
|
// Queue
|
|
|
|
const tagsFilter = tagsFilters.shift();
|
2020-09-10 19:33:06 +02:00
|
|
|
|
|
|
|
if (tagsFilter === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tagsFilter instanceof And) {
|
|
|
|
tagsFilters.push(...tagsFilter.and);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tagsFilter instanceof Tag) {
|
|
|
|
if (keyValues[tagsFilter.key] === undefined) {
|
|
|
|
keyValues[tagsFilter.key] = [];
|
|
|
|
}
|
|
|
|
keyValues[tagsFilter.key].push(...tagsFilter.value.split(";"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error("Invalid type to flatten the multiAnswer", tagsFilter);
|
|
|
|
throw "Invalid type to FlattenMultiAnswer"
|
|
|
|
}
|
2020-10-27 14:13:37 +01:00
|
|
|
return keyValues;
|
|
|
|
}
|
2021-01-06 01:11:07 +01:00
|
|
|
|
2020-10-27 14:13:37 +01:00
|
|
|
/**
|
|
|
|
* Given multiple tagsfilters which can be used as answer, will take the tags with the same keys together as set.
|
|
|
|
* E.g:
|
|
|
|
*
|
|
|
|
* FlattenMultiAnswer([and: [ "x=a", "y=0;1"], and: ["x=b", "y=2"], and: ["x=", "y=3"]])
|
|
|
|
* will result in
|
|
|
|
* ["x=a;b", "y=0;1;2;3"]
|
|
|
|
*
|
|
|
|
* @param tagsFilters
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
static FlattenMultiAnswer(tagsFilters: TagsFilter[]): And {
|
|
|
|
if (tagsFilters === undefined) {
|
|
|
|
return new And([]);
|
|
|
|
}
|
2020-09-10 19:33:06 +02:00
|
|
|
|
2020-10-27 14:13:37 +01:00
|
|
|
let keyValues = TagUtils.SplitKeys(tagsFilters);
|
2020-09-10 19:33:06 +02:00
|
|
|
const and: TagsFilter[] = []
|
|
|
|
for (const key in keyValues) {
|
|
|
|
and.push(new Tag(key, Utils.Dedup(keyValues[key]).join(";")));
|
|
|
|
}
|
|
|
|
return new And(and);
|
|
|
|
}
|
|
|
|
|
2021-01-06 01:11:07 +01:00
|
|
|
static MatchesMultiAnswer(tag: TagsFilter, tags: any): boolean {
|
|
|
|
const splitted = TagUtils.SplitKeys([tag]);
|
|
|
|
for (const splitKey in splitted) {
|
|
|
|
const neededValues = splitted[splitKey];
|
2021-02-20 16:48:42 +01:00
|
|
|
if (tags[splitKey] === undefined) {
|
2021-01-06 02:09:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-20 16:48:42 +01:00
|
|
|
|
2021-01-06 01:11:07 +01:00
|
|
|
const actualValue = tags[splitKey].split(";");
|
|
|
|
for (const neededValue of neededValues) {
|
|
|
|
if (actualValue.indexOf(neededValue) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|