Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions src/hierarchyFinder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
module.exports = {};

var hasName = function(r) {
return r.name;
};

var isDefined = function(r) {
return r;
};
Comment on lines -7 to -9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is excessive even by @trescube standards 😆


/*
This function finds all the WOF records associated with a hierarchy

Expand All @@ -26,24 +16,20 @@ var isDefined = function(r) {
]

lastly, filter out any hierarchy elements that are undefined or w/o a name

*/
function resolveHierarchy(wofRecords, hierarchy) {
return Object.keys(hierarchy).map(function(key) {
return wofRecords[hierarchy[key]];
}).filter(isDefined).filter(hasName);
}

/*
This function returns all the resolved hierarchies for a wofRecord. Each
wofRecord can have multiple hierarchies, so resolve them by looking up the
referenced wofRecord in the big collection of wofRecords.
referenced wofRecord in the big collection of parentRecords.
*/
module.exports = function(wofRecords) {
return function(wofRecord) {
return wofRecord.hierarchies.reduce(function(resolvedHierarchies, hierarchy) {
resolvedHierarchies.push(resolveHierarchy(wofRecords, hierarchy));
return resolvedHierarchies;
}, []);
module.exports = (parentRecords) => {
return (wofRecord) => {
return wofRecord.hierarchies.map(hierarchy => {
return Object.values(hierarchy)
.map(parentId => parentRecords[parentId])
.filter(Boolean)
.filter(r => r.name);
});
Comment on lines +26 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice cleanup here. Modern JS and easier to understand as well.

};
};
16 changes: 7 additions & 9 deletions src/peliasDocGenerators.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,16 @@ function setupDocument(record, hierarchy) {

}

module.exports.create = function(hierarchy_finder) {
return through2.obj(function(record, enc, next) {
// if there are no hierarchies, then just return the doc as-is
var hierarchies = hierarchy_finder(record);
module.exports.create = (hierarchy_finder) => {
return through2.obj(function(record, _enc, next) {
const hierarchies = hierarchy_finder(record);

try {
if (hierarchies && hierarchies.length > 0) {
hierarchies.forEach(function(hierarchy) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the major change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified via a console.log that this code is not called by the PIP service. So it should only affect actual whosonfirst records in Elasticsearch, not admin lookup for any other records like addresses. 👍

this.push(setupDocument(record, hierarchy));
}, this);

if (Array.isArray(hierarchies) && hierarchies.length > 0) {
// only use the first hierarchy when multiple hierarchies exist
this.push(setupDocument(record, hierarchies[0]));
} else {
// if there are no hierarchies, then just return the doc as-is
this.push(setupDocument(record));
}
}
Expand Down
7 changes: 1 addition & 6 deletions test/peliasDocGeneratorsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ tape('create', function(test) {

});

test.test('a document should be created for each available hierarchy', function(t) {
test.test('a single document should be created when multiple hierarchies exist', function(t) {
var wofRecords = {
1: {
id: 1,
Expand Down Expand Up @@ -619,11 +619,6 @@ tape('create', function(test) {
.setCentroid({ lat: 12.121212, lon: 21.212121 })
.addParent( 'neighbourhood', 'neighbourhood name', '1')
.addParent( 'country', 'country name 1', '2'),
new Document( 'whosonfirst', 'neighbourhood', '1')
.setName('default', 'neighbourhood name')
.setCentroid({ lat: 12.121212, lon: 21.212121 })
.addParent( 'neighbourhood', 'neighbourhood name', '1')
.addParent( 'country', 'country name 2', '3')
];

var hierarchies_finder = function() {
Expand Down