Skip to content

Commit

Permalink
Fix make tree
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Sep 7, 2024
1 parent 92848a9 commit a291bad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.28.1",
"version": "0.28.2",
"repository": "github:ShaitanLyss/selenite-commons",
"license": "MIT",
"keywords": [
Expand Down
10 changes: 6 additions & 4 deletions src/lib/components/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ export function isForest<T>(
export { default as TreeComponent } from './Tree.svelte'

type TreeCollector<T> = { leaves: T[]; forest: Map<string, TreeCollector<T>> };
export function makeTree<T, K extends string>({


export function makeTree<K extends string | number, T extends {[k in K]?: string[]}>({
items,
pathKey,
sort
}: {
items: (T & { [k in K]: string[] })[];
items: T[];
pathKey: K;
sort?: (a: T, b: T) => number;
}): Tree<T> {
const collector: TreeCollector<T> = { leaves: [], forest: new Map() };
for (const item of items) {
let current = collector;
for (const parent of item[pathKey]) {
for (const parent of item[pathKey] ?? []) {
if (!current.forest.has(parent)) {
current.forest.set(parent, { leaves: [], forest: new Map() });
}
Expand All @@ -28,7 +30,7 @@ export function makeTree<T, K extends string>({
current.leaves.push(item);
}

console.log('collector', collector);
// console.debug('collector', collector);

const res: Tree<T> = [];
function rec(current: Tree<T>, currentCollector: TreeCollector<T>) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
* @typeParam T - The object type.
*/
export type SaveData<T extends { toJSON: () => unknown }> = ReturnType<T['toJSON']>;


export type ArrayKeys<T> = { [K in keyof T]: T[K] extends unknown[] ? K : never }[keyof T];

0 comments on commit a291bad

Please sign in to comment.