Skip to content

Instantly share code, notes, and snippets.

@Nimelrian
Last active February 14, 2017 11:33
Show Gist options
  • Save Nimelrian/42741e79768c47f871da48beeca75974 to your computer and use it in GitHub Desktop.
Save Nimelrian/42741e79768c47f871da48beeca75974 to your computer and use it in GitHub Desktop.
function validatePath (path) {
const components = path.split('/');
const validatedFolderTree = [];
const getRootFolders = foldersApi.getTopLevelFolders();
const finalValidationPromise = components.reduce((promise, folder, index) => {
const folderName = folder.toLowerCase();
return promise
.then(parentChildren => {
const folderInChildren = parentChildren.find(child => child.name.toLowerCase() === folderName);
if (!folderInChildren) {
// Offset index by one since slice's end parameter is exclusive
// TODO: LOC
throw new Error(`Path validation failed: The folder ${components.slice(0, index + 1).join('/')} could not be found.`);
}
else {
validatedFolderTree.push(folderInChildren);
return foldersApi.getChildFolders(folderInChildren.id);
}
})
.then(childFolders => {
validatedFolderTree[index].children = childFolders;
return childFolders;
});
}, getRootFolders);
return finalValidationPromise.then(() => validatedFolderTree);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment