MCPcopy Create free account
hub / github.com/Power-Playground/app / createSetVFile

Function createSetVFile

core/src/virtual-files.ts:68–112  ·  view source on GitHub ↗
(
  setVFiles: (arg0: (vFiles: VFile[]) => VFile[]) => void
)

Source from the content-addressed store, hash-verified

66}
67
68export const createSetVFile = (
69 setVFiles: (arg0: (vFiles: VFile[]) => VFile[]) => void
70) => pipply(createVFile, (rt, index?: -1 | undefined) => {
71 const rtAlias = rt as VFile
72 const parents: Iterable<string> = {
73 [Symbol.iterator]: function* () {
74 let parent = rtAlias.dirname
75 while (parent !== '') {
76 yield parent
77 parent = parent.slice(0, parent.lastIndexOf('/'))
78 }
79 }
80 }
81 function autoCreateParent(vFiles: VFile[]) {
82 const rtVFiles: VFile[] = []
83 let insertIndex = -1
84 for (const parent of parents) {
85 // TODO map path to index
86 insertIndex = vFiles.findIndex(vFile => vFile.path === parent)
87 if (insertIndex !== -1) break
88 rtVFiles.unshift(createVFile({ path: parent }) as VFile)
89 }
90 return [insertIndex, rtVFiles] as const
91 }
92
93 setVFiles(vFiles => {
94 const [insertIndex, autoCreateParents] = autoCreateParent(vFiles)
95 const [leftVFiles, rightVFiles] = insertIndex === -1
96 ? [vFiles, []]
97 : [vFiles.slice(0, insertIndex + 1), vFiles.slice(insertIndex + 1)]
98 if (index === -1) {
99 return [...leftVFiles, ...autoCreateParents, rtAlias, ...rightVFiles]
100 } else {
101 // TODO insert to the last child of parent children
102 return [
103 ...leftVFiles,
104 ...rightVFiles.slice(0, autoCreateParents.length),
105 ...autoCreateParents,
106 rtAlias,
107 ...rightVFiles.slice(autoCreateParents.length)
108 ]
109 }
110 })
111 return rt
112})
113
114type CreateSetVFileReturn = ReturnType<typeof createSetVFile>
115

Callers 3

createSetVFileByStoreFunction · 0.85
useVFilesFunction · 0.85

Calls 1

autoCreateParentFunction · 0.85

Tested by

no test coverage detected