const traverse: any = (arr: any[], parentId: number, idName: string) =>
    arr.filter(node => node.parentId === parentId)
        .reduce((result, current) => [
            ...result,
            {
                ...current,
                children: traverse(arr, current[idName])
            }
        ], [])

export const parseTree = (arr: any[], idName: string = "id") =>
    arr.sort(({ order }) => order)
        .filter(({ parentId }) => !parentId)
        .map(node => ({
            ...node,
            children: traverse(arr, node[idName], idName)
        }))