From b6b4c9df61bbf71bfa1dcd3fc7f6b882e10f47be Mon Sep 17 00:00:00 2001 From: Jean-Philippe Sirois Date: Tue, 17 Sep 2019 00:49:09 -0400 Subject: [PATCH] refact: extract resources helpers to utils --- src/components/ResourcesList/useBuildTree.tsx | 70 +------------------ .../ResourcesSidebar/useBuildTree.tsx | 66 +---------------- src/utils/index.ts | 62 +++++++++++++++- 3 files changed, 65 insertions(+), 133 deletions(-) diff --git a/src/components/ResourcesList/useBuildTree.tsx b/src/components/ResourcesList/useBuildTree.tsx index 8a26996..f347827 100644 --- a/src/components/ResourcesList/useBuildTree.tsx +++ b/src/components/ResourcesList/useBuildTree.tsx @@ -1,71 +1,5 @@ -import chain from "ramda/es/chain" -import partition from "ramda/es/partition" - -import { - IAllResourcesQuery, - IFile, - IFileOrFolder, - IFileQuery, - IFolder, -} from "../../types" -import { traversePathsToFiles } from "../../utils" - -function generateFolder({ - title, - path, - targets, -}: { - title: IFolder["title"] - path: IFile["path"] - targets: IFolder[] -}): IFolder { - const children = join(chain(target => target.children, targets)) - - return { - title, - type: "folder", - path, - children: children.sort((a, b) => - a.title.split("/").length > b.title.split("/").length - ? -1 - : a.title.localeCompare(b.title) - ), - } -} - -function generateFile({ - title, - path, -}: { - title: IFile["title"] - path: IFile["path"] -}): IFile { - return { - title, - type: "file", - path, - } -} - -function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] { - if (!head) { - return [] - } - - const [similarFs, remaining] = partition( - obj => obj.title === head.title && obj.type === head.type, - tail - ) - const targets = [head, ...similarFs] - const { title, path } = head - - const current = - head.type === "folder" - ? generateFolder({ title, path, targets: targets as IFolder[] }) - : generateFile({ title, path }) - - return [current, ...join(remaining)] -} +import { IAllResourcesQuery, IFileQuery } from "../../types" +import { join, traversePathsToFiles } from "../../utils" export default function useBuildTree(resources: IAllResourcesQuery) { const objects = resources.allFile.edges.map(({ node: file }: IFileQuery) => diff --git a/src/components/ResourcesSidebar/useBuildTree.tsx b/src/components/ResourcesSidebar/useBuildTree.tsx index 0e0b01a..0be532b 100644 --- a/src/components/ResourcesSidebar/useBuildTree.tsx +++ b/src/components/ResourcesSidebar/useBuildTree.tsx @@ -1,67 +1,5 @@ -import chain from "ramda/es/chain" -import partition from "ramda/es/partition" - -import { - IAllResourcesQuery, - IFile, - IFileOrFolder, - IFileQuery, - IFolder, -} from "../../types" -import { traversePaths } from "../../utils" - -function generateFolder({ - title, - path, - targets, -}: { - title: IFolder["title"] - path: IFile["path"] - targets: IFolder[] -}): IFolder { - const children = join(chain(target => target.children, targets)) - - return { - title, - type: "folder", - path, - children, - } -} - -function generateFile({ - title, - path, -}: { - title: IFile["title"] - path: IFile["path"] -}): IFile { - return { - title, - type: "file", - path, - } -} - -function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] { - if (!head) { - return [] - } - - const [similarFs, remaining] = partition( - obj => obj.title === head.title && obj.type === head.type, - tail - ) - const targets = [head, ...similarFs] - const { title, path } = head - - const current = - head.type === "folder" - ? generateFolder({ title, path, targets: targets as IFolder[] }) - : generateFile({ title, path }) - - return [current, ...join(remaining)] -} +import { IAllResourcesQuery, IFileQuery } from "../../types" +import { join, traversePaths } from "../../utils" export default function useBuildTree(resources: IAllResourcesQuery) { const objects = resources.allFile.edges.map(({ node: file }: IFileQuery) => diff --git a/src/utils/index.ts b/src/utils/index.ts index 3aced7c..0f12043 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,7 @@ -import { IFileOrFolder } from "../types" +import chain from "ramda/es/chain" +import partition from "ramda/es/partition" + +import { IFile, IFileOrFolder, IFolder } from "../types" export function traversePaths( [head, ...tail]: string[], @@ -47,3 +50,60 @@ export function traversePathsToFiles( children: [traversePathsToFiles(tail, path, 1)], } } + +function generateFolder({ + title, + path, + targets, +}: { + title: IFolder["title"] + path: IFile["path"] + targets: IFolder[] +}): IFolder { + const children = join(chain(target => target.children, targets)) + + return { + title, + type: "folder", + path, + children: children.sort((a, b) => + a.title.split("/").length > b.title.split("/").length + ? -1 + : a.title.localeCompare(b.title) + ), + } +} + +function generateFile({ + title, + path, +}: { + title: IFile["title"] + path: IFile["path"] +}): IFile { + return { + title, + type: "file", + path, + } +} + +export function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] { + if (!head) { + return [] + } + + const [similarFs, remaining] = partition( + obj => obj.title === head.title && obj.type === head.type, + tail + ) + const targets = [head, ...similarFs] + const { title, path } = head + + const current = + head.type === "folder" + ? generateFolder({ title, path, targets: targets as IFolder[] }) + : generateFile({ title, path }) + + return [current, ...join(remaining)] +}