Skip to content

Commit

Permalink
modify hierarchies_walker to walk multiple hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
trescube committed Apr 22, 2016
1 parent 9191ade commit db67c44
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
20 changes: 16 additions & 4 deletions src/hierarchyFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports.parent_id_walker = function(wofRecords) {
parent_id = parent.parent_id;
}

return parents.filter(hasName);
return [parents.filter(hasName)];

};

Expand All @@ -53,10 +53,22 @@ module.exports.parent_id_walker = function(wofRecords) {
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.
*/
module.exports.hierarchies_walker = function(wofRecords) {
return function(wofRecord) {
return Object.keys(wofRecord.hierarchy).map(function(key) {
return wofRecords[wofRecord.hierarchy[key]];
}).filter(isDefined).filter(hasName);
return wofRecord.hierarchies.reduce(function(resolvedHierarchies, hierarchy) {
resolvedHierarchies.push(resolveHierarchy(wofRecords, hierarchy));
return resolvedHierarchies;
}, []);
};
};
70 changes: 43 additions & 27 deletions test/hierarchyFinderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ tape('parent_id_walker tests', function(test) {

var hierarchies = parent_id_walker(wofRecords['4']);

t.deepEqual(hierarchies, [
t.deepEqual(hierarchies, [[
wofRecords['4'],
wofRecords['3'],
wofRecords['2'],
wofRecords['1']
]);
]]);
t.end();

});
Expand Down Expand Up @@ -67,10 +67,10 @@ tape('parent_id_walker tests', function(test) {

var hierarchies = parent_id_walker(wofRecords['4']);

t.deepEqual(hierarchies, [
t.deepEqual(hierarchies, [[
wofRecords['4'],
wofRecords['1']
]);
]]);
t.end();

});
Expand All @@ -92,12 +92,18 @@ tape('hierarchies_walker tests', function(test) {
},
4: {
name: 'name 4',
hierarchy: { // keys don't matter
'first arbitrary level': 4,
'second arbitrary level': 3,
'third arbitrary level': 2,
'fourth arbitrary level': 1
}
hierarchies: [
{ // keys don't matter
'first arbitrary level': 4,
'second arbitrary level': 3,
'third arbitrary level': 2,
'fourth arbitrary level': 1
},
{
'fifth arbitrary level': 4,
'sixth arbitrary level': 2
}
]
}
};

Expand All @@ -107,10 +113,16 @@ tape('hierarchies_walker tests', function(test) {
var hierarchies = hierarchies_walker(wofRecords['4']);

t.deepEqual(hierarchies, [
wofRecords['4'],
wofRecords['3'],
wofRecords['2'],
wofRecords['1']
[
wofRecords['4'],
wofRecords['3'],
wofRecords['2'],
wofRecords['1']
],
[
wofRecords['4'],
wofRecords['2']
]
]);
t.end();

Expand All @@ -130,12 +142,14 @@ tape('hierarchies_walker tests', function(test) {
},
4: {
name: 'name 4',
hierarchy: { // keys don't matter
'first arbitrary level': 4,
'second arbitrary level': 3, // no name, will be excluded
'third arbitrary level': 2,
'fourth arbitrary level': 1
}
hierarchies: [
{ // keys don't matter
'first arbitrary level': 4,
'second arbitrary level': 3, // no name, will be excluded
'third arbitrary level': 2,
'fourth arbitrary level': 1
}
]
}
};

Expand All @@ -144,11 +158,11 @@ tape('hierarchies_walker tests', function(test) {

var hierarchies = hierarchies_walker(wofRecords['4']);

t.deepEqual(hierarchies, [
t.deepEqual(hierarchies, [[
wofRecords['4'],
wofRecords['2'],
wofRecords['1']
]);
]]);
t.end();

});
Expand All @@ -164,12 +178,12 @@ tape('hierarchies_walker tests', function(test) {
},
4: {
name: 'name 4',
hierarchy: { // keys don't matter
hierarchies: [{ // keys don't matter
'first arbitrary level': 4,
'second arbitrary level': 3,
'third arbitrary level': 2, // this will be undefined
'fourth arbitrary level': 1
}
}]
}
};

Expand All @@ -179,9 +193,11 @@ tape('hierarchies_walker tests', function(test) {
var hierarchy = hierarchies_walker(wofRecords['4']);

t.deepEqual(hierarchy, [
wofRecords['4'],
wofRecords['3'],
wofRecords['1']
[
wofRecords['4'],
wofRecords['3'],
wofRecords['1']
]
]);
t.end();

Expand Down

0 comments on commit db67c44

Please sign in to comment.