Skip to content

Commit

Permalink
fix traversers
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 10, 2024
1 parent 4c1c299 commit fd66e56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
44 changes: 22 additions & 22 deletions packages/fdr-sdk/src/navigation/utils/pruneNavigationTree.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import structuredClone from "@ungap/structured-clone";
import { FernNavigation } from "../..";
import { bfs } from "../../utils/traversers/bfs";
import { prunetree } from "../../utils/traversers/prunetree";
import { mutableDeleteChild } from "./deleteChild";
import { mutableUpdatePointsTo } from "./updatePointsTo";

type Predicate<T extends FernNavigation.NavigationNode = FernNavigation.NavigationNode> = (
node: T,
parents: readonly FernNavigation.NavigationNodeParent[],
) => boolean;

export class Pruner<ROOT extends FernNavigation.NavigationNode> {
public static from<ROOT extends FernNavigation.NavigationNode>(tree: ROOT): Pruner<ROOT> {
return new Pruner(tree);
Expand All @@ -15,7 +19,7 @@ export class Pruner<ROOT extends FernNavigation.NavigationNode> {
this.tree = structuredClone(tree) as ROOT;
}

public keep(predicate: (node: FernNavigation.NavigationNode) => boolean): this {
public keep(predicate: Predicate): this {
if (this.tree == null) {
return this;
}
Expand All @@ -29,35 +33,31 @@ export class Pruner<ROOT extends FernNavigation.NavigationNode> {
return this;
}

public hide(predicate: (node: FernNavigation.NavigationNodeWithMetadata) => boolean): this {
public remove(predicate: Predicate): this {
return this.keep((node, parents) => !predicate(node, parents));
}

public hide(predicate: Predicate<FernNavigation.NavigationNodeWithMetadata>): this {
if (this.tree == null) {
return this;
}
bfs(
this.tree,
(node) => {
if (FernNavigation.hasMarkdown(node) && predicate(node)) {
node.hidden = true;
}
},
FernNavigation.getChildren,
);
FernNavigation.traverseBF(this.tree, (node, parents) => {
if (FernNavigation.hasMetadata(node)) {
node.hidden = predicate(node, parents) ? true : undefined;
}
});
return this;
}

public authed(predicate: (node: FernNavigation.NavigationNodeWithMetadata) => boolean): this {
public authed(predicate: Predicate<FernNavigation.NavigationNodeWithMetadata>): this {
if (this.tree == null) {
return this;
}
bfs(
this.tree,
(node) => {
if (FernNavigation.hasMarkdown(node) && predicate(node)) {
node.authed = true;
}
},
FernNavigation.getChildren,
);
FernNavigation.traverseBF(this.tree, (node, parents) => {
if (FernNavigation.hasMetadata(node)) {
node.authed = predicate(node, parents) ? true : undefined;
}
});
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/fdr-sdk/src/utils/traversers/prunetree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface PruneTreeOptions<NODE, PARENT extends NODE = NODE, POINTER = NODE> {
* @param node the node to check
* @returns **false** if the node SHOULD be deleted
*/
predicate: (node: NODE) => boolean;
predicate: (node: NODE, parents: readonly PARENT[]) => boolean;
getChildren: (node: PARENT) => readonly NODE[];

/**
Expand Down Expand Up @@ -53,7 +53,7 @@ export function prunetree<NODE, ROOT extends NODE = NODE, PARENT extends NODE =
}

// continue traversal if the node is not to be deleted
if (predicate(node)) {
if (predicate(node, parents)) {
return;
}
const ancestors = [...parents];
Expand Down

0 comments on commit fd66e56

Please sign in to comment.