function getTreeFromFlatData({
  flatData,
  getKey = node => node.id,
  getParentKey = node => node.parentId,
  rootKey = '0',
}) {
  if (!flatData) {
    return [];
  }

  const childrenToParents = {};
  flatData.forEach(child => {
    const parentKey = getParentKey(child);

    if (parentKey in childrenToParents) {
      childrenToParents[parentKey].push(child);
    } else {
      childrenToParents[parentKey] = [child];
    }
  });

  if (!(rootKey in childrenToParents)) {
    return [];
  }

  const trav = parent => {
    const parentKey = getKey(parent);
    if (parentKey in childrenToParents) {
      return {
        ...parent,
        children: childrenToParents[parentKey].map(child => trav(child)),
      };
    }

    return { ...parent };
  };

  return childrenToParents[rootKey].map(child => trav(child));
}
Generate a tree structure from flat data.

@param {!Object[]} flatData
@param {!function=} getKey - Function to get the key from the nodeData
@param {!function=} getParentKey - Function to get the parent key from the nodeData
@param {string|number=} rootKey - The value returned by `getParentKey` that corresponds to the root node.
                                 For example, if your nodes have id 1-99, you might use rootKey = 0

@return {Object[]} treeData - The flat data represented as a tree