From 8baac054d56c07a29670cdf60be42190b606d7ad Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Fri, 24 Jan 2025 18:37:09 +0100 Subject: [PATCH] JSON parser: more modern code style --- leaflet-osm.js | 120 +++++++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 79 deletions(-) diff --git a/leaflet-osm.js b/leaflet-osm.js index 4b4c8d7..73bc33e 100644 --- a/leaflet-osm.js +++ b/leaflet-osm.js @@ -322,7 +322,6 @@ L.OSM.XMLParser = { }, getTags: function (xml) { - var result = {}; var tags = xml.getElementsByTagName("tag"); @@ -335,39 +334,29 @@ L.OSM.XMLParser = { } L.OSM.JSONParser = { - getChangesets: function (json) { - var result = []; - - var changesets = json.changeset ? [json.changeset] : []; - - for (var i = 0; i < changesets.length; i++) { - var cs = changesets[i]; - result.push({ - id: cs["id"].toString(), - type: "changeset", - latLngBounds: L.latLngBounds( - [cs["min_lat"], cs["min_lon"]], - [cs["max_lat"], cs["max_lon"]]), - tags: this.getTags(cs) - }); - } - - return result; + getChangesets(json) { + const changesets = json.changeset ? [json.changeset] : []; + + return changesets.map(cs => ({ + id: String(cs.id), + type: "changeset", + latLngBounds: L.latLngBounds( + [cs.min_lat, cs.min_lon], + [cs.max_lat, cs.max_lon] + ), + tags: this.getTags(cs) + })); }, - getNodes: function (json) { - var result = {}; - - var nodes = json.elements?.filter(d => d.type === "node") || []; - for (var i = 0; i < nodes.length; i++) { - var node = nodes[i], id = node["id"]; + getNodes(json) { + const nodes = json.elements?.filter(el => el.type === "node") ?? []; + let result = {}; - result[id] = { - id: id.toString(), + for (const node of nodes) { + result[node.id] = { + id: String(node.id), type: "node", - latLng: L.latLng(node["lat"], - node["lon"], - true), + latLng: L.latLng(node.lat, node.lon, true), tags: this.getTags(node) }; } @@ -375,58 +364,31 @@ L.OSM.JSONParser = { return result; }, - getWays: function (json, nodes) { - var result = []; + getWays(json, nodes) { + const ways = json.elements?.filter(el => el.type === "way") ?? []; - var ways = json.elements?.filter(d => d.type === "way") || []; - for (var i = 0; i < ways.length; i++) { - var way = ways[i], nds = way.nodes; - - var way_object = { - id: way["id"].toString(), - type: "way", - nodes: new Array(nds.length), - tags: this.getTags(way) - }; - - for (var j = 0; j < nds.length; j++) { - way_object.nodes[j] = nodes[nds[j]]; - } - - result.push(way_object); - } - - return result; + return ways.map(way => ({ + id: String(way.id), + type: "way", + nodes: way.nodes.map(nodeId => nodes[nodeId]), + tags: this.getTags(way) + })); }, - getRelations: function (json, nodes, ways) { - var result = []; - - var rels = json.elements?.filter(d => d.type === "relation") || []; - for (var i = 0; i < rels.length; i++) { - var rel = rels[i], members = rel["members"]; - - var rel_object = { - id: rel["id"].toString(), - type: "relation", - members: new Array(members.length), - tags: this.getTags(rel) - }; - - for (var j = 0; j < members.length; j++) { - if (members[j]["type"] === "node") - rel_object.members[j] = nodes[members[j]["ref"]]; - else // relation-way and relation-relation membership not implemented - rel_object.members[j] = null; - } - rel_object.members = rel_object.members.filter(i => i !== null && i !== undefined) - result.push(rel_object); - } - - return result; + getRelations(json, nodes) { + const relations = json.elements?.filter(el => el.type === "relation") ?? []; + + return relations.map(rel => ({ + id: String(rel.id), + type: "relation", + members: (rel.members ?? []) // relation-way and relation-relation membership not implemented + .map(member => member.type === "node" ? nodes[member.ref] : null) + .filter(Boolean), // filter out null and undefined + tags: this.getTags(rel) + })); }, - getTags: function (json) { - return json["tags"]; + getTags(json) { + return json.tags ?? {}; } -} \ No newline at end of file +};