Skip to content

Commit

Permalink
fix(printTree): position our leafs
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Feb 24, 2020
1 parent 514084a commit 1b5bca1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
40 changes: 35 additions & 5 deletions src/index/treePrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

const createBranchFromParts = (parts: string[], count: number) =>
parts.slice(0, count).join("/");

const removePathDuplication = (target: string, match: string) =>
target.replace(new RegExp(`^(/*)${match.replace(/\//, "")}(/+)`), "$1$2");

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) => {
/** Resolve every unique leaf from a list of paths. */
const resolveLeafs = (paths: string[]) => {
const leafs = paths.reduce<Set<string>>((acc, target) => {
const parts = target.split("/");

parts.forEach((_, idx) => acc.add(createBranchFromParts(parts, idx + 1)));
Expand All @@ -20,11 +25,36 @@ const resolveLeafs = (targets: string[]) => {
return Array.from(leafs).sort(compareLeafs);
};

export const treePrinter = (tree: Record<string, string>) => {
const targets = Object.values(tree);
const leafs = resolveLeafs(targets);
/**
* Iterates over all leafs and positions them on the correct branches, ie
* resolving their end name in relation to their branch and their position.
*/
const positionLeafs = (leafs: string[]) => {
const res = [];

let queue = [...leafs];
while (queue.length > 0) {
const leaf = queue.shift();

if (leaf == null) break;
queue = queue.map(queuedLeaf => removePathDuplication(queuedLeaf, leaf));

res.push(leaf);
}

return res.map(x => {
const parts = x.split("/");
return { text: parts.pop() ?? "", position: parts.length };
});
};

/** Prints a tree visualization of given paths. */
export const treePrinter = (paths: string[]) => {
const leafs = resolveLeafs(paths);
const positionedLeafs = positionLeafs(leafs);

console.log(leafs);
console.log(positionedLeafs);
};

export default treePrinter;
21 changes: 11 additions & 10 deletions tests/treePrinter.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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",
});
treePrinter([
"index.js",
"index/index.css",
"index/App.js",
"index/Foo/foo.svg",
"index/Foo/Bar/bar.css",
"index/App/logo.svg",
"index/App/App.css",
"index/serviceWorker.js",
"index/App.test.js",
]);

describe("treePrinter", () => {
it("should", () => {
Expand Down

0 comments on commit 1b5bca1

Please sign in to comment.