Skip to content

Commit 44628b1

Browse files
committed
Use Array.map and Array.forEach
1 parent 8943dc4 commit 44628b1

File tree

13 files changed

+224
-304
lines changed

13 files changed

+224
-304
lines changed

src/control.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,7 @@ export class GeocoderControl extends EventedControl {
273273
this._results = results;
274274
L.DomUtil.removeClass(this._alts, 'leaflet-control-geocoder-alternatives-minimized');
275275
L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-options-open');
276-
for (let i = 0; i < results.length; i++) {
277-
this._alts.appendChild(this._createAlt(results[i], i));
278-
}
276+
this._results.forEach((result, i) => this._alts.appendChild(this._createAlt(result, i)));
279277
} else {
280278
L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-options-error');
281279
L.DomUtil.addClass(this._errorElement, 'leaflet-control-geocoder-error');

src/geocoders/arcgis.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { IGeocoder, GeocoderOptions, geocodingParams, GeocodingResult, reversePa
44

55
export interface ArcGisOptions extends GeocoderOptions {}
66

7-
8-
97
/**
108
* Implementation of the [ArcGIS geocoder](https://developers.arcgis.com/features/geocoding/)
119
*/
@@ -33,24 +31,18 @@ export class ArcGis implements IGeocoder {
3331
this.options.serviceUrl + '/findAddressCandidates',
3432
params
3533
);
36-
const results: GeocodingResult[] = [];
37-
if (data.candidates && data.candidates.length) {
38-
for (let i = 0; i <= data.candidates.length - 1; i++) {
39-
const loc = data.candidates[i];
40-
const latLng = L.latLng(loc.location.y, loc.location.x);
41-
const latLngBounds = L.latLngBounds(
42-
L.latLng(loc.extent.ymax, loc.extent.xmax),
43-
L.latLng(loc.extent.ymin, loc.extent.xmin)
44-
);
45-
results[i] = {
46-
name: loc.address,
47-
bbox: latLngBounds,
48-
center: latLng
49-
};
50-
}
51-
}
52-
53-
return results;
34+
return data.candidates.map((loc): GeocodingResult => {
35+
const center = L.latLng(loc.location.y, loc.location.x);
36+
const bbox = L.latLngBounds(
37+
L.latLng(loc.extent.ymax, loc.extent.xmax),
38+
L.latLng(loc.extent.ymin, loc.extent.xmin)
39+
);
40+
return {
41+
name: loc.address,
42+
bbox,
43+
center
44+
};
45+
});
5446
}
5547

5648
suggest(query: string): Promise<GeocodingResult[]> {
@@ -64,18 +56,18 @@ export class ArcGis implements IGeocoder {
6456
f: 'json'
6557
});
6658
const data = await getJSON<any>(this.options.serviceUrl + '/reverseGeocode', params);
67-
const result: GeocodingResult[] = [];
68-
if (data && !data.error) {
69-
const center = L.latLng(data.location.y, data.location.x);
70-
const bbox = L.latLngBounds(center, center);
71-
result.push({
72-
name: data.address.Match_addr,
73-
center: center,
74-
bbox: bbox
75-
});
59+
if (!data || data.error) {
60+
return [];
7661
}
77-
78-
return result;
62+
const center = L.latLng(data.location.y, data.location.x);
63+
const bbox = L.latLngBounds(center, center);
64+
return [
65+
{
66+
name: data.address.Match_addr,
67+
center,
68+
bbox
69+
}
70+
];
7971
}
8072
}
8173

@@ -115,4 +107,3 @@ interface Candidate {
115107
ymax: number;
116108
};
117109
}
118-

src/geocoders/azure.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,16 @@ export class AzureMaps implements IGeocoder {
3838
const url = this.options.serviceUrl + '/address/json';
3939
const data = await getJSON<AzureMapsResponse>(url, params);
4040

41-
const results: GeocodingResult[] = [];
42-
if (data.results && data.results.length > 0) {
43-
for (const result of data.results) {
44-
results.push({
45-
name: result.address.freeformAddress,
46-
bbox: L.latLngBounds(
47-
[result.viewport.topLeftPoint.lat, result.viewport.topLeftPoint.lon],
48-
[result.viewport.btmRightPoint.lat, result.viewport.btmRightPoint.lon]
49-
),
50-
center: L.latLng(result.position.lat, result.position.lon)
51-
});
52-
}
53-
}
54-
return results;
41+
return (data.results || []).map(
42+
(result): GeocodingResult => ({
43+
name: result.address.freeformAddress,
44+
bbox: L.latLngBounds(
45+
[result.viewport.topLeftPoint.lat, result.viewport.topLeftPoint.lon],
46+
[result.viewport.btmRightPoint.lat, result.viewport.btmRightPoint.lon]
47+
),
48+
center: L.latLng(result.position.lat, result.position.lon)
49+
})
50+
);
5551
}
5652

