Skip to content

Instantly share code, notes, and snippets.

@ankitbtanna
Created November 13, 2023 08:56
Show Gist options
  • Save ankitbtanna/f459deddc20a9350005fe1ca712c0e95 to your computer and use it in GitHub Desktop.
Save ankitbtanna/f459deddc20a9350005fe1ca712c0e95 to your computer and use it in GitHub Desktop.
Atlassian Interview - Recursively parse the folder tree structure
import fetch from 'node-fetch';
async function fetchFileStructure() {
return await fetch('https://api.jsonbin.io/v3/b/64cef151b89b1e2299cc09a2');
}
async function useAndFetchFileStructure() {
try {
const response = await fetchFileStructure();
const fileStructure = await response.json();
displayFolderAndFileStructure(fileStructure.record);
return fileStructure;
} catch (e) {
console.log(e);
return [];
}
}
function displayFolderAndFileStructure(fileSystem, level = '') {
const isFolder = fileSystem.type === 'folder';
const isFile = fileSystem.type === 'file';
const hasChildren = !isFile && fileSystem.children.length > 0;
const indentation = level + '--';
if (isFile) {
console.log(`${indentation} f ${fileSystem.name}`);
return;
}
if (isFolder) {
console.log(`${indentation} F ${fileSystem.name}`);
if (hasChildren) {
fileSystem.children.forEach((fs) => {
displayFolderAndFileStructure(fs, indentation);
});
} else {
return;
}
}
}
useAndFetchFileStructure();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment