diff --git a/pkg/tree.go b/pkg/tree.go index 4b4f36d..8118d22 100644 --- a/pkg/tree.go +++ b/pkg/tree.go @@ -13,7 +13,7 @@ func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string) for _, v := range data { path := pathFunc(v) if len(path) < length { - return nil + return } p := path[:len(path)-length] if p == parent { @@ -25,3 +25,17 @@ func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string) } return } + +// 拆分层级结构 A00001B00001C00001 拆分为 ["A00001","A00001B00001","A00001B00001C00001"] +// length A00001 一个节点长度 +func Split(code string, length int) (res []string) { + if len(code) < length { + return + } + + times := len(code) / length + for i := 0; i < times; i++ { + res = append(res, code[:len(code)-length*i]) + } + return +}