5753
/**
@@ -67,20 +63,16 @@ export class AzureMaps implements IGeocoder {
6763
const url = this.options.serviceUrl + '/address/reverse/json';
6864
const data = await getJSON<any>(url, params);
6965

70-
const results: GeocodingResult[] = [];
71-
if (data.addresses && data.addresses.length > 0) {
72-
for (const address of data.addresses) {
73-
results.push({
74-
name: address.address.freeformAddress,
75-
bbox: L.latLngBounds(
76-
[address.viewport.topLeftPoint.lat, address.viewport.topLeftPoint.lon],
77-
[address.viewport.btmRightPoint.lat, address.viewport.btmRightPoint.lon]
78-
),
79-
center: L.latLng(location.lat, location.lng)
80-
});
81-
}
82-
}
83-
return results;
66+
return (data.addresses || []).map(
67+
(address): GeocodingResult => ({
68+
name: address.address.freeformAddress,
69+
bbox: L.latLngBounds(
70+
[address.viewport.topLeftPoint.lat, address.viewport.topLeftPoint.lon],
71+
[address.viewport.btmRightPoint.lat, address.viewport.btmRightPoint.lon]
72+
),
73+
center: L.latLng(location.lat, location.lng)
74+
})
75+
);
8476
}
8577
}
8678

src/geocoders/bing.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as L from 'leaflet';
22
import { getJSON } from '../util';
3-
import { IGeocoder, GeocoderOptions, geocodingParams, GeocodingResult, reverseParams } from './api';
3+
import { GeocoderOptions, geocodingParams, GeocodingResult, IGeocoder, reverseParams } from './api';
44

55
export interface BingOptions extends GeocoderOptions {}
66

@@ -26,19 +26,7 @@ export class Bing implements IGeocoder {
2626
key: this.options.apiKey
2727
});
2828
const data = await getJSON<any>(this.options.serviceUrl, params);
29-
const results: GeocodingResult[] = [];
30-
if (data.resourceSets.length > 0) {
31-
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
32-
const resource = data.resourceSets[0].resources[i],
33-
bbox = resource.bbox;
34-
results[i] = {
35-
name: resource.name,
36-
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
37-
center: L.latLng(resource.point.coordinates)
38-
};
39-
}
40-
}
41-
return results;
29+
return this._parseResults(data);
4230
}
4331

4432
async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
@@ -49,17 +37,18 @@ export class Bing implements IGeocoder {
4937
this.options.serviceUrl + location.lat + ',' + location.lng,
5038
params
5139
);
52-
const results: GeocodingResult[] = [];
53-
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
54-
const resource = data.resourceSets[0].resources[i],
55-
bbox = resource.bbox;
56-
results[i] = {
40+
return this._parseResults(data);
41+
}
42+
43+
private _parseResults(data) {
44+
return data.resourceSets[0].resources.map((resource): GeocodingResult => {
45+
const bbox = resource.bbox;
46+
return {
5747
name: resource.name,
5848
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
5949
center: L.latLng(resource.point.coordinates)
6050
};
61-
}
62-
return results;
51+
});
6352
}
6453
}
6554

src/geocoders/google.ts

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,7 @@ export class Google implements IGeocoder {
2222
address: query
2323
});
2424
const data = await getJSON<GoogleResponse>(this.options.serviceUrl, params);
25-
const results: GeocodingResult[] = [];
26-
if (data.results && data.results.length) {
27-
for (let i = 0; i <= data.results.length - 1; i++) {
28-
const loc = data.results[i];
29-
const latLng = L.latLng(loc.geometry.location);
30-
const latLngBounds = L.latLngBounds(
31-
L.latLng(loc.geometry.viewport.northeast),
32-
L.latLng(loc.geometry.viewport.southwest)
33-
);
34-
results[i] = {
35-
name: loc.formatted_address,
36-
bbox: latLngBounds,
37-
center: latLng,
38-
properties: loc.address_components
39-
};
40-
}
41-
}
42-
43-
return results;
25+
return this._parseResults(data);
4426
}
4527

4628
async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
@@ -49,25 +31,23 @@ export class Google implements IGeocoder {
4931
latlng: location.lat + ',' + location.lng
5032
});
5133
const data = await getJSON<any>(this.options.serviceUrl, params);
52-
const results: GeocodingResult[] = [];
53-
if (data.results && data.results.length) {
54-
for (let i = 0; i <= data.results.length - 1; i++) {
55-
const loc = data.results[i];
56-
const center = L.latLng(loc.geometry.location);
57-
const bbox = L.latLngBounds(
58-
L.latLng(loc.geometry.viewport.northeast),
59-
L.latLng(loc.geometry.viewport.southwest)
60-
);
61-
results[i] = {
62-
name: loc.formatted_address,
63-
bbox: bbox,
64-
center: center,
65-
properties: loc.address_components
66-
};
67-
}
68-
}
34+
return this._parseResults(data);
35+
}
6936

70-
return results;
37+
private _parseResults(data: GoogleResponse) {
38+
return (data.results || [])?.map((loc): GeocodingResult => {
39+
const center = L.latLng(loc.geometry.location);
40+
const bbox = L.latLngBounds(
41+
L.latLng(loc.geometry.viewport.northeast),
42+
L.latLng(loc.geometry.viewport.southwest)
43+
);
44+
return {
45+
name: loc.formatted_address,
46+
bbox,
47+
center,
48+
properties: loc.address_components
49+
};
50+
});
7151
}
7252
}
7353

src/geocoders/here.ts

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,20 @@ export class HERE implements IGeocoder {
6666

6767
async getJSON(url: string, params: any): Promise<GeocodingResult[]> {
6868
const data = await getJSON<any>(url, params);
69-
const results: GeocodingResult[] = [];
70-
71-
if (data.response.view && data.response.view.length) {
72-
for (let i = 0; i <= data.response.view[0].result.length - 1; i++) {
73-
const loc = data.response.view[0].result[i].location;
74-
const center = L.latLng(loc.displayPosition.latitude, loc.displayPosition.longitude);
75-
const bbox = L.latLngBounds(
76-
L.latLng(loc.mapView.topLeft.latitude, loc.mapView.topLeft.longitude),
77-
L.latLng(loc.mapView.bottomRight.latitude, loc.mapView.bottomRight.longitude)
78-
);
79-
results[i] = {
80-
name: loc.address.label,
81-
properties: loc.address,
82-
bbox: bbox,
83-
center: center
84-
};
85-
}
86-
}
87-
return results;
69+
return (data.response.view?.[0]?.result || []).map((result): GeocodingResult => {
70+
const loc = result.location;
71+
const center = L.latLng(loc.displayPosition.latitude, loc.displayPosition.longitude);
72+
const bbox = L.latLngBounds(
73+
L.latLng(loc.mapView.topLeft.latitude, loc.mapView.topLeft.longitude),
74+
L.latLng(loc.mapView.bottomRight.latitude, loc.mapView.bottomRight.longitude)
75+
);
76+
return {
77+
name: loc.address.label,
78+
properties: loc.address,
79+
bbox,
80+
center
81+
};
82+
});
8883
}
8984
}
9085

@@ -131,34 +126,28 @@ export class HEREv2 implements IGeocoder {
131126

132127
async getJSON(url: string, params: any): Promise<GeocodingResult[]> {
133128
const data = await getJSON<HEREv2Response>(url, params);
134-
const results: GeocodingResult[] = [];
135-
136-
if (data.items && data.items.length) {
137-
for (let i = 0; i <= data.items.length - 1; i++) {
138-
const item = data.items[i];
139-
const latLng = L.latLng(item.position.lat, item.position.lng);
140-
let bbox: L.LatLngBounds;
141-
if (item.mapView) {
142-
bbox = L.latLngBounds(
143-
L.latLng(item.mapView.south, item.mapView.west),
144-
L.latLng(item.mapView.north, item.mapView.east)
145-
);
146-
} else {
147-
// Using only position when not provided
148-
bbox = L.latLngBounds(
149-
L.latLng(item.position.lat, item.position.lng),
150-
L.latLng(item.position.lat, item.position.lng)
151-
);
152-
}
153-
results[i] = {
154-
name: item.address.label,
155-
properties: item.address,
156-
bbox: bbox,
157-
center: latLng
158-
};
129+
return (data.items || []).map((item): GeocodingResult => {
130+
const center = L.latLng(item.position.lat, item.position.lng);
131+
let bbox: L.LatLngBounds;
132+
if (item.mapView) {
133+
bbox = L.latLngBounds(
134+
L.latLng(item.mapView.south, item.mapView.west),
135+
L.latLng(item.mapView.north, item.mapView.east)
136+
);
137+
} else {
138+
// Using only position when not provided
139+
bbox = L.latLngBounds(
140+
L.latLng(item.position.lat, item.position.lng),
141+
L.latLng(item.position.lat, item.position.lng)
142+
);
159143
}
160-
}
161-
return results;
144+
return {
145+
name: item.address.label,
146+
properties: item.address,
147+
bbox,
148+
center
149+
};
150+
});
162151
}
163152
}
164153

0 commit comments

Comments
 (0)