Last active
February 14, 2017 11:33
-
-
Save Nimelrian/42741e79768c47f871da48beeca75974 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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