-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update example app's sample data generator to generate node…
…s with different id types
- Loading branch information
1 parent
45baaac
commit dd564cc
Showing
8 changed files
with
38 additions
and
32 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
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
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
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
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,32 +1,38 @@ | ||
interface TreeNode { | ||
id: string; | ||
export interface TreeNode<ID> { | ||
id: ID; | ||
name: string; | ||
children?: TreeNode[]; | ||
children?: TreeNode<ID>[]; | ||
} | ||
|
||
// This function generates a TreeNode with a random number of children. | ||
function generateNode(id: string, level: number, maxLevel: number, maxChildren: number): TreeNode { | ||
const node: TreeNode = { | ||
id: id, | ||
name: `Node ${id}`, | ||
function generateNode<ID = string>(id: ID, idx: number, level: number, maxLevel: number, maxChildren: number, nextID: (prev: ID, idx: number, parent?: ID) => ID, parentID?: ID): TreeNode<ID> { | ||
let nid = nextID(id, idx, parentID); | ||
const node: TreeNode<ID> = { | ||
id: nid, | ||
name: `Node ${nid}`, | ||
}; | ||
|
||
if (level < maxLevel) { | ||
const numChildren = Math.floor(Math.random() * maxChildren) + 1; // generates a random number between 1 and maxChildren | ||
node.children = []; | ||
for (let i = 1; i <= numChildren; i++) { | ||
node.children.push(generateNode(`${id}.${i}`, level + 1, maxLevel, maxChildren)); | ||
node.children.push(generateNode(nid, i, level + 1, maxLevel, maxChildren, nextID, node.id)); | ||
nid = nextID(nid, idx, node.id); | ||
} | ||
} | ||
|
||
return node; | ||
} | ||
|
||
// This function generates a list of TreeNodes | ||
export function generateTreeList(num: number, maxLevel: number, maxChildren: number): TreeNode[] { | ||
let result: TreeNode[] = []; | ||
export function generateTreeList<ID = string>(num: number, maxLevel: number, maxChildren: number, nextID: (prev: ID, idx: number) => ID, initialValue: ID): TreeNode<ID>[] { | ||
let result: TreeNode<ID>[] = []; | ||
let curID = initialValue; | ||
for (let i = 1; i <= num; i++) { | ||
result.push(generateNode(`${i}`, 1, maxLevel, maxChildren)); | ||
result.push(generateNode(curID, i, 1, maxLevel, maxChildren, nextID)); | ||
curID = nextID(curID, i); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
export const defaultID = (_prev: string, idx: number, parentID?: string) => `${parentID ? parentID + '.' : ''}${idx}` |