mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-04 23:44:11 +02:00
refact: extract resources helpers to utils
This commit is contained in:
+61
-1
@@ -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)]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user