tree-select icon indicating copy to clipboard operation
tree-select copied to clipboard

DFS这里是否能调整为尾递归

Open biubiukam opened this issue 1 year ago • 0 comments

https://github.com/react-component/tree-select/blob/a000435807512c5799292abbd13bfe4649334bae/src/hooks/useFilterTreeData.ts#L40

function dig(list: DefaultOptionType[], keepAll: boolean = false): DefaultOptionType[] {
  function digRecursive(list: DefaultOptionType[], result: DefaultOptionType[]): DefaultOptionType[] {
    if (list.length === 0) {
      return result;
    }

    const dataNode = list[0];
    const children = dataNode[fieldChildren];
    const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode));
    const childList = dig(children || [], match);

    if (match || childList.length) {
      result.push({
        ...dataNode,
        isLeaf: undefined,
        [fieldChildren]: childList,
      });
    }

    return digRecursive(list.slice(1), result);
  }

  return digRecursive(list, []);
}

biubiukam avatar Jan 09 '24 06:01 biubiukam