function getFolderObjects(objs) { // _.map takes a collection and uses some function to transform each element into something else
return _.map(objs, function(o) { // check if the current element of the collection is a string (an id for a character or handout)
if (_.isString(o)) { // return the handout, or the character if it's not a handout's id
return getObj('handout', o) || getObj('character', o);
} // if the element isn't a string, it should be an object; one of its properties should be i, which should be an array
if (_.isArray(o.i)) { // the i property is the contents of the folder, so recursively call this function to get the objects in the folder
o.i = getFolderObjects(o.i); // return the folder
return o;
}
});
}
function getObjectFromFolder(path, folderData, getFolder) { // if path contains a period
if (path.indexOf('.') < 0) { // if the client code wants a folder returned
if (getFolder) { // _.find returns the first element in a collection that passes the predicate function // "(o) => ..." is shorthand for "function(o) { return ...; }" // a lambda function like this has some implications for the `this` keyword, but they're not relevant here // the function body will make sure the element has a name and the name matches the path
return _.find(folderData, (o) => o.n && o.n.toLowerCase() === path.toLowerCase()).i;
} // if this line is reached, the client code wants a character/handout // the function body will make sure the element has a get function and that the name matches the path
return _.find(folderData, (o) => o.get && o.get('name').toLowerCase() === path.toLowerCase());
} // if this line is reached, the path is trying to reach something inside a folder // split the path by the separator (period); "a.b.c" becomes ["a", "b", "c"]
path = path.split('.'); // the first part of the path is a folder // path.shift removes the first element from the array and returns the value // ["a", "b", "c"] becomes ["b", "c"] and folder becomes "a"
var folder = path.shift(); // turn path back into a period-separated list; ["b", "c"] becomes "b.c"
path = path.join('.'); // the function body finds the folder with the name of `folder`
folderData = _.find(folderData, (o) => o.n && o.n.toLowerCase() === folder.toLowerCase()); // recursively call this function to drill deeper into the folder structure
return getObjectFromFolder(path, folderData.i);
} Do the comments help at all? =)