Skip to content

Commit 6a8f8d8

Browse files
committed
Fix comments and make getTreeFromFlatData a bit easier to use
1 parent 5f29231 commit 6a8f8d8

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/utils/tree-data-utils.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ export function getVisibleNodeInfoAtIndex({ treeData, index: targetIndex, getNod
243243
* @param {!function} getNodeKey - Function to get the key from the nodeData and tree index
244244
* @param {function} callback - Function to call on each node
245245
* @param {boolean=} ignoreCollapsed - Ignore children of nodes without `expanded` set to `true`
246+
*
247+
* @return void
246248
*/
247249
export function walk({ treeData, getNodeKey, callback, ignoreCollapsed = true }) {
248250
if (!treeData || treeData.length < 1) {
@@ -269,6 +271,8 @@ export function walk({ treeData, getNodeKey, callback, ignoreCollapsed = true })
269271
* @param {!function} getNodeKey - Function to get the key from the nodeData and tree index
270272
* @param {function} callback - Function to call on each node
271273
* @param {boolean=} ignoreCollapsed - Ignore children of nodes without `expanded` set to `true`
274+
*
275+
* @return {Object[]} changedTreeData - The changed tree data
272276
*/
273277
export function map({ treeData, getNodeKey, callback, ignoreCollapsed = true }) {
274278
if (!treeData || treeData.length < 1) {
@@ -292,6 +296,8 @@ export function map({ treeData, getNodeKey, callback, ignoreCollapsed = true })
292296
*
293297
* @param {!Object[]} treeData - Tree data
294298
* @param {?boolean} expanded - Whether the node is expanded or not
299+
*
300+
* @return {Object[]} changedTreeData - The changed tree data
295301
*/
296302
export function toggleExpandedForAll({ treeData, expanded = true }) {
297303
return map({
@@ -311,7 +317,7 @@ export function toggleExpandedForAll({ treeData, expanded = true }) {
311317
* @param {!function} getNodeKey - Function to get the key from the nodeData and tree index
312318
* @param {boolean=} ignoreCollapsed - Ignore children of nodes without `expanded` set to `true`
313319
*
314-
* @return {Object} changedTreeData - The updated tree data
320+
* @return {Object[]} changedTreeData - The changed tree data
315321
*/
316322
export function changeNodeAtPath({ treeData, path, newNode, getNodeKey, ignoreCollapsed = true }) {
317323
const RESULT_MISS = 'RESULT_MISS';
@@ -395,7 +401,7 @@ export function changeNodeAtPath({ treeData, path, newNode, getNodeKey, ignoreCo
395401
* @param {!function} getNodeKey - Function to get the key from the nodeData and tree index
396402
* @param {boolean=} ignoreCollapsed - Ignore children of nodes without `expanded` set to `true`
397403
*
398-
* @return {Object} changedTreeData - The updated tree data
404+
* @return {Object[]} changedTreeData - The tree data with the node removed
399405
*/
400406
export function removeNodeAtPath({ treeData, path, getNodeKey, ignoreCollapsed = true }) {
401407
return changeNodeAtPath({
@@ -449,7 +455,7 @@ export function getNodeAtPath({ treeData, path, getNodeKey, ignoreCollapsed = tr
449455
* @param {boolean=} expandParent - If true, expands the parentNode specified by parentPath
450456
*
451457
* @return {Object} result
452-
* @return {Object} result.treeData - The updated tree data
458+
* @return {Object[]} result.treeData - The updated tree data
453459
* @return {number} result.treeIndex - The tree index at which the node was inserted
454460
*/
455461
export function addNodeUnderParent({
@@ -670,6 +676,10 @@ function addNodeAtDepthAndIndex({
670676
* @param {!Object} newNode - The node to insert into the tree
671677
* @param {boolean=} ignoreCollapsed - Ignore children of nodes without `expanded` set to `true`
672678
* @param {boolean=} expandParent - If true, expands the parent of the inserted node
679+
*
680+
* @return {Object} result
681+
* @return {Object[]} result.treeData - The tree data with the node added
682+
* @return {number} result.treeIndex - The tree index at which the node was inserted
673683
*/
674684
export function insertNode({
675685
treeData,
@@ -744,16 +754,18 @@ export function getFlatDataFromTree({ treeData, getNodeKey, ignoreCollapsed = tr
744754
* Generate a tree structure from flat data.
745755
*
746756
* @param {!Object[]} flatData
747-
* @param {!function} getKey - Function to get the key from the nodeData
748-
* @param {!function} getParentKey - Function to get the parent key from the nodeData
757+
* @param {!function=} getKey - Function to get the key from the nodeData
758+
* @param {!function=} getParentKey - Function to get the parent key from the nodeData
759+
* @param {string|number=} rootKey - The value returned by `getParentKey` that corresponds to the root node.
760+
* For example, if your nodes have id 1-99, you might use rootKey = 0
749761
*
750762
* @return {Object[]} treeData - The flat data represented as a tree
751763
*/
752764
export function getTreeFromFlatData({
753765
flatData,
754-
getKey,
755-
getParentKey,
756-
rootKey,
766+
getKey = (node => node.id),
767+
getParentKey = (node => node.parentId),
768+
rootKey = '0',
757769
}) {
758770
if (!flatData) {
759771
return [];
@@ -808,7 +820,7 @@ export function isDescendant(older, younger) {
808820
* @param {!Object} node - Node in the tree
809821
* @param {?number} depth - The current depth
810822
*
811-
* @return {boolean}
823+
* @return {number} maxDepth - The deepest depth in the tree
812824
*/
813825
export function getDepth(node, depth = 0) {
814826
if (!node.children) {

0 commit comments

Comments
 (0)