Typing: add 'readonly' to tag method, add 'applyOn' convenience method

This commit is contained in:
Pieter Vander Vennet 2024-03-05 00:16:17 +01:00
parent 5215662a0c
commit 4aefd43be7
5 changed files with 22 additions and 11 deletions

View file

@ -159,7 +159,7 @@ export class And extends TagsFilter {
return [].concat(...this.and.map((subkeys) => subkeys.usedTags()))
}
asChange(properties: Record<string, string>): { k: string; v: string }[] {
asChange(properties: Readonly<Record<string, string>>): { k: string; v: string }[] {
const result = []
for (const tagsFilter of this.and) {
result.push(...tagsFilter.asChange(properties))

View file

@ -3,7 +3,7 @@ import { TagConfigJson } from "../../Models/ThemeConfig/Json/TagConfigJson"
import { Tag } from "./Tag"
import { ExpressionSpecification } from "maplibre-gl"
export default class ComparingTag implements TagsFilter {
export default class ComparingTag extends TagsFilter {
private readonly _key: string
private readonly _predicate: (value: string) => boolean
private readonly _representation: "<" | ">" | "<=" | ">="
@ -15,13 +15,14 @@ export default class ComparingTag implements TagsFilter {
representation: "<" | ">" | "<=" | ">=",
boundary: string
) {
super()
this._key = key
this._predicate = predicate
this._representation = representation
this._boundary = boundary
}
asChange(_: Record<string, string>): { k: string; v: string }[] {
asChange(_: Readonly<Record<string, string>>): { k: string; v: string }[] {
throw "A comparable tag can not be used to be uploaded to OSM"
}

View file

@ -96,7 +96,7 @@ export class Or extends TagsFilter {
return [].concat(...this.or.map((subkeys) => subkeys.usedTags()))
}
asChange(properties: Record<string, string>): { k: string; v: string }[] {
asChange(properties: Readonly<Record<string, string>>): { k: string; v: string }[] {
const result = []
for (const tagsFilter of this.or) {
result.push(...tagsFilter.asChange(properties))

View file

@ -13,12 +13,13 @@ import { ExpressionSpecification } from "maplibre-gl"
* The 'key' is always fixed and should not contain substitutions.
* This cannot be used to query features
*/
export default class SubstitutingTag implements TagsFilter {
export default class SubstitutingTag extends TagsFilter {
private readonly _key: string
private readonly _value: string
private readonly _invert: boolean
constructor(key: string, value: string, invert = false) {
super()
this._key = key
this._value = value
this._invert = invert
@ -99,7 +100,7 @@ export default class SubstitutingTag implements TagsFilter {
return []
}
asChange(properties: Record<string, string>): { k: string; v: string }[] {
asChange(properties: Readonly<Record<string, string>>): { k: string; v: string }[] {
if (this._invert) {
throw "An inverted substituting tag can not be used to create a change"
}

View file

@ -15,9 +15,9 @@ export abstract class TagsFilter {
abstract matchesProperties(properties: Record<string, string>): boolean
abstract asHumanString(
linkToWiki: boolean,
shorten: boolean,
properties: Record<string, string>
linkToWiki?: boolean,
shorten?: boolean,
properties?: Record<string, string>
): string
abstract asJson(): TagConfigJson
@ -34,9 +34,18 @@ export abstract class TagsFilter {
* Converts the tagsFilter into a list of key-values that should be uploaded to OSM.
* Throws an error if not applicable.
*
* Note: properties are the already existing tags-object. It is only used in the substituting tag
* @param properties are the already existing tags-object. It is only used in the substituting tag and will not be changed
*/
abstract asChange(properties: Record<string, string>): { k: string; v: string }[]
abstract asChange(properties: Readonly<Record<string, string>>): { k: string; v: string }[]
public applyOn(properties: Readonly<Record<string, string>>): Record<string, string> {
const copy = { ...properties }
const changes = this.asChange(properties)
for (const { k, v } of changes) {
copy[k] = v
}
return copy
}
/**
* Returns an optimized version (or self) of this tagsFilter