From 3fba5b0529315d42396ecf9685770c5a4dd51a7b Mon Sep 17 00:00:00 2001 From: Eric van der Vlist Date: Mon, 12 Jun 2023 16:29:17 +0200 Subject: [PATCH] Implementing the OSM tag format definitions library --- src/lib/osm/tags.test.ts | 29 ++++++++++++++++++++++++++-- src/lib/osm/tags.ts | 41 +++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/lib/osm/tags.test.ts b/src/lib/osm/tags.test.ts index 6a54167..c637164 100644 --- a/src/lib/osm/tags.test.ts +++ b/src/lib/osm/tags.test.ts @@ -2,9 +2,34 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { getTagStatus } from './tags'; describe('The getTagStatus function', () => { - it('should return false/false for a tag that does NOT exist', () => { + it('should return undefined/false for a tag that does NOT exist', () => { expect(getTagStatus('whatever', {}, '', '')).toEqual({ - selected: false, + highlighted: false, + }); + }); + it('should return definition/true for a tag that exists and is highlighted', () => { + expect( + getTagStatus( + 'whatever', + { hiking: { eating: { whatever: {} } } }, + 'hiking', + 'eating' + ) + ).toEqual({ + definition: {}, + highlighted: true, + }); + }); + it('should return definition/false for a tag that exists and is NOT highlighted', () => { + expect( + getTagStatus( + 'whatever', + { hiking: { sleeping: { whatever: {} } } }, + 'hiking', + 'eating' + ) + ).toEqual({ + definition: {}, highlighted: false, }); }); diff --git a/src/lib/osm/tags.ts b/src/lib/osm/tags.ts index 9d35719..77932a4 100644 --- a/src/lib/osm/tags.ts +++ b/src/lib/osm/tags.ts @@ -1,8 +1,3 @@ -interface TagStatus { - selected: boolean; - highlighted: boolean; -} - interface TagFormatDefinition {} interface TagCategoryDefinitions { @@ -17,15 +12,43 @@ interface TagDefinitions { [key: string]: TagActivityDefinitions; } -export const getTagStatus = ( +interface TagStatus { + definition?: TagFormatDefinition; + highlighted: boolean; +} + +export const getTagStatus = ( tag: string, definitions: TagDefinitions, selectedKey: string, highlightedKey: string ) => { - let status: TagStatus = { - selected: false, + const defaultStatus = { highlighted: false, }; - return status; + const activityDefinitions = definitions[selectedKey]; + if (!activityDefinitions) { + return defaultStatus; + } + + const highlightedDefinitions = activityDefinitions[highlightedKey]; + + if (!!highlightedDefinitions) { + const definition = highlightedDefinitions[tag]; + if (!!definition) { + return { definition, highlighted: true }; + } + } + + for (const category of Object.keys(activityDefinitions)) { + if (category !== highlightedKey) { + const categoryDefinitions = activityDefinitions[category]; + const definition = categoryDefinitions[tag]; + if (!!definition) { + return { definition, highlighted: false }; + } + } + } + + return defaultStatus; };