# All files / tree utils.ts

100% Statements 64/64
100% Branches 13/13
100% Functions 2/2
100% Lines 64/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    651x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    9x
    9x
    9x
    9x
    9x
    9x
    11x
    11x
    11x
    9x
    9x
    11x
    9x
    9x
    9x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    105x
    86x
    86x
    19x
    19x
    19x
    19x
    105x
    105x
    23x
    23x
     import type { TreeItemNode, TreeData, DefaultTreeItemNode } from "./types";

    /**
     * This will get all the items from the provided itemId up to the root of the
     * tree that can be used for drag and drop behavior or building a breadcrumb
     * list.
     *
     * @param data - The flattened tree data to navigate.
     * @param itemId - The item id to start the search at.
     * @returns an ordered list of the current item followed by all the direct
     * parents of that item.
     * @remarks \@since 6.0.0 Renamed from `getItemsFrom` to `getTreeItemsFrom`.
     */
    export function getTreeItemsFrom<T extends TreeItemNode = DefaultTreeItemNode>(
      data: TreeData<T>,
      itemId: string | null
    ): readonly T[] {
      const items: T[] = [];
      let currentId = itemId;
      while (currentId) {
        const item = data[currentId];
        currentId = item?.parentId ?? null;
        if (item) {
          items.push(item);
        }
      }

      return items;
    }

    /**
     * Gets all the child items for a specific parent item id. If the `recursive`
     * argument is enabled, all children of the items will also be returned instead
     * of only the top level items.
     *
     * @param data - Either the flattened tree data or a list of all the tree data
     * to iterate over
     * @param parentId - The parent id to get children of
     * @param recursive - Boolean if the children's children should also be returned
     * @returns a list of all the items for a specific parent item id. Note: if the
     * recursive param is enabled, the list will be ordered so that the children of
     * a item will appear before the next item at the same level. So you either need
     * to sort by `parentId` or something else if you want a specific order.
     * @remarks \@since 6.0.0 Renamed from `getChildItems` to `getChildTreeItems`.
     */
    export function getChildTreeItems<T extends TreeItemNode = DefaultTreeItemNode>(
      data: TreeData<T> | readonly T[],
      parentId: string | null,
      recursive = false
    ): readonly T[] {
      const items = Array.isArray(data) ? data : Object.values(data);

      return items.reduce<T[]>((list, item) => {
        if (parentId !== item.parentId) {
          return list;
        }

        return [
          ...list,
          item,
          ...(recursive ? getChildTreeItems(items, item.itemId, recursive) : []),
        ];
      }, []);
    }