X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/c9687b102bb6f544a034dc4d508bf0ae4a5fc221..13445adc0d500a8c8285c9c75707f35df89d9d0e:/src/lib/helpers.js diff --git a/src/lib/helpers.js b/src/lib/helpers.js index 2dfb186..dfe4ced 100644 --- a/src/lib/helpers.js +++ b/src/lib/helpers.js @@ -1,21 +1,6 @@ -module.exports.formatOSMType = formatOSMType; -module.exports.formatShortOSMType = formatShortOSMType; -module.exports.osmLink = osmLink; -module.exports.formatLabel = formatLabel; -module.exports.detailsURL = detailsURL; -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'; @@ -28,30 +13,23 @@ function formatOSMType(sType, bExcludeExternal) { return ''; } -function formatShortOSMType(sType) { - if (sType === 'node') return 'N'; - if (sType === 'way') return 'W'; - if (sType === 'relation') return 'R'; - return ''; -} - -function osmLink(aPlace) { +export function osmLink(aPlace) { if (!aPlace.osm_type) return ''; var sOSMType = formatOSMType(aPlace.osm_type, false); if (!sOSMType) return ''; - return '' + sOSMType + ' ' + aPlace.osm_id + '' + return '' + sOSMType + ' ' + aPlace.osm_id + ''; } -function formatLabel(aPlace) { +export function formatLabel(aPlace) { if (aPlace.label) return aPlace.label; function capitalize(s) { return s && s[0].toUpperCase() + s.slice(1); } - if (aPlace.type && aPlace.type === 'yes' && aPlace.class) { - return capitalize(aPlace.class.replace(/_/g, ' ')); + if (aPlace.type && aPlace.type === 'yes' && aPlace.category) { + return capitalize(aPlace.category.replace(/_/g, ' ')); } if (aPlace.type) { return capitalize(aPlace.type.replace(/_/g, ' ')); @@ -59,27 +37,8 @@ function formatLabel(aPlace) { return ''; } -// 'details.html?osmtype=R&osmid=2181874&class=boundary' -function detailsURL(aFeature) { - if (!aFeature) return ''; - - var sOSMType = aFeature.osm_type; - if (sOSMType && sOSMType.length !== 1) { - sOSMType = formatShortOSMType(aFeature.osm_type, false); // node => N - } - if (!sOSMType) return ''; - - var sURL = 'details.html?osmtype=' + sOSMType + '&osmid=' + aFeature.osm_id; - if (aFeature.class) { - sURL = sURL + '&class=' + encodeURIComponent(aFeature.class); - } else if (aFeature.category) { - sURL = sURL + '&class=' +encodeURIComponent(aFeature.category); - } - return sURL; -} - - /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */ -function wikipediaLink(aPlace) { +/* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */ +export function wikipediaLink(aPlace) { if (!aPlace.calculated_wikipedia) return ''; var parts = aPlace.calculated_wikipedia.split(':', 2); @@ -91,15 +50,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'; @@ -118,7 +77,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 + ')'; @@ -127,11 +86,11 @@ function formatPlaceType(aPlace) { } // Any over 15 are invalid data in OSM anyway -function formatAdminLevel(iLevel) { - return (iLevel < 15 ? 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) @@ -150,11 +109,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 */ '', @@ -180,5 +139,4 @@ function zoomLevels() { /* 21 */ '' ]; return aZoomLevels; - -} \ No newline at end of file +}