This commit is contained in:
xing 2023-01-27 00:52:59 +08:00
parent 31409b7594
commit 4e84bf21a0
5 changed files with 169 additions and 5 deletions

View File

@ -20,6 +20,32 @@ func FilterAndMap[N any, T any](arr []T, fn func(T) (N, bool)) (r []N) {
return return
} }
func Walk[T any](arr []T, fn func(*T)) {
for i := 0; i < len(arr); i++ {
fn(&arr[i])
}
}
func First[T any](arr []T, fn func(T) bool) (int, T) {
for i, t := range arr {
if fn(t) {
return i, t
}
}
var r T
return -1, r
}
func Last[T any](arr []T, fn func(T) bool) (int, T) {
for i := len(arr) - 1; i > 0; i-- {
if fn(arr[i]) {
return i, arr[i]
}
}
var r T
return -1, r
}
func Filter[T any](arr []T, fn func(T) bool) []T { func Filter[T any](arr []T, fn func(T) bool) []T {
var r []T var r []T
for _, t := range arr { for _, t := range arr {

View File

@ -1,6 +1,7 @@
package slice package slice
import ( import (
"fmt"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
"reflect" "reflect"
"testing" "testing"
@ -527,3 +528,126 @@ func TestToAnySlice(t *testing.T) {
}) })
} }
} }
func TestFirst(t *testing.T) {
type args[T int] struct {
arr []T
fn func(T) bool
}
type testCase[T int] struct {
name string
args args[T]
want int
want1 T
}
tests := []testCase[int]{
{
name: "t1",
args: args[int]{
arr: number.Range(1, 10, 1),
fn: func(t int) bool {
return t == 5
},
},
want: 4,
want1: 5,
}, {
name: "t2",
args: args[int]{
arr: number.Range(1, 10, 1),
fn: func(t int) bool {
return t == 11
},
},
want: -1,
want1: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := First(tt.args.arr, tt.args.fn)
if got != tt.want {
t.Errorf("First() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("First() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestLast(t *testing.T) {
type args[T int] struct {
arr []T
fn func(T) bool
}
type testCase[T int] struct {
name string
args args[T]
want int
want1 T
}
tests := []testCase[int]{
{
name: "t1",
args: args[int]{
arr: []int{1, 55, 5, 5, 5, 5, 22},
fn: func(t int) bool {
return t == 5
},
},
want: 5,
want1: 5,
}, {
name: "t2",
args: args[int]{
arr: number.Range(1, 10, 1),
fn: func(t int) bool {
return t == 11
},
},
want: -1,
want1: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := Last(tt.args.arr, tt.args.fn)
if got != tt.want {
t.Errorf("Last() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("Last() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestWalk(t *testing.T) {
type args[T int] struct {
arr []T
fn func(*T)
}
type testCase[T int] struct {
name string
args args[T]
}
tests := []testCase[int]{
{
name: "t1",
args: args[int]{
arr: number.Range(1, 10, 1),
fn: func(i *int) {
*i = *i * 2
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fmt.Println(tt.args.arr)
Walk(tt.args.arr, tt.args.fn)
fmt.Println(tt.args.arr)
})
}
}

View File

@ -140,6 +140,7 @@ func (h *indexHandle) parseParams() (err error) {
if category == "" { if category == "" {
category = h.c.Param("tag") category = h.c.Param("tag")
if category != "" { if category != "" {
h.scene = plugins.Tag
allNames := cache.AllTagsNames(h.c) allNames := cache.AllTagsNames(h.c)
if _, ok := allNames[category]; !ok { if _, ok := allNames[category]; !ok {
return errors.New(str.Join("not exists tag ", category)) return errors.New(str.Join("not exists tag ", category))
@ -148,6 +149,7 @@ func (h *indexHandle) parseParams() (err error) {
h.header = fmt.Sprintf("标签: <span>%s</span>", category) h.header = fmt.Sprintf("标签: <span>%s</span>", category)
} }
} else { } else {
h.scene = plugins.Category
allNames := cache.AllCategoryNames(h.c) allNames := cache.AllCategoryNames(h.c)
if _, ok := allNames[category]; !ok { if _, ok := allNames[category]; !ok {
return errors.New(str.Join("not exists category ", category)) return errors.New(str.Join("not exists category ", category))
@ -189,7 +191,6 @@ func (h *indexHandle) parseParams() (err error) {
"left join", "wp_terms d", "c.term_id=d.term_id", "left join", "wp_terms d", "c.term_id=d.term_id",
}) })
h.setTitleLR(category, wpconfig.Options.Value("blogname")) h.setTitleLR(category, wpconfig.Options.Value("blogname"))
h.scene = plugins.Category
} }
s := h.c.Query("s") s := h.c.Query("s")
if s != "" && strings.Replace(s, " ", "", -1) != "" { if s != "" && strings.Replace(s, " ", "", -1) != "" {

View File

@ -8,6 +8,7 @@ const (
Home = iota + 1 Home = iota + 1
Archive Archive
Category Category
Tag
Search Search
Detail Detail
@ -21,6 +22,7 @@ var IndexSceneMap = map[int]struct{}{
Home: {}, Home: {},
Archive: {}, Archive: {},
Category: {}, Category: {},
Tag: {},
Search: {}, Search: {},
} }

View File

@ -64,9 +64,19 @@ func Hook(status int, c *gin.Context, h gin.H, scene, stats int) {
d = 0 d = 0
} }
} else if scene == plugins.Category { } else if scene == plugins.Category {
cate := slice.Filter(cache.Categories(c), func(my models.TermsMy) bool { cat := c.Param("category")
return my.Name == c.Param("category") _, cate := slice.First(cache.Categories(c), func(my models.TermsMy) bool {
})[0] return my.Name == cat
})
d = int(cate.Terms.TermId)
if cate.Slug[0] != '%' {
s = cate.Slug
}
} else if scene == plugins.Tag {
cat := c.Param("tag")
_, cate := slice.First(cache.Tags(c), func(my models.TermsMy) bool {
return my.Name == cat
})
d = int(cate.Terms.TermId) d = int(cate.Terms.TermId)
if cate.Slug[0] != '%' { if cate.Slug[0] != '%' {
s = cate.Slug s = cate.Slug
@ -175,7 +185,7 @@ func bodyClass(scene, d int, a ...any) string {
} else { } else {
s = "search-no-results" s = "search-no-results"
} }
} else if scene == plugins.Category { } else if scene == plugins.Category || scene == plugins.Tag {
s = fmt.Sprintf("category-%d %v", d, a[0]) s = fmt.Sprintf("category-%d %v", d, a[0])
} else if scene == plugins.Detail { } else if scene == plugins.Detail {
s = fmt.Sprintf("postid-%d", d) s = fmt.Sprintf("postid-%d", d)
@ -184,6 +194,7 @@ func bodyClass(scene, d int, a ...any) string {
plugins.Home: "home blog ", plugins.Home: "home blog ",
plugins.Archive: "archive date page-two-column", plugins.Archive: "archive date page-two-column",
plugins.Category: str.Join("archive category page-two-column ", s), plugins.Category: str.Join("archive category page-two-column ", s),
plugins.Tag: str.Join("archive category page-two-column ", s),
plugins.Search: str.Join("search ", s), plugins.Search: str.Join("search ", s),
plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s), plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s),
}[scene] }[scene]