Skip to content

Commit

Permalink
fix(printTree): add leaf resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Feb 24, 2020
1 parent 81977d0 commit 514084a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/index/treePrinter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* TODO:
* - ASCII pieces to use: ├, │, └, ─.
*/

const createBranchFromParts = (parts: string[], count: number) =>
parts.slice(0, count).join("/");
const sanitizeDots = (text: string) => text.replace(/\./g, "");
const compareLeafs = (a: string, b: string) =>
sanitizeDots(a).localeCompare(sanitizeDots(b));

const resolveLeafs = (targets: string[]) => {
const leafs = targets.reduce<Set<string>>((acc, target) => {
const parts = target.split("/");

parts.forEach((_, idx) => acc.add(createBranchFromParts(parts, idx + 1)));
return acc;
}, new Set());

return Array.from(leafs).sort(compareLeafs);
};

export const treePrinter = (tree: Record<string, string>) => {
const targets = Object.values(tree);
const leafs = resolveLeafs(targets);

console.log(leafs);
};

export default treePrinter;
18 changes: 18 additions & 0 deletions tests/treePrinter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import treePrinter from "../src/index/treePrinter";

treePrinter({
"index.js": "index.js",
"index.css": "index/index.css",
"App.js": "index/App.js",
"foo.svg": "index/Foo/foo.svg",
"logo.svg": "index/App/logo.svg",
"App.css": "index/App/App.css",
"serviceWorker.js": "index/serviceWorker.js",
"App.test.js": "index/App.test.js",
});

describe("treePrinter", () => {
it("should", () => {
expect(true).toBe(true);
});
});

0 comments on commit 514084a

Please sign in to comment.