forked from MapComplete/MapComplete
		
	Typing: add 'readonly' to tag method, add 'applyOn' convenience method
This commit is contained in:
		
							parent
							
								
									5215662a0c
								
							
						
					
					
						commit
						4aefd43be7
					
				
					 5 changed files with 22 additions and 11 deletions
				
			
		| 
						 | 
					@ -159,7 +159,7 @@ export class And extends TagsFilter {
 | 
				
			||||||
        return [].concat(...this.and.map((subkeys) => subkeys.usedTags()))
 | 
					        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 = []
 | 
					        const result = []
 | 
				
			||||||
        for (const tagsFilter of this.and) {
 | 
					        for (const tagsFilter of this.and) {
 | 
				
			||||||
            result.push(...tagsFilter.asChange(properties))
 | 
					            result.push(...tagsFilter.asChange(properties))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,7 @@ import { TagConfigJson } from "../../Models/ThemeConfig/Json/TagConfigJson"
 | 
				
			||||||
import { Tag } from "./Tag"
 | 
					import { Tag } from "./Tag"
 | 
				
			||||||
import { ExpressionSpecification } from "maplibre-gl"
 | 
					import { ExpressionSpecification } from "maplibre-gl"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class ComparingTag implements TagsFilter {
 | 
					export default class ComparingTag extends TagsFilter {
 | 
				
			||||||
    private readonly _key: string
 | 
					    private readonly _key: string
 | 
				
			||||||
    private readonly _predicate: (value: string) => boolean
 | 
					    private readonly _predicate: (value: string) => boolean
 | 
				
			||||||
    private readonly _representation: "<" | ">" | "<=" | ">="
 | 
					    private readonly _representation: "<" | ">" | "<=" | ">="
 | 
				
			||||||
| 
						 | 
					@ -15,13 +15,14 @@ export default class ComparingTag implements TagsFilter {
 | 
				
			||||||
        representation: "<" | ">" | "<=" | ">=",
 | 
					        representation: "<" | ">" | "<=" | ">=",
 | 
				
			||||||
        boundary: string
 | 
					        boundary: string
 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
 | 
					        super()
 | 
				
			||||||
        this._key = key
 | 
					        this._key = key
 | 
				
			||||||
        this._predicate = predicate
 | 
					        this._predicate = predicate
 | 
				
			||||||
        this._representation = representation
 | 
					        this._representation = representation
 | 
				
			||||||
        this._boundary = boundary
 | 
					        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"
 | 
					        throw "A comparable tag can not be used to be uploaded to OSM"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,7 +96,7 @@ export class Or extends TagsFilter {
 | 
				
			||||||
        return [].concat(...this.or.map((subkeys) => subkeys.usedTags()))
 | 
					        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 = []
 | 
					        const result = []
 | 
				
			||||||
        for (const tagsFilter of this.or) {
 | 
					        for (const tagsFilter of this.or) {
 | 
				
			||||||
            result.push(...tagsFilter.asChange(properties))
 | 
					            result.push(...tagsFilter.asChange(properties))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,12 +13,13 @@ import { ExpressionSpecification } from "maplibre-gl"
 | 
				
			||||||
 * The 'key' is always fixed and should not contain substitutions.
 | 
					 * The 'key' is always fixed and should not contain substitutions.
 | 
				
			||||||
 * This cannot be used to query features
 | 
					 * 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 _key: string
 | 
				
			||||||
    private readonly _value: string
 | 
					    private readonly _value: string
 | 
				
			||||||
    private readonly _invert: boolean
 | 
					    private readonly _invert: boolean
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructor(key: string, value: string, invert = false) {
 | 
					    constructor(key: string, value: string, invert = false) {
 | 
				
			||||||
 | 
					        super()
 | 
				
			||||||
        this._key = key
 | 
					        this._key = key
 | 
				
			||||||
        this._value = value
 | 
					        this._value = value
 | 
				
			||||||
        this._invert = invert
 | 
					        this._invert = invert
 | 
				
			||||||
| 
						 | 
					@ -99,7 +100,7 @@ export default class SubstitutingTag implements TagsFilter {
 | 
				
			||||||
        return []
 | 
					        return []
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    asChange(properties: Record<string, string>): { k: string; v: string }[] {
 | 
					    asChange(properties: Readonly<Record<string, string>>): { k: string; v: string }[] {
 | 
				
			||||||
        if (this._invert) {
 | 
					        if (this._invert) {
 | 
				
			||||||
            throw "An inverted substituting tag can not be used to create a change"
 | 
					            throw "An inverted substituting tag can not be used to create a change"
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,9 +15,9 @@ export abstract class TagsFilter {
 | 
				
			||||||
    abstract matchesProperties(properties: Record<string, string>): boolean
 | 
					    abstract matchesProperties(properties: Record<string, string>): boolean
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    abstract asHumanString(
 | 
					    abstract asHumanString(
 | 
				
			||||||
        linkToWiki: boolean,
 | 
					        linkToWiki?: boolean,
 | 
				
			||||||
        shorten: boolean,
 | 
					        shorten?: boolean,
 | 
				
			||||||
        properties: Record<string, string>
 | 
					        properties?: Record<string, string>
 | 
				
			||||||
    ): string
 | 
					    ): string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    abstract asJson(): TagConfigJson
 | 
					    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.
 | 
					     * Converts the tagsFilter into a list of key-values that should be uploaded to OSM.
 | 
				
			||||||
     * Throws an error if not applicable.
 | 
					     * 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
 | 
					     * Returns an optimized version (or self) of this tagsFilter
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue