Implementing the OSM tag format definitions library

This commit is contained in:
Eric van der Vlist 2023-06-12 16:29:17 +02:00
parent 3f56cf9c23
commit 3fba5b0529
2 changed files with 59 additions and 11 deletions

View File

@ -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,
});
});

View File

@ -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 = <TagStatus>(
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;
};