forked from MapComplete/MapComplete
More work on the charging stations theme
This commit is contained in:
parent
c9f2079501
commit
7e2744576c
10 changed files with 476 additions and 97 deletions
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue