More work on the charging stations theme

This commit is contained in:
Pieter Vander Vennet 2021-09-07 21:03:29 +02:00
parent c9f2079501
commit 7e2744576c
10 changed files with 476 additions and 97 deletions

View file

@ -2,10 +2,10 @@ import {Tag} from "./Tag";
import {TagsFilter} from "./TagsFilter";
export class RegexTag extends TagsFilter {
private readonly key: RegExp | string;
private readonly value: RegExp | string;
private readonly invert: boolean;
private readonly matchesEmpty: boolean
public readonly key: RegExp | string;
public readonly value: RegExp | string;
public readonly invert: boolean;
public readonly matchesEmpty: boolean
constructor(key: string | RegExp, value: RegExp | string, invert: boolean = false) {
super();

View file

@ -7,6 +7,7 @@ import {RegexTag} from "./RegexTag";
import SubstitutingTag from "./SubstitutingTag";
import {Or} from "./Or";
import {AndOrTagConfigJson} from "../../Models/ThemeConfig/Json/TagConfigJson";
import {isRegExp} from "util";
export class TagUtils {
static ApplyTemplate(template: string, tags: any): string {
@ -47,16 +48,15 @@ export class TagUtils {
}
/***
* Creates a hash {key --> [values]}, with all the values present in the tagsfilter
* Creates a hash {key --> [values : string | Regex ]}, with all the values present in the tagsfilter
*
* @param tagsFilters
* @constructor
*/
static SplitKeys(tagsFilters: TagsFilter[]) {
static SplitKeys(tagsFilters: TagsFilter[], allowRegex = false) {
const keyValues = {} // Map string -> string[]
tagsFilters = [...tagsFilters] // copy all
tagsFilters = [...tagsFilters] // copy all, use as queue
while (tagsFilters.length > 0) {
// Queue
const tagsFilter = tagsFilters.shift();
if (tagsFilter === undefined) {
@ -75,6 +75,21 @@ export class TagUtils {
keyValues[tagsFilter.key].push(...tagsFilter.value.split(";"));
continue;
}
if(allowRegex && tagsFilter instanceof RegexTag) {
const key = tagsFilter.key
if(isRegExp(key)) {
console.error("Invalid type to flatten the multiAnswer: key is a regex too", tagsFilter);
throw "Invalid type to FlattenMultiAnswer"
}
const keystr = <string>key
if (keyValues[keystr] === undefined) {
keyValues[keystr ] = [];
}
keyValues[keystr].push(tagsFilter);
continue;
}
console.error("Invalid type to flatten the multiAnswer", tagsFilter);
throw "Invalid type to FlattenMultiAnswer"
@ -106,16 +121,30 @@ export class TagUtils {
return new And(and);
}
static MatchesMultiAnswer(tag: TagsFilter, tags: any): boolean {
const splitted = TagUtils.SplitKeys([tag]);
/**
* Returns true if the properties match the tagsFilter, interpreted as a multikey.
* Note that this might match a regex tag
* @param tag
* @param properties
* @constructor
*/
static MatchesMultiAnswer(tag: TagsFilter, properties: any): boolean {
const splitted = TagUtils.SplitKeys([tag], true);
for (const splitKey in splitted) {
const neededValues = splitted[splitKey];
if (tags[splitKey] === undefined) {
if (properties[splitKey] === undefined) {
return false;
}
const actualValue = tags[splitKey].split(";");
const actualValue = properties[splitKey].split(";");
for (const neededValue of neededValues) {
if(neededValue instanceof RegexTag) {
if(!neededValue.matchesProperties(properties)) {
return false
}
continue
}
if (actualValue.indexOf(neededValue) < 0) {
return false;
}