|
|
@@ -10,3 +10,24 @@ export function getParamsBySearchParams<T = any>(query: URLSearchParams) { |
|
|
|
); |
|
|
|
return params as T; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function convertToTree<T = any>(source: T[], keyExtra: (item: T) => string | number, parentKeyExtra: (item: T) => string | number): T[] { |
|
|
|
const sourceMap: {[key: string | number]: T} = {}; |
|
|
|
for(const item of source) { |
|
|
|
const children = (item as any)['children'] ? (item as any)['children'] : []; |
|
|
|
(item as any).children = children; |
|
|
|
sourceMap[keyExtra(item)] = item; |
|
|
|
} |
|
|
|
const rootKeys = []; |
|
|
|
for(const item of source) { |
|
|
|
const parentKey = parentKeyExtra(item); |
|
|
|
if(parentKey in sourceMap) { |
|
|
|
(sourceMap[parentKey] as any).children?.push(item) |
|
|
|
} else { |
|
|
|
rootKeys.push(keyExtra(item)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return rootKeys.map(key => sourceMap[key]) |
|
|
|
} |