-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sax from "sax";
const TYPED_ATTRS = [
"id",
"uid",
"min_lon",
"min_lat",
"max_lon",
"max_lat",
"comments_count",
"changes_count",
"open",
];
/*
* Parse OSM Changeset Metadata XML, of the form returned by
* https://www.openstreetmap.org/api/0.6/changeset/:id
* (Contains changeset's bbox, timestamp, comment, and authorship)
*/
function parseChangesetXML(xmlString) {
return new Promise((resolve, reject) => {
let parser = sax.parser(true /* strict mode */, { lowercase: true });
let result = {};
parser.onopentag = (node) => {
let name = node.name, attrs = node.attributes;
switch (name) {
case "osm":
Object.assign(result, attrs);
break;
case "changeset":
Object.assign((result.changeset ||= {}), attrs);
break;
case "tag":
Object.assign((result.changeset.tags ||= {}), { [attrs.k]: attrs.v });
break;
}
};
parser.onend = () => {
let { changeset } = result;
for (let attr of TYPED_ATTRS) {
if (changeset[attr] !== undefined) {
changeset[attr] = JSON.parse(changeset[attr]);
}
}
resolve(result);
};
parser.onerror = reject;
parser.write(xmlString);
parser.close();
});
}
export default parseChangesetXML;