From cd91e16b2641f32e2f4ec3ad20b7be52fe665907 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 11 May 2023 10:06:32 +0800 Subject: [PATCH] doc: update document for strutil and convertor package --- docs/convertor.md | 38 +++++++++++++- docs/convertor_zh-CN.md | 39 +++++++++++++- docs/strutil.md | 111 +++++++++++++++++++++++++++++++++++++++- docs/strutil_zh-CN.md | 108 ++++++++++++++++++++++++++++++++++++++ strutil/string.go | 8 +-- 5 files changed, 294 insertions(+), 10 deletions(-) diff --git a/docs/convertor.md b/docs/convertor.md index 0ce83aa..394858a 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -40,6 +40,7 @@ import ( - [DecodeByte](#DecodeByte) - [DeepClone](#DeepClone) - [CopyProperties](#CopyProperties) +- [ToInterface](#ToInterface)
@@ -629,7 +630,6 @@ func main() { } ``` - ### DeepClone

Creates a deep copy of passed item, can't clone unexported field of struct.

@@ -694,7 +694,6 @@ func main() { } ``` - ### CopyProperties

Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.

@@ -772,4 +771,39 @@ func main() { // 127.0.0.1 // 3 } +``` + +### ToInterface + +

Converts reflect value to its interface type.

+ +Signature: + +```go +func ToInterface(v reflect.Value) (value interface{}, ok bool) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + val := reflect.ValueOf("abc") + iVal, ok := convertor.ToInterface(val) + + fmt.Printf("%T\n", iVal) + fmt.Printf("%v\n", iVal) + fmt.Println(ok) + + // Output: + // string + // abc + // true +} ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index 40bb7a6..aa6908f 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -40,7 +40,7 @@ import ( - [DecodeByte](#DecodeByte) - [DeepClone](#DeepClone) - [CopyProperties](#CopyProperties) - +- [ToInterface](#ToInterface)
@@ -771,4 +771,39 @@ func main() { // 127.0.0.1 // 3 } -``` \ No newline at end of file +``` + +### ToInterface + +

将反射值转换成对应的interface类型。

+ +函数签名: + +```go +func ToInterface(v reflect.Value) (value interface{}, ok bool) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + val := reflect.ValueOf("abc") + iVal, ok := convertor.ToInterface(val) + + fmt.Printf("%T\n", iVal) + fmt.Printf("%v\n", iVal) + fmt.Println(ok) + + // Output: + // string + // abc + // true +} +``` diff --git a/docs/strutil.md b/docs/strutil.md index 331cff0..aee09c3 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -52,6 +52,9 @@ import ( - [HasPrefixAny](#HasPrefixAny) - [HasSuffixAny](#HasSuffixAny) - [IndexOffset](#IndexOffset) +- [ReplaceWithMap](#ReplaceWithMap) +- [Trim](#Trim) +- [SplitAndTrim](#SplitAndTrim)
@@ -1076,7 +1079,7 @@ func main() { Signature: ```go -func HasPrefixAny(str string, prefixes []string) bool +func ReplaceWithMap(str string, replaces map[string]string) string ``` Example: @@ -1163,7 +1166,7 @@ func main() { fmt.Println(result3) fmt.Println(result4) fmt.Println(result5) - + // Output: // 12 // 1 @@ -1172,3 +1175,107 @@ func main() { // -1 } ``` + +### ReplaceWithMap + +

Returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.

+ +Signature: + +```go +func ReplaceWithMap(str string, replaces map[string]string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := "ac ab ab ac" + replaces := map[string]string{ + "a": "1", + "b": "2", + } + + result := strutil.ReplaceWithMap(str, replaces) + + fmt.Println(result) + // Output: + // 1c 12 12 1c +} +``` + +### Trim + +

Strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.

+ +Signature: + +```go +func Trim(str string, characterMask ...string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := strutil.Trim("\nabcd") + + str := "$ ab cd $ " + + result2 := strutil.Trim(str) + result3 := strutil.Trim(str, "$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // abcd + // $ ab cd $ + // ab cd +} +``` + + +### SplitAndTrim + +

Splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of this array. It ignores the elements which are empty after Trim.

+ +Signature: + +```go +func SplitAndTrim(str, delimiter string, characterMask ...string) []string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := " a,b, c,d,$1 " + + result1 := strutil.SplitAndTrim(str, ",") + result2 := strutil.SplitAndTrim(str, ",", "$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // [a b c d $1] + // [a b c d 1] +} +``` diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index d96c746..eab9464 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -52,6 +52,9 @@ import ( - [HasPrefixAny](#HasPrefixAny) - [HasSuffixAny](#HasSuffixAny) - [IndexOffset](#IndexOffset) +- [ReplaceWithMap](#ReplaceWithMap) +- [Trim](#Trim) +- [SplitAndTrim](#SplitAndTrim)
@@ -1171,3 +1174,108 @@ func main() { // -1 } ``` + + +### ReplaceWithMap + +

返回`str`的副本,以无序的方式被map替换,区分大小写。

+ +函数签名: + +```go +func ReplaceWithMap(str string, replaces map[string]string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := "ac ab ab ac" + replaces := map[string]string{ + "a": "1", + "b": "2", + } + + result := strutil.ReplaceWithMap(str, replaces) + + fmt.Println(result) + // Output: + // 1c 12 12 1c +} +``` + +### Trim + +

从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。

+ +函数签名: + +```go +func Trim(str string, characterMask ...string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := strutil.Trim("\nabcd") + + str := "$ ab cd $ " + + result2 := strutil.Trim(str) + result3 := strutil.Trim(str, "$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // abcd + // $ ab cd $ + // ab cd +} +``` + + +### SplitAndTrim + +

将字符串str按字符串 =delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。

+ +函数签名: + +```go +func SplitAndTrim(str, delimiter string, characterMask ...string) []string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := " a,b, c,d,$1 " + + result1 := strutil.SplitAndTrim(str, ",") + result2 := strutil.SplitAndTrim(str, ",", "$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // [a b c d $1] + // [a b c d 1] +} +``` diff --git a/strutil/string.go b/strutil/string.go index f9ca53e..947a6b1 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -407,7 +407,7 @@ func IsBlank(str string) bool { return true } -// HasPrefixAny check if a string starts with any of an array of specified strings. +// HasPrefixAny check if a string starts with any of a slice of specified strings. // Play: https://go.dev/play/p/8UUTl2C5slo func HasPrefixAny(str string, prefixes []string) bool { if len(str) == 0 || len(prefixes) == 0 { @@ -421,7 +421,7 @@ func HasPrefixAny(str string, prefixes []string) bool { return false } -// HasSuffixAny check if a string ends with any of an array of specified strings. +// HasSuffixAny check if a string ends with any of a slice of specified strings. // Play: https://go.dev/play/p/sKWpCQdOVkx func HasSuffixAny(str string, suffixes []string) bool { if len(str) == 0 || len(suffixes) == 0 { @@ -457,8 +457,8 @@ func ReplaceWithMap(str string, replaces map[string]string) string { return str } -// SplitAndTrim splits string `str` by a string `delimiter` to an array, -// and calls Trim to every element of this array. It ignores the elements +// SplitAndTrim splits string `str` by a string `delimiter` to a slice, +// and calls Trim to every element of this slice. It ignores the elements // which are empty after Trim. func SplitAndTrim(str, delimiter string, characterMask ...string) []string { result := make([]string, 0)