More refactoring of the tests

This commit is contained in:
Pieter Vander Vennet 2022-03-18 13:04:12 +01:00
parent 308ab74a08
commit 97c88af619
11 changed files with 199 additions and 247 deletions

View file

@ -2,9 +2,14 @@ import {TagsFilter} from "./TagsFilter";
import {Or} from "./Or";
import {TagUtils} from "./TagUtils";
// @ts-ignore
import {Tag} from "./Tag";// needed for tests
// @ts-ignore
import {RegexTag} from "./RegexTag";// needed for tests
export class And extends TagsFilter {
public and: TagsFilter[]
constructor(and: TagsFilter[]) {
super();
this.and = and
@ -40,6 +45,10 @@ export class And extends TagsFilter {
return true;
}
/**
* const and = new And([new Tag("boundary","protected_area"), new RegexTag("protect_class","98",true)])
* and.asOverpass() // => [ "[\"boundary\"=\"protected_area\"][\"protect_class\"!~\"^98$\"]" ]
*/
asOverpass(): string[] {
let allChoices: string[] = null;
for (const andElement of this.and) {
@ -73,6 +82,23 @@ export class And extends TagsFilter {
return true;
}
/**
* const t0 = new And([
* new Tag("valves:special", "A"),
* new Tag("valves", "A")
* ])
* const t1 = new And([new Tag("valves", "A")])
* const t2 = new And([new Tag("valves", "B")])
* t0.isEquivalent(t0) // => true
* t1.isEquivalent(t1) // => true
* t2.isEquivalent(t2) // => true
* t0.isEquivalent(t1) // => false
* t0.isEquivalent(t2) // => false
* t1.isEquivalent(t0) // => false
* t1.isEquivalent(t2) // => false
* t2.isEquivalent(t0) // => false
* t2.isEquivalent(t1) // => false
*/
isEquivalent(other: TagsFilter): boolean {
if (!(other instanceof And)) {
return false;

View file

@ -2,6 +2,10 @@ import {TagsFilter} from "./TagsFilter";
import {TagUtils} from "./TagUtils";
import {And} from "./And";
// @ts-ignore
import {Tag} from "./Tag";// needed for tests
// @ts-ignore
import {RegexTag} from "./RegexTag";// needed for tests
export class Or extends TagsFilter {
public or: TagsFilter[]
@ -21,6 +25,15 @@ export class Or extends TagsFilter {
return false;
}
/**
* const and = new And([new Tag("boundary","protected_area"), new RegexTag("protect_class","98",true)])
* const or = new Or([and, new Tag("leisure", "nature_reserve"])
* or.asOverpass() // => [ "[\"boundary\"=\"protected_area\"][\"protect_class\"!~\"^98$\"]", "[\"leisure\"=\"nature_reserve\"]" ]
*
* // should fuse nested ors into a single list
* const or = new Or([new Tag("key","value"), new Or([new Tag("key1","value1"), new Tag("key2","value2")])])
* or.asOverpass() // => [ `["key"="value"]`, `["key1"="value1"]`, `["key2"="value2"]` ]
*/
asOverpass(): string[] {
const choices = [];
for (const tagsFilter of this.or) {

View file

@ -42,6 +42,12 @@ export class RegexTag extends TagsFilter {
return r.source;
}
/**
* new RegexTag("a", /^[xyz]$/).asOverpass() // => [ `["a"~"^[xyz]$"]` ]
*
* // A wildcard regextag should only give the key
* new RegexTag("a", /^..*$/).asOverpass() // => [ `["a"]` ]
*/
asOverpass(): string[] {
const inv =this.invert ? "!" : ""
if (typeof this.key !== "string") {
@ -89,7 +95,29 @@ export class RegexTag extends TagsFilter {
* notRegex.matchesProperties({"x": "z"}) // => true
* notRegex.matchesProperties({"x": ""}) // => true
* notRegex.matchesProperties({}) // => true
*
* const bicycleTubeRegex = new RegexTag("vending", /^.*bicycle_tube.*$/)
* bicycleTubeRegex.matchesProperties({"vending": "bicycle_tube"}) // => true
* bicycleTubeRegex.matchesProperties({"vending": "something;bicycle_tube"}) // => true
* bicycleTubeRegex.matchesProperties({"vending": "bicycle_tube;something"}) // => true
* bicycleTubeRegex.matchesProperties({"vending": "xyz;bicycle_tube;something"}) // => true
*
* const nameStartsWith = new RegexTag("name", /^[sS]peelbox.*$/)
* nameStartsWith.matchesProperties({"name": "Speelbos Sint-Anna"} => true
* nameStartsWith.matchesProperties({"name": "speelbos Sint-Anna"} => true
* nameStartsWith.matchesProperties({"name": "Sint-Anna"} => false
* nameStartsWith.matchesProperties({"name": ""} => false
*
* const notEmptyList = new RegexTag("xyz", /^\[\]$/, true)
* notEmptyList.matchesProperties({"xyz": undefined}) // => true
* notEmptyList.matchesProperties({"xyz": "[]"}) // => false
* notEmptyList.matchesProperties({"xyz": "[\"abc\"]"}) // => true
*
* const importMatch = new RegexTag("tags", /(^|.*;)amenity=public_bookcase($|;.*)/)
* importMatch.matchesProperties({"tags": "amenity=public_bookcase;name=test"}) // =>true
* importMatch.matchesProperties({"tags": "amenity=public_bookcase"}) // =>true
* importMatch.matchesProperties({"tags": "name=test;amenity=public_bookcase"}) // =>true
* importMatch.matchesProperties({"tags": "amenity=bench"}) // =>false
*/
matchesProperties(tags: any): boolean {
if (typeof this.key === "string") {

View file

@ -46,6 +46,14 @@ export default class SubstitutingTag implements TagsFilter {
return !this._invert;
}
/**
* const assign = new SubstitutingTag("survey:date", "{_date:now}")
* assign.matchesProperties({"survey:date": "2021-03-29", "_date:now": "2021-03-29"}) // => true
* assign.matchesProperties({"survey:date": "2021-03-29", "_date:now": "2021-01-01"}) // => false
* assign.matchesProperties({"survey:date": "2021-03-29"}) // => false
* assign.matchesProperties({"_date:now": "2021-03-29"}) // => false
* assign.matchesProperties({"some_key": "2021-03-29"}) // => false
*/
matchesProperties(properties: any): boolean {
const value = properties[this._key];
if (value === undefined || value === "") {

View file

@ -167,6 +167,21 @@ export class TagUtils {
return new Tag(tag[0], tag[1]);
}
/**
* Parses a tag configuration (a json) into a TagsFilter
*
* TagUtils.Tag("key=value") // => new Tag("key", "value")
* TagUtils.Tag("key=") // => new Tag("key", "")
* TagUtils.Tag("key!=") // => new RegexTag("key", "^..*$", true)
* TagUtils.Tag("key!=value") // => new RegexTag("key", /^value$/, true)
* TagUtils.Tag("vending~.*bicycle_tube.*") // => new RegexTag("vending", /^.*bicycle_tube.*$/)
* TagUtils.Tag("x!~y") // => new RegexTag("x", /^y$/, true)
* TagUtils.Tag({"and": ["key=value", "x=y"]}) // => new And([new Tag("key","value"), new Tag("x","y")])
* TagUtils.Tag("name~[sS]peelbos.*") // => new RegexTag("name", /^[sS]peelbos.*$/)
* TagUtils.Tag("survey:date:={_date:now}") // => new SubstitutingTag("survey:date", "{_date:now}")
* TagUtils.Tag("xyz!~\\[\\]") // => new RegexTag("xyz", /^\[\]$/, true)
* TagUtils.Tag("tags~(^|.*;)amenity=public_bookcase($|;.*)") // => new RegexTag("tags", /(^|.*;)amenity=public_bookcase($|;.*)/)
*/
public static Tag(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter {
try {
return this.TagUnsafe(json, context);