46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
export type LeaderTreeNode = {
|
|
title?: string
|
|
value?: string | number
|
|
children?: LeaderTreeNode[]
|
|
}
|
|
|
|
export function flattenLeaderTree(
|
|
nodes: LeaderTreeNode[],
|
|
prefixTitle: string[] = []
|
|
): Array<{ label: string; value: string }> {
|
|
const result: Array<{ label: string; value: string }> = []
|
|
|
|
nodes.forEach((node) => {
|
|
const title = node.title ?? ""
|
|
const nextPrefix = title ? [...prefixTitle, title] : prefixTitle
|
|
const children = node.children ?? []
|
|
const valueRaw = node.value
|
|
|
|
if (children.length > 0) {
|
|
result.push(...flattenLeaderTree(children, nextPrefix))
|
|
return
|
|
}
|
|
|
|
if (valueRaw === undefined || valueRaw === null) return
|
|
if (typeof valueRaw === "string" && valueRaw.includes("-")) return
|
|
|
|
result.push({
|
|
label: nextPrefix.join(" / "),
|
|
value: String(valueRaw),
|
|
})
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
export function toNumberArray(values?: string[]) {
|
|
if (!values || values.length === 0) return undefined
|
|
const numbers = values.map((v) => Number(v)).filter((v) => Number.isFinite(v))
|
|
return numbers.length > 0 ? numbers : undefined
|
|
}
|
|
|
|
export function weekDayText(day: number) {
|
|
const list = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
|
|
return list[day] ?? "-"
|
|
}
|