service.xpcool.com/common/tools/slicex/slicex.go
夏犀麟 4aca0c7f6f
Some checks failed
Build and Deploy (service.xpcool.com) / build-and-deploy (push) Failing after 5m7s
feat(house): 实现看房模块后端功能
- 新增 9 张房屋相关数据表(社区/楼宇/房源/价格快照/交易/设施/社区设施/学区/偏好)
- 添加菜单权限种子数据并绑定超级管理员角色
- 生成 DAO 层代码和实体对象
- 实现房屋模块 API 接口(社区/房源/看板)和控制器服务层
- 支持多平台软关联匹配、笋盘标记和低可信度标记功能
- 更新超级管理员账号为 xxcool/xxCool@2026
- 调整 RBAC 菜单结构,移除管理员管理功能,新增日志管理菜单
- 修复 RBAC 安全漏洞,确保禁用角色权限失效
- 重构认证模块,将登录相关接口迁移到统一包结构下
- 移除废弃的管理模块和工具类接口定义
- 为通用工具包添加中文注释和文档说明
2026-08-26 23:42:38 +08:00

65 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package slicex 基于标准库提供通用切片工具Go 1.23+)。
// (续上一行)
package slicex
import (
"slices"
)
// Contains 判断 items 中是否包含 v。
func Contains[T comparable](items []T, v T) bool {
return slices.Contains(items, v)
}
// Unique 去除 items 中重复元素,保持首次出现顺序。
func Unique[T comparable](items []T) []T {
seen := make(map[T]struct{}, len(items))
out := make([]T, 0, len(items))
for _, v := range items {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
out = append(out, v)
}
return out
}
// Chunk 将 items 切分为最多 size 个元素的子切片,
// size <= 0 或 items 为空时返回 nil。
func Chunk[T any](items []T, size int) [][]T {
if size <= 0 || len(items) == 0 {
return nil
}
out := make([][]T, 0, (len(items)+size-1)/size)
for len(items) > 0 {
n := size
if len(items) < n {
n = len(items)
}
out = append(out, items[:n])
items = items[n:]
}
return out
}
// Map 对每个元素应用 fn 并返回结果。
func Map[T, R any](items []T, fn func(T) R) []R {
out := make([]R, len(items))
for i, v := range items {
out[i] = fn(v)
}
return out
}
// Filter 返回 fn 为 true 的元素,保持原顺序。
func Filter[T any](items []T, fn func(T) bool) []T {
out := make([]T, 0, len(items))
for _, v := range items {
if fn(v) {
out = append(out, v)
}
}
return out
}