Your IP : 216.73.216.48


Current Path : /hdd/hdd21/my5/backup/my5-backend/src/utils/
Upload File :
Current File : /hdd/hdd21/my5/backup/my5-backend/src/utils/buildFsTree.js

const buildFsTree = arr => {
  const root = arr.find(f => f.parentID === null);
  return {root: buildChildren(root, arr), 
    ...(!!root && {rootID: root.id})
  };
};

const buildChildren = (root, arr) => {
  return arr
    .filter(f => f.parentID === root.id)
    .map(f => {
      if (f.type === 'FILE') {
        return shapeFile(f);
      } else if (f.type === 'DIR') {
        return {
          id: f.id,
          name: f.name,
          type: f.type,
          children: buildChildren(f, arr)
        };
      }
    });
};

const shapeFile = file => ({
  name: file.name,
  size: file.size,
  type: file.type,
  id: file.id
});

module.exports = buildFsTree;