-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { SourceDataAccessors } from "./source-data-accessor"; | ||
|
||
export function createDefaultAccessors<T>(): SourceDataAccessors<T> { | ||
return { | ||
id: (d: T) => { | ||
if (d && typeof d === "object" && "id" in d) { | ||
return d.id as string; | ||
} else { | ||
throw new Error("No id found for node data. Specify an id accessor."); | ||
} | ||
}, | ||
children: (d: T) => { | ||
if (d && typeof d === "object" && "children" in d) { | ||
return d.children as T[]; | ||
} else { | ||
return null; | ||
} | ||
}, | ||
isLeaf: (d: T) => { | ||
if (d && typeof d === "object" && "children" in d) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}, | ||
sortBy: (_d: T) => 0, | ||
sortOrder: "asc", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,46 @@ | ||
import { createDefaultAccessors } from "./default-accessors"; | ||
|
||
export type SourceDataAccessors<T> = { | ||
id: (d: T) => string; | ||
children: (d: T) => T[]; | ||
children: (d: T) => T[] | null; | ||
isLeaf: (d: T) => boolean; | ||
sortBy: (d: T) => number | string | boolean; | ||
sortOrder: "asc" | "desc"; | ||
}; | ||
|
||
export class SourceDataAccessor<T> { | ||
constructor(public accessors: Partial<SourceDataAccessors<T>> = {}) {} | ||
access: SourceDataAccessors<T>; | ||
|
||
constructor(accessors: Partial<SourceDataAccessors<T>> = {}) { | ||
this.access = { ...createDefaultAccessors(), ...accessors }; | ||
} | ||
|
||
getId(d: T): string { | ||
if (this.accessors.id) { | ||
return this.accessors.id(d); | ||
} else if (d && typeof d === "object" && "id" in d) { | ||
return d.id as string; | ||
} else { | ||
throw new Error("No id found for node data. Specify an id accessor."); | ||
} | ||
return this.access.id(d); | ||
} | ||
|
||
getChildren(d: T): null | T[] { | ||
if (this.accessors.children) { | ||
return this.accessors.children(d); | ||
} else if (d && typeof d === "object" && "children" in d) { | ||
return d.children as T[]; | ||
} else { | ||
return null; | ||
} | ||
return this.access.children(d); | ||
} | ||
|
||
getIsLeaf(d: T): boolean { | ||
if (this.accessors.isLeaf) { | ||
return this.accessors.isLeaf(d); | ||
} else { | ||
return !this.getChildren(d); | ||
} | ||
return this.access.isLeaf(d); | ||
} | ||
|
||
sort(array: T[]) { | ||
return array.sort((a, b) => { | ||
const first = this.access.sortBy(a); | ||
const second = this.access.sortBy(b); | ||
|
||
if (this.asc) { | ||
return first < second ? -1 : 1; | ||
} else { | ||
return first > second ? -1 : 1; | ||
} | ||
}); | ||
} | ||
|
||
get asc() { | ||
return this.access.sortOrder === "asc"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters