From ccc02399ef19a7444151f63058ae9abf281a36d6 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 28 Feb 2023 19:19:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/slice/sort.go | 24 +++++++++++- helper/slice/sort_test.go | 37 ++++++++++++++++++- internal/pkg/constraints/const.go | 1 + internal/theme/common/common.go | 14 +++++-- internal/theme/twentyfifteen/twentyfifteen.go | 2 +- .../theme/twentyseventeen/twentyseventeen.go | 2 +- 6 files changed, 72 insertions(+), 8 deletions(-) diff --git a/helper/slice/sort.go b/helper/slice/sort.go index 2685c98..63ea03f 100644 --- a/helper/slice/sort.go +++ b/helper/slice/sort.go @@ -1,6 +1,14 @@ package slice -import "sort" +import ( + "golang.org/x/exp/constraints" + "sort" +) + +const ( + ASC = iota + DESC +) type anyArr[T any] struct { data []T @@ -28,6 +36,7 @@ func SortSelf[T any](arr []T, fn func(i, j T) bool) { return } +// Sort fn 中i>j 为降序,反之为升序 func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) { r = make([]T, len(arr)) copy(r, arr) @@ -38,3 +47,16 @@ func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) { sort.Sort(slice) return } + +func Sorts[T constraints.Ordered](a []T, order int) { + slice := anyArr[T]{ + data: a, + fn: func(i, j T) bool { + if order == DESC { + return i > j + } + return i < j + }, + } + sort.Sort(slice) +} diff --git a/helper/slice/sort_test.go b/helper/slice/sort_test.go index 6cf7e96..7e0f46c 100644 --- a/helper/slice/sort_test.go +++ b/helper/slice/sort_test.go @@ -1,6 +1,8 @@ package slice import ( + "fmt" + "golang.org/x/exp/constraints" "reflect" "testing" ) @@ -109,4 +111,37 @@ func TestSort(t *testing.T) { } }) } -} \ No newline at end of file +} + +func TestSorts(t *testing.T) { + type args[T constraints.Ordered] struct { + a []T + order int + } + type testCase[T constraints.Ordered] struct { + name string + args args[T] + } + tests := []testCase[int]{ + { + name: "asc", + args: args[int]{ + a: []int{1, -3, 6, 10, 3, 2, 8}, + order: ASC, + }, //[-3 1 2 3 6 8 10] + }, + { + name: "desc", + args: args[int]{ + a: []int{1, -3, 6, 10, 3, 2, 8}, + order: DESC, + }, //[10 8 6 3 2 1 -3] + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + Sorts(tt.args.a, tt.args.order) + fmt.Println(tt.args.a) + }) + } +} diff --git a/internal/pkg/constraints/const.go b/internal/pkg/constraints/const.go index 448727a..ff7452d 100644 --- a/internal/pkg/constraints/const.go +++ b/internal/pkg/constraints/const.go @@ -13,6 +13,7 @@ const ( Error404 ParamError InternalErr + AllStats Defaults = "default" diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index 321c05b..9dd934b 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -28,7 +28,7 @@ type Handle struct { Class []string Components map[string][]Components ThemeMods wpconfig.ThemeMods - HandleFns []HandleFn[*Handle] + HandleFns map[int][]HandleFn[*Handle] } func NewHandle(c *gin.Context, scene int, theme string) *Handle { @@ -56,8 +56,8 @@ func NewComponents(fn func(*Handle) string, order int) Components { return Components{Fn: fn, Order: order} } -func (h *Handle) PushHandleFn(fns ...HandleFn[*Handle]) { - h.HandleFns = append(h.HandleFns, fns...) +func (h *Handle) PushHandleFn(stats int, fns ...HandleFn[*Handle]) { + h.HandleFns[stats] = append(h.HandleFns[stats], fns...) } func (h *Handle) AddComponent(name string, fn func(*Handle) string) { @@ -84,7 +84,13 @@ func (h *Handle) GetPassword() { } func (h *Handle) ExecHandleFns() { - for _, fn := range h.HandleFns { + calls, ok := h.HandleFns[h.Stats] + if ok { + for _, call := range calls { + call(h) + } + } + for _, fn := range h.HandleFns[constraints.AllStats] { fn(h) } } diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 8850ee6..227510f 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -41,7 +41,7 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) { common.NewComponents(CalCustomBackGround, 10), common.NewComponents(colorSchemeCss, 10), ) - h.PushHandleFn(customHeader) + h.PushHandleFn(constraints.AllStats, customHeader) switch h.Scene { case constraints.Detail: detail(next, h.Detail) diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 50bb475..c11d50e 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -36,7 +36,7 @@ func Hook(h *common.Handle) { func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) { h.WidgetAreaData() h.GetPassword() - h.PushHandleFn(calClass) + h.PushHandleFn(constraints.AllStats, calClass) h.PushHeadScript( common.NewComponents(colorScheme, 10), )