-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make functions abstract, polyfill structuredClone, etc
- Loading branch information
1 parent
ea4c20d
commit 520edee
Showing
32 changed files
with
834 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare module "core-js-pure/actual/structured-clone" { | ||
const structuredClone: <T>(value: T) => T; | ||
|
||
export default structuredClone; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/fdr-sdk/src/navigation/utils/__test__/pruneNavigationTree.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { FernNavigation } from "../../.."; | ||
import { pruneNavigationTree } from "../pruneNavigationTree"; | ||
|
||
describe("pruneNavigationTree", () => { | ||
it("should not prune the tree if keep returns true for all nodes", () => { | ||
const root: FernNavigation.NavigationNode = { | ||
type: "section", | ||
id: FernNavigation.NodeId("root"), | ||
slug: FernNavigation.Slug("root"), | ||
title: "Root", | ||
children: [ | ||
{ | ||
type: "page", | ||
id: FernNavigation.NodeId("page"), | ||
slug: FernNavigation.Slug("root/page"), | ||
title: "Page", | ||
pageId: FernNavigation.PageId("page.mdx"), | ||
canonicalSlug: undefined, | ||
icon: undefined, | ||
hidden: undefined, | ||
noindex: undefined, | ||
}, | ||
], | ||
collapsed: undefined, | ||
canonicalSlug: undefined, | ||
icon: undefined, | ||
hidden: undefined, | ||
overviewPageId: undefined, | ||
noindex: undefined, | ||
pointsTo: undefined, | ||
}; | ||
|
||
const result = pruneNavigationTree(root, () => true); | ||
|
||
// structuredClone should duplicate the object | ||
expect(result === root).toBe(false); | ||
|
||
expect(result).toStrictEqual({ | ||
type: "section", | ||
id: FernNavigation.NodeId("root"), | ||
slug: FernNavigation.Slug("root"), | ||
title: "Root", | ||
children: [ | ||
{ | ||
type: "page", | ||
id: FernNavigation.NodeId("page"), | ||
slug: FernNavigation.Slug("root/page"), | ||
title: "Page", | ||
pageId: FernNavigation.PageId("page.mdx"), | ||
canonicalSlug: undefined, | ||
icon: undefined, | ||
hidden: undefined, | ||
noindex: undefined, | ||
}, | ||
], | ||
collapsed: undefined, | ||
canonicalSlug: undefined, | ||
icon: undefined, | ||
hidden: undefined, | ||
overviewPageId: undefined, | ||
noindex: undefined, | ||
pointsTo: undefined, | ||
}); | ||
}); | ||
|
||
it("should return undefined if no visitable pages are left", () => { | ||
const root: FernNavigation.NavigationNode = { | ||
type: "section", | ||
id: FernNavigation.NodeId("root"), | ||
slug: FernNavigation.Slug("root"), | ||
title: "Root", | ||
children: [ | ||
{ | ||
type: "page", | ||
id: FernNavigation.NodeId("page"), | ||
slug: FernNavigation.Slug("root/page"), | ||
title: "Page", | ||
pageId: FernNavigation.PageId("page.mdx"), | ||
canonicalSlug: undefined, | ||
icon: undefined, | ||
hidden: undefined, | ||
noindex: undefined, | ||
}, | ||
], | ||
collapsed: undefined, | ||
canonicalSlug: undefined, | ||
icon: undefined, | ||
hidden: undefined, | ||
overviewPageId: undefined, | ||
noindex: undefined, | ||
pointsTo: undefined, | ||
}; | ||
|
||
const result = pruneNavigationTree(root, (node) => node.id !== FernNavigation.NodeId("page")); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { UnreachableCaseError } from "ts-essentials"; | ||
import { FernNavigation } from "../.."; | ||
|
||
/** | ||
* @param parent delete node from this parent (mutable) | ||
* @param node node to delete | ||
* @returns the id of the deleted node or null if the node was not deletable from the parent | ||
*/ | ||
export function mutableDeleteChild( | ||
parent: FernNavigation.NavigationNodeParent, | ||
node: FernNavigation.NavigationNode, | ||
): FernNavigation.NodeId | null { | ||
switch (parent.type) { | ||
case "apiPackage": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "apiReference": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
parent.changelog = parent.changelog?.id === node.id ? undefined : parent.changelog; | ||
return node.id; | ||
case "changelog": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "changelogYear": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "changelogMonth": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "endpointPair": | ||
return null; | ||
case "productgroup": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
parent.landingPage = parent.landingPage?.id === node.id ? undefined : parent.landingPage; | ||
return node.id; | ||
case "product": | ||
return null; | ||
case "root": | ||
return null; | ||
case "unversioned": | ||
if (node.id === parent.landingPage?.id) { | ||
parent.landingPage = undefined; | ||
return node.id; | ||
} | ||
return null; | ||
case "section": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "sidebarGroup": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "tab": | ||
return null; | ||
case "sidebarRoot": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "tabbed": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
case "version": | ||
if (node.id === parent.landingPage?.id) { | ||
parent.landingPage = undefined; | ||
return node.id; | ||
} | ||
return null; | ||
case "versioned": | ||
parent.children = parent.children.filter((child) => child.id !== node.id); | ||
return node.id; | ||
default: | ||
throw new UnreachableCaseError(parent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.