Change ingest() => Use named args instead of positional args

folders
Rick Carlino 2019-12-05 08:54:39 -06:00
parent be16f15917
commit 0fa65f0eac
5 changed files with 26 additions and 11 deletions

View File

@ -51,7 +51,7 @@ const FOLDERS: FolderNode[] = [
{ id: 18, parent_id: 16, color: "blue", name: "Eighteen" }
];
const GRAPH = ingest(FOLDERS, {});
const GRAPH = ingest({ folders: FOLDERS, parentIndex: {} });
const randomNode = () => {
const node = sample(FOLDERS);

View File

@ -9,7 +9,7 @@ const FOLDERS: FolderNode[] = [
{ id: 4, color: "gray", name: "tests", parent_id: undefined },
{ id: 5, color: "pink", name: "deeply nested directory", parent_id: 3 }
];
const TREE = ingest(FOLDERS, {});
const TREE = ingest({ folders: FOLDERS, parentIndex: {} });
describe("climb()", () => {
it("traverses through the nodes", () => {

View File

@ -1,5 +1,10 @@
import { Color } from "farmbot/dist/corpus";
export interface FolderMeta {
open?: boolean;
editing?: boolean;
}
interface FolderUI {
id: number;
name: string;

View File

@ -27,20 +27,30 @@ const emptyIndex: FoldersIndexedByParentId = {};
export type SequenceIndexedByParentId = Record<number, string[] | undefined>;
const PARENTLESS = -1;
type IngestFn =
(input: FolderNode[], map: SequenceIndexedByParentId) => RootFolderNode;
(props: IngestFnProps) => RootFolderNode;
export const ingest: IngestFn = (input, parentIdMapping) => {
interface IngestFnProps {
folders: FolderNode[];
/** "Which sequences are using this folder as their parent?"
* Key is a number, representing a folder ID.
* Value is a string, representing sequence UUIDS
* (sequences that are embedded in the folders)
*/
parentIndex: Record<number, string[] | undefined>;
}
export const ingest: IngestFn = ({ folders, parentIndex }) => {
const output: RootFolderNode = {
folders: [],
folderless: parentIdMapping[PARENTLESS] || []
folderless: parentIndex[PARENTLESS] || []
};
const index = input.map(setDefaultParentId).reduce(addToIndex, emptyIndex);
const index = folders.map(setDefaultParentId).reduce(addToIndex, emptyIndex);
const childrenOf = (i: number) => sortBy(index[i] || [], (x) => x.name.toLowerCase());
const terminal = (x: FolderNode): FolderNodeTerminal => ({
...x,
kind: "terminal",
content: parentIdMapping[x.id] || [],
content: parentIndex[x.id] || [],
children: []
});
@ -48,7 +58,7 @@ export const ingest: IngestFn = (input, parentIdMapping) => {
...x,
kind: "medial",
children: childrenOf(x.id).map(terminal),
content: parentIdMapping[x.id] || []
content: parentIndex[x.id] || []
});
childrenOf(-1).map((root) => {
@ -57,7 +67,7 @@ export const ingest: IngestFn = (input, parentIdMapping) => {
...root,
kind: "initial",
children,
content: parentIdMapping[root.id] || []
content: parentIndex[root.id] || []
});
});

View File

@ -60,7 +60,7 @@ export const folderIndexer: IndexerCallback = (r, i) => {
}
}));
const map = selectAllSequences(i)
const parentIndex = selectAllSequences(i)
.reduce((a, s) => {
if (!a[s.body.folder_id || -1]) {
a[s.body.folder_id || -1] = [];
@ -68,7 +68,7 @@ export const folderIndexer: IndexerCallback = (r, i) => {
a[s.body.folder_id || -1]?.push(s.uuid);
return a;
}, {} as SequenceIndexedByParentId);
i.sequenceFolders = ingest(folders, map);
i.sequenceFolders = ingest({ folders, parentIndex });
}
};