Ad

The forEach could be replaced with a reduce but I think it's less readable.

Code
Diff
  • function getTree(strings) {
      const tree = {};
      strings.forEach(path => {
        path && path.split("/").reduce((acc, cur, idx, src) => {
          if (idx >= src.length - 1) {
            acc[cur] = 'file';
          } else {
            acc[cur] = acc[cur] || {};
          }
          return acc[cur];
        }, tree);
      });
      return tree;
    }
    • function getTree(strings) {
    • return strings.reduce(
    • (root, path) => {
    • let items = path.split("/").filter(x=>x);
    • if (!items.length) return root;
    • let branch = root;
    • while (items.length > 1) {
    • let item = items.shift();
    • branch=branch[item]=branch[item]||{};
    • const tree = {};
    • strings.forEach(path => {
    • path && path.split("/").reduce((acc, cur, idx, src) => {
    • if (idx >= src.length - 1) {
    • acc[cur] = 'file';
    • } else {
    • acc[cur] = acc[cur] || {};
    • }
    • branch[items[0]] = "file";
    • return root;
    • },
    • {}
    • );
    • return acc[cur];
    • }, tree);
    • });
    • return tree;
    • }