]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/helpers.js
Merge remote-tracking branch 'upstream/master'
[nominatim-ui.git] / src / lib / helpers.js
1 import escapeHtml from 'escape-html';
2
3 export function formatOSMType(sType, bExcludeExternal) {
4   if (sType === 'N') return 'node';
5   if (sType === 'W') return 'way';
6   if (sType === 'R') return 'relation';
7
8   if (!bExcludeExternal) return '';
9
10   if (sType === 'T') return 'way';
11   if (sType === 'I') return 'way';
12
13   return '';
14 }
15
16 // https://www.openstreetmap.org/relation/123 => ['R', 123]
17 // w123 => ['W', 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])];
23 }
24
25 export function formatLabel(aPlace) {
26   if (aPlace.label) return aPlace.label;
27
28   function capitalize(s) {
29     return s && s[0].toUpperCase() + s.slice(1);
30   }
31
32   if (aPlace.type && aPlace.type === 'yes' && aPlace.category) {
33     return capitalize(aPlace.category.replace(/_/g, ' '));
34   }
35   if (aPlace.type) {
36     return capitalize(aPlace.type.replace(/_/g, ' '));
37   }
38   return '';
39 }
40
41 export function coverageType(aPlace) {
42   return (aPlace.isarea ? 'Polygon' : 'Point');
43 }
44
45 export function isAdminBoundary(aPlace) {
46   return aPlace.category === 'boundary' && aPlace.type === 'administrative';
47 }
48
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';
65   return 'other';
66 }
67
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 + ')';
72   }
73   return escapeHtml(sOut);
74 }
75
76 // Any over 15 are invalid data in OSM anyway
77 export function formatAdminLevel(iLevel) {
78   return (iLevel && iLevel < 15 ? iLevel : '');
79 }
80
81 export function formatDistance(fDistance, bInMeters) {
82   if (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';
87
88     return '<abbr class="distance" title="' + fDistance + ' meters">~' + sFormatted + '</abbr>';
89   }
90
91   // spheric distance, http://postgis.net/docs/ST_Distance_Spheroid.html
92   if (fDistance === 0) return '0';
93
94   return '<abbr class="distance" title="spheric distance ' + fDistance + '">~'
95       + (Math.round(fDistance * 1000, 4) / 1000)
96       + '</abbr>';
97 }
98
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);
102 }
103
104 export function zoomLevels() {
105   const aZoomLevels = [
106     /*  0 */ 'Continent / Sea',
107     /*  1 */ '',
108     /*  2 */ '',
109     /*  3 */ 'Country',
110     /*  4 */ '',
111     /*  5 */ 'State',
112     /*  6 */ 'Region',
113     /*  7 */ '',
114     /*  8 */ 'County',
115     /*  9 */ '',
116     /* 10 */ 'City',
117     /* 11 */ '',
118     /* 12 */ 'Town / Borough',
119     /* 13 */ 'Village / Suburb',
120     /* 14 */ 'Neighbourhood',
121     /* 15 */ 'Locality',
122     /* 16 */ 'Major Street',
123     /* 17 */ 'Minor Street',
124     /* 18 */ 'Building'
125   ];
126   return aZoomLevels;
127 }