1 import escapeHtml from 'escape-html';
3 export function formatOSMType(sType, bExcludeExternal) {
4 if (sType === 'N') return 'node';
5 if (sType === 'W') return 'way';
6 if (sType === 'R') return 'relation';
8 if (!bExcludeExternal) return '';
10 if (sType === 'T') return 'way';
11 if (sType === 'I') return 'way';
16 // https://www.openstreetmap.org/relation/123 => ['R', 123]
18 export function identifyLinkInQuery(query) {
19 if (!query) return undefined;
20 const m = query.match(/\/(relation|way|node)\/(-?\d+)/) || query.match(/^([nwr])(-?\d+)$/i);
21 if (!m) return undefined;
22 return [m[1][0].toUpperCase(), Number(m[2])];
25 export function formatLabel(aPlace) {
26 if (aPlace.label) return aPlace.label;
28 function capitalize(s) {
29 return s && s[0].toUpperCase() + s.slice(1);
32 if (aPlace.type && aPlace.type === 'yes' && aPlace.category) {
33 return capitalize(aPlace.category.replace(/_/g, ' '));
36 return capitalize(aPlace.type.replace(/_/g, ' '));
41 export function coverageType(aPlace) {
42 return (aPlace.isarea ? 'Polygon' : 'Point');
45 export function isAdminBoundary(aPlace) {
46 return aPlace.category === 'boundary' && aPlace.type === 'administrative';
49 export function formatAddressRank(iRank) {
50 if (iRank < 4) return 'other';
51 if (iRank < 6) return 'country';
52 if (iRank < 8) return 'region';
53 if (iRank < 10) return 'state';
54 if (iRank < 12) return 'state district';
55 if (iRank < 14) return 'county';
56 if (iRank < 16) return 'municipality';
57 if (iRank < 18) return 'city / town / village';
58 if (iRank < 20) return 'city / village district';
59 if (iRank < 22) return 'suburb / hamlet';
60 if (iRank < 24) return 'neighbourhood';
61 if (iRank < 26) return 'city block / square';
62 if (iRank === 26) return 'major street';
63 if (iRank === 27) return 'minory street / path';
64 if (iRank <= 30) return 'house / building';
68 export function formatPlaceType(aPlace) {
69 var sOut = aPlace.class + ':' + aPlace.type;
70 if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) {
71 sOut = sOut + ' (' + aPlace.place_type + ')';
73 return escapeHtml(sOut);
76 // Any over 15 are invalid data in OSM anyway
77 export function formatAdminLevel(iLevel) {
78 return (iLevel && iLevel < 15 ? iLevel : '');
81 export function formatDistance(fDistance, bInMeters) {
83 if (fDistance < 1) return '0';
84 var sFormatted = (fDistance >= 1000)
85 ? Math.round(fDistance / 1000, 1) + ' km'
86 : Math.round(fDistance, 0) + ' m';
88 return '<abbr class="distance" title="' + fDistance + ' meters">~' + sFormatted + '</abbr>';
91 // spheric distance, http://postgis.net/docs/ST_Distance_Spheroid.html
92 if (fDistance === 0) return '0';
94 return '<abbr class="distance" title="spheric distance ' + fDistance + '">~'
95 + (Math.round(fDistance * 1000, 4) / 1000)
99 // mark partial tokens (those starting with a space) with a star for readability
100 export function formatKeywordToken(sToken) {
101 return (sToken[0] === ' ' ? '*' : '') + escapeHtml(sToken);
104 export function zoomLevels() {
105 const aZoomLevels = [
106 /* 0 */ 'Continent / Sea',
118 /* 12 */ 'Town / Borough',
119 /* 13 */ 'Village / Suburb',
120 /* 14 */ 'Neighbourhood',
122 /* 16 */ 'Major Street',
123 /* 17 */ 'Minor Street',