Skip to content

Commit

Permalink
fix(printTree): add proper sorting of files and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Feb 24, 2020
1 parent ef01dae commit bc177c1
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/index/printTree.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
type Leaf = { text: string; position: number };
type PositionedLeaf = { text: string; position: number };

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

/** Remove path that matches `match` but save '/' to calculate position. */
const removePathDuplication = (target: string, match: string) =>
target.replace(new RegExp(`^(/*)${match.replace(/\//g, "")}(/+)`), "$1$2");

const sanitizeDots = (text: string) => text.replace(/\./g, "");
/**
* Matches anything between '/' and '.' and prepends the highest possible char
* code to it. This enables us sort files lower than directories.
*/
const prependMaxCharCodeToFile = (text: string) =>
text.replace(
/([^/]+)(?=\.)/g,
String.fromCharCode(Number.MAX_SAFE_INTEGER) + "$1"
);
const compareLeafs = (a: string, b: string) =>
sanitizeDots(a).localeCompare(sanitizeDots(b));
prependMaxCharCodeToFile(a).localeCompare(prependMaxCharCodeToFile(b));

/** Check if leaf is the last of its siblings excluding children. */
const isLeafLastSibling = (leaf: Leaf, remainingLeafs: Leaf[]) => {
const isLeafLastSibling = (
leaf: PositionedLeaf,
remainingLeafs: PositionedLeaf[]
) => {
for (const remaningLeaf of remainingLeafs) {
if (remaningLeaf.position > leaf.position) continue;
if (remaningLeaf.position < leaf.position) return true;
Expand Down

0 comments on commit bc177c1

Please sign in to comment.