diff --git a/app/utils/osm.js b/app/utils/osm.js index d28168d..b16295c 100644 --- a/app/utils/osm.js +++ b/app/utils/osm.js @@ -33,27 +33,38 @@ export function getLocalizedName(tags, defaultName = 'Untitled Place') { return defaultName; } +const PLACE_TYPE_KEYS = [ + 'amenity', + 'shop', + 'tourism', + 'historic', + 'leisure', + 'office', + 'craft', + 'building', + 'landuse', + 'public_transport', + 'highway', + 'aeroway', + 'waterway', + 'natural', + 'place', + 'border_type', + 'admin_title', +]; + export function getPlaceType(tags) { if (!tags) return null; - const rawType = - tags.amenity || - tags.shop || - tags.tourism || - tags.historic || - tags.leisure || - tags.office || - tags.craft || - tags.building || - tags.landuse || - tags.place || - tags.natural || - tags.public_transport || - tags.highway || - tags.aeroway || - tags.waterway || - tags.border_type || - tags.admin_title; + for (const key of PLACE_TYPE_KEYS) { + const value = tags[key]; + if (value) { + if (value === 'yes') { + return humanizeOsmTag(key); + } + return humanizeOsmTag(value); + } + } - return humanizeOsmTag(rawType); + return null; } diff --git a/tests/unit/utils/osm-test.js b/tests/unit/utils/osm-test.js new file mode 100644 index 0000000..c2e9314 --- /dev/null +++ b/tests/unit/utils/osm-test.js @@ -0,0 +1,43 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'marco/tests/helpers'; +import { getLocalizedName, getPlaceType } from 'marco/utils/osm'; + +module('Unit | Utility | osm', function (hooks) { + setupTest(hooks); + + test('getLocalizedName returns default name if tags are missing', function (assert) { + const result = getLocalizedName(null); + assert.strictEqual(result, 'Untitled Place'); + }); + + test('getLocalizedName returns name tag', function (assert) { + const tags = { name: 'Foo' }; + const result = getLocalizedName(tags); + assert.strictEqual(result, 'Foo'); + }); + + test('getPlaceType returns value for normal tags', function (assert) { + const tags = { amenity: 'restaurant' }; + const result = getPlaceType(tags); + assert.strictEqual(result, 'Restaurant'); + }); + + test('getPlaceType returns key name if value is "yes"', function (assert) { + const tags = { building: 'yes' }; + const result = getPlaceType(tags); + assert.strictEqual(result, 'Building'); + }); + + test('getPlaceType prioritizes order (amenity > shop > building)', function (assert) { + // If something is both a shop and a building, it should be a shop + const tags = { building: 'yes', shop: 'supermarket' }; + const result = getPlaceType(tags); + assert.strictEqual(result, 'Supermarket'); + }); + + test('getPlaceType returns null if no known type found', function (assert) { + const tags = { foo: 'bar' }; + const result = getPlaceType(tags); + assert.strictEqual(result, null); + }); +});