- common/tools 工具模块,10 个子功能:md5/cryptox/uuid/random/timex/convertx/strx/slicex/ip/filex(薄封装 GoFrame 内置组件) - AGENTS.md 项目智能体说明书(架构、规范、命令、记忆体系索引) - docs/change-log/2026-08-24.md 本次请求与变更记录 - .gitignore 排除 .workbuddy/;PROJECT_STRUCTURE.md 补充目录树
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
// Package strx provides string helpers on top of gstr, including naming
|
|
// conversion and sensitive-data masking.
|
|
package strx
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
)
|
|
|
|
// SnakeCase converts s to snake_case, e.g. "UserName" -> "user_name".
|
|
func SnakeCase(s string) string {
|
|
return gstr.CaseSnake(s)
|
|
}
|
|
|
|
// CamelCase converts s to CamelCase, e.g. "user_name" -> "UserName".
|
|
func CamelCase(s string) string {
|
|
return gstr.CaseCamel(s)
|
|
}
|
|
|
|
// LowerCamelCase converts s to lowerCamelCase, e.g. "user_name" -> "userName".
|
|
func LowerCamelCase(s string) string {
|
|
return gstr.CaseCamelLower(s)
|
|
}
|
|
|
|
// IsEmpty reports whether s is empty or whitespace-only.
|
|
func IsEmpty(s string) bool {
|
|
return strings.TrimSpace(s) == ""
|
|
}
|
|
|
|
// MaskPhone masks a phone number, keeping the first 3 and last 4 characters.
|
|
// e.g. "13812345678" -> "138****5678".
|
|
func MaskPhone(s string) string {
|
|
if len(s) < 7 {
|
|
return s
|
|
}
|
|
return s[:3] + "****" + s[len(s)-4:]
|
|
}
|
|
|
|
// MaskIDCard masks a Chinese ID card number, keeping the first 6 and last 4
|
|
// characters. e.g. "110101199003074512" -> "110101********4512".
|
|
func MaskIDCard(s string) string {
|
|
if len(s) < 10 {
|
|
return s
|
|
}
|
|
return s[:6] + "********" + s[len(s)-4:]
|
|
}
|
|
|
|
// MaskName masks a Chinese name, keeping only the first character.
|
|
// e.g. "张三丰" -> "张**".
|
|
func MaskName(s string) string {
|
|
r := []rune(s)
|
|
if len(r) <= 1 {
|
|
return s
|
|
}
|
|
return string(r[0]) + strings.Repeat("*", len(r)-1)
|
|
}
|