-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 2.2 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
58
59
60
61
62
63
'use strict';
const rp = require('request-promise-native');
const rules = require('./rules');
function revertedChangesets(changesetID){
return fetchChangeset(changesetID).then((changeset) => {
return getFeatureHistory(getPreviousChangesets(changeset)).then((oldFeatureVersions) => {
const nthVersions = changeset.map((feature) => feature.new);
const nMinusOneVersions = oldFeatureVersions.map((feature) => feature.new);
const nMinusTwoVersions = oldFeatureVersions.map((feature) => feature.old);
const revertedIndexes = rules.getRevertedIndexes(nthVersions, nMinusOneVersions.concat(nMinusTwoVersions));
return listRevertedChangesets(revertedIndexes, changeset, changesetID);
});
}).catch((error) => console.error(error));
}
function listRevertedChangesets(indexes, changeset, changesetID){
const changesetsList = [];
for (let i=0; i < indexes.length; i++){
if (indexes[i]){
changesetsList.push(changeset[i].old.properties['osm:changeset'].toString());
}
}
const uniqueIds = changesetsList.filter((v, i, a) => a.indexOf(v) === i && v !== changesetID);
if (uniqueIds.length === 0) {
return false;
} return uniqueIds;
}
function fetchChangeset(changesetID) {
return rp('https://s3.amazonaws.com/mapbox/osm-adiff-compare/production/' + changesetID + '.json')
.then((response) => JSON.parse(response))
.catch((error) => console.error(`ERROR ${changesetID}`, error));
}
function getFeatureHistory(changesetIds) {
return getChangesetFeatures(changesetIds).then((changesetArray) => {
const featuresArray = [];
changesetArray.forEach((changeset) => {
changeset.forEach((feature) => {
featuresArray.push(feature);
});
});
return featuresArray;
});
}
function getChangesetFeatures(changesets){
return Promise.all(changesets.map((id) => {
return fetchChangeset(id);
}));
}
function getPreviousChangesets(changeset) {
const changesetList = changeset.map((feature) => {
if (feature && feature.old && feature.old.properties) {
return feature.old.properties['osm:changeset'];
} return null;
});
return changesetList.filter((v, i, a) => a.indexOf(v) === i && v !== null);
}
module.exports = revertedChangesets;