From 4aefd43be7e665913d9ce5445f6b9c41f33e8775 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 5 Mar 2024 00:16:17 +0100 Subject: [PATCH] Typing: add 'readonly' to tag method, add 'applyOn' convenience method --- src/Logic/Tags/And.ts | 2 +- src/Logic/Tags/ComparingTag.ts | 5 +++-- src/Logic/Tags/Or.ts | 2 +- src/Logic/Tags/SubstitutingTag.ts | 5 +++-- src/Logic/Tags/TagsFilter.ts | 19 ++++++++++++++----- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Logic/Tags/And.ts b/src/Logic/Tags/And.ts index 88be5afcc..57c6ac3ba 100644 --- a/src/Logic/Tags/And.ts +++ b/src/Logic/Tags/And.ts @@ -159,7 +159,7 @@ export class And extends TagsFilter { return [].concat(...this.and.map((subkeys) => subkeys.usedTags())) } - asChange(properties: Record): { k: string; v: string }[] { + asChange(properties: Readonly>): { k: string; v: string }[] { const result = [] for (const tagsFilter of this.and) { result.push(...tagsFilter.asChange(properties)) diff --git a/src/Logic/Tags/ComparingTag.ts b/src/Logic/Tags/ComparingTag.ts index 861507c95..42a26c497 100644 --- a/src/Logic/Tags/ComparingTag.ts +++ b/src/Logic/Tags/ComparingTag.ts @@ -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): { k: string; v: string }[] { + asChange(_: Readonly>): { k: string; v: string }[] { throw "A comparable tag can not be used to be uploaded to OSM" } diff --git a/src/Logic/Tags/Or.ts b/src/Logic/Tags/Or.ts index bd19c0794..871361f67 100644 --- a/src/Logic/Tags/Or.ts +++ b/src/Logic/Tags/Or.ts @@ -96,7 +96,7 @@ export class Or extends TagsFilter { return [].concat(...this.or.map((subkeys) => subkeys.usedTags())) } - asChange(properties: Record): { k: string; v: string }[] { + asChange(properties: Readonly>): { k: string; v: string }[] { const result = [] for (const tagsFilter of this.or) { result.push(...tagsFilter.asChange(properties)) diff --git a/src/Logic/Tags/SubstitutingTag.ts b/src/Logic/Tags/SubstitutingTag.ts index bf13dc7c3..acdda6835 100644 --- a/src/Logic/Tags/SubstitutingTag.ts +++ b/src/Logic/Tags/SubstitutingTag.ts @@ -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): { k: string; v: string }[] { + asChange(properties: Readonly>): { k: string; v: string }[] { if (this._invert) { throw "An inverted substituting tag can not be used to create a change" } diff --git a/src/Logic/Tags/TagsFilter.ts b/src/Logic/Tags/TagsFilter.ts index c75b7f6f4..5c6070755 100644 --- a/src/Logic/Tags/TagsFilter.ts +++ b/src/Logic/Tags/TagsFilter.ts @@ -15,9 +15,9 @@ export abstract class TagsFilter { abstract matchesProperties(properties: Record): boolean abstract asHumanString( - linkToWiki: boolean, - shorten: boolean, - properties: Record + linkToWiki?: boolean, + shorten?: boolean, + properties?: Record ): 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): { k: string; v: string }[] + abstract asChange(properties: Readonly>): { k: string; v: string }[] + + public applyOn(properties: Readonly>): Record { + 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