X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/bd736ef3e4a2554147cc49a55537052489ed7bfe..23ad83a45b7cb0fb0c07109230e869ab2cb7b3b5:/src/lib/helpers.js?ds=inline diff --git a/src/lib/helpers.js b/src/lib/helpers.js index de1cfd4..0099d83 100644 --- a/src/lib/helpers.js +++ b/src/lib/helpers.js @@ -1,19 +1,6 @@ -module.exports.formatOSMType = formatOSMType; -module.exports.osmLink = osmLink; -module.exports.formatLabel = formatLabel; -module.exports.wikipediaLink = wikipediaLink; -module.exports.coverageType = coverageType; -module.exports.isAdminBoundary = isAdminBoundary; -module.exports.formatAddressRank = formatAddressRank; -module.exports.formatPlaceType = formatPlaceType; -module.exports.formatAdminLevel = formatAdminLevel; -module.exports.formatDistance = formatDistance; -module.exports.formatKeywordToken = formatKeywordToken; -module.exports.zoomLevels = zoomLevels; - -const escapeHtml = require('escape-html'); - -function formatOSMType(sType, bExcludeExternal) { +import escapeHtml from 'escape-html'; + +export function formatOSMType(sType, bExcludeExternal) { if (sType === 'N') return 'node'; if (sType === 'W') return 'way'; if (sType === 'R') return 'relation'; @@ -26,7 +13,16 @@ function formatOSMType(sType, bExcludeExternal) { return ''; } -function osmLink(aPlace) { +// https://www.openstreetmap.org/relation/123 => ['R', 123] +// w123 => ['W', 123] +export function identifyLinkInQuery(query) { + if (!query) return undefined; + const m = query.match(/\/(relation|way|node)\/(-?\d+)/) || query.match(/^([nwr])(-?\d+)$/i); + if (!m) return undefined; + return [m[1][0].toUpperCase(), Number(m[2])]; +} + +export function osmLink(aPlace) { if (!aPlace.osm_type) return ''; var sOSMType = formatOSMType(aPlace.osm_type, false); if (!sOSMType) return ''; @@ -34,7 +30,7 @@ function osmLink(aPlace) { return '' + sOSMType + ' ' + aPlace.osm_id + ''; } -function formatLabel(aPlace) { +export function formatLabel(aPlace) { if (aPlace.label) return aPlace.label; function capitalize(s) { @@ -51,7 +47,7 @@ function formatLabel(aPlace) { } /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */ -function wikipediaLink(aPlace) { +export function wikipediaLink(aPlace) { if (!aPlace.calculated_wikipedia) return ''; var parts = aPlace.calculated_wikipedia.split(':', 2); @@ -63,15 +59,15 @@ function wikipediaLink(aPlace) { return '' + sTitle + ''; } -function coverageType(aPlace) { +export function coverageType(aPlace) { return (aPlace.isarea ? 'Polygon' : 'Point'); } -function isAdminBoundary(aPlace) { +export function isAdminBoundary(aPlace) { return aPlace.category === 'boundary' && aPlace.type === 'administrative'; } -function formatAddressRank(iRank) { +export function formatAddressRank(iRank) { if (iRank < 4) return 'other'; if (iRank < 6) return 'country'; if (iRank < 8) return 'region'; @@ -90,7 +86,7 @@ function formatAddressRank(iRank) { return 'other'; } -function formatPlaceType(aPlace) { +export function formatPlaceType(aPlace) { var sOut = aPlace.class + ':' + aPlace.type; if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) { sOut = sOut + ' (' + aPlace.place_type + ')'; @@ -99,11 +95,11 @@ function formatPlaceType(aPlace) { } // Any over 15 are invalid data in OSM anyway -function formatAdminLevel(iLevel) { +export function formatAdminLevel(iLevel) { return (iLevel && iLevel < 15 ? iLevel : ''); } -function formatDistance(fDistance, bInMeters) { +export function formatDistance(fDistance, bInMeters) { if (bInMeters) { if (fDistance < 1) return '0'; var sFormatted = (fDistance >= 1000) @@ -122,11 +118,11 @@ function formatDistance(fDistance, bInMeters) { } // mark partial tokens (those starting with a space) with a star for readability -function formatKeywordToken(sToken) { +export function formatKeywordToken(sToken) { return (sToken[0] === ' ' ? '*' : '') + escapeHtml(sToken); } -function zoomLevels() { +export function zoomLevels() { const aZoomLevels = [ /* 0 */ 'Continent / Sea', /* 1 */ '', @@ -140,16 +136,13 @@ function zoomLevels() { /* 9 */ '', /* 10 */ 'City', /* 11 */ '', - /* 12 */ 'Town / Village', - /* 13 */ '', - /* 14 */ 'Suburb', - /* 15 */ '', - /* 16 */ 'Street', - /* 17 */ '', - /* 18 */ 'Building', - /* 19 */ '', - /* 20 */ '', - /* 21 */ '' + /* 12 */ 'Town / Borough', + /* 13 */ 'Village / Suburb', + /* 14 */ 'Neighbourhood', + /* 15 */ 'Locality', + /* 16 */ 'Major Street', + /* 17 */ 'Minor Street', + /* 18 */ 'Building' ]; return aZoomLevels; }