master
nikkiing 2 years ago
parent d0b7a52caa
commit 51ba9d1623
  1. 23
      pkg/tree/tree.go
  2. 58
      pkg/tree/tree_test.go

@ -1,10 +1,20 @@
package tree package tree
import (
"regexp"
)
type Node[T any] struct { type Node[T any] struct {
Data T `json:"data"` Data T `json:"data"`
Children []Node[T] `json:"children"` Children []Node[T] `json:"children"`
} }
func Length(code string) int {
re := regexp.MustCompile(`[A-Z]\d+`)
match := re.FindString(code)
return len(match)
}
// 适用 A00001B00001 层级结构 // 适用 A00001B00001 层级结构
// parent 父级字符串 // parent 父级字符串
// length A00001 一个节点长度 // length A00001 一个节点长度
@ -27,15 +37,18 @@ func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string)
} }
// 拆分层级结构 A00001B00001C00001 拆分为 ["A00001","A00001B00001","A00001B00001C00001"] // 拆分层级结构 A00001B00001C00001 拆分为 ["A00001","A00001B00001","A00001B00001C00001"]
// length A00001 一个节点长度 func Split(code string) (res []string) {
func Split(code string, length int) (res []string) { if code == "" {
if len(code) < length { return []string{}
}
l := Length(code)
if len(code) < l {
return return
} }
times := len(code) / length times := len(code) / l
for i := 0; i < times; i++ { for i := 0; i < times; i++ {
res = append(res, code[:len(code)-length*i]) res = append(res, code[:len(code)-l*i])
} }
return return
} }

@ -8,7 +8,6 @@ import (
func TestSplit(t *testing.T) { func TestSplit(t *testing.T) {
type args struct { type args struct {
code string code string
length int
} }
tests := []struct { tests := []struct {
name string name string
@ -19,14 +18,27 @@ func TestSplit(t *testing.T) {
name: "test1", name: "test1",
args: args{ args: args{
code: "A00001B00001C00001", code: "A00001B00001C00001",
length: 6,
}, },
wantRes: []string{"A00001B00001C00001", "A00001B00001", "A00001"}, wantRes: []string{"A00001B00001C00001", "A00001B00001", "A00001"},
}, },
{
name: "test2",
args: args{
code: "A001",
},
wantRes: []string{"A001"},
},
{
name: "test3",
args: args{
code: "",
},
wantRes: []string{},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotRes := Split(tt.args.code, tt.args.length); !reflect.DeepEqual(gotRes, tt.wantRes) { if gotRes := Split(tt.args.code); !reflect.DeepEqual(gotRes, tt.wantRes) {
t.Errorf("Split() = %v, want %v", gotRes, tt.wantRes) t.Errorf("Split() = %v, want %v", gotRes, tt.wantRes)
} }
}) })
@ -110,3 +122,43 @@ func TestTree(t *testing.T) {
}) })
} }
} }
func TestLength(t *testing.T) {
type args struct {
code string
}
tests := []struct {
name string
args args
want int
}{
{
name: "test1",
args: args{
code: "A00001B00001C00001",
},
want: 6,
},
{
name: "test2",
args: args{
code: "C0001D0001E00001",
},
want: 5,
},
{
name: "test3",
args: args{
code: "A0001",
},
want: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Length(tt.args.code); got != tt.want {
t.Errorf("length() = %v, want %v", got, tt.want)
}
})
}
}

Loading…
Cancel
Save