diff --git a/README.md b/README.md index 8af26e5..7d69023 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,14 @@ import "github.com/duke-git/lancet/v2/convertor" - **GbkToUtf8** : converts GBK encoding data to utf8 encoding data. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#GbkToUtf8)] [[play](https://go.dev/play/p/OphmHCN_9u8)] +- **ToStdBase64** : converts a value to a string encoded in standard Base64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToStdBase64)] +- **ToUrlBase64** : converts a value to a string encoded in url Base64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToUrlBase64)] +- **ToRawStdBase64** : converts a value to a string encoded in raw standard Base64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToRawStdBase64)] +- **ToRawUrlBase64** : converts a value to a string encoded in raw url Base64. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToRawUrlBase64)]
值转换为 StdBase64 编码的字符串。error 类型的数据也会把 error 的原因进行编码,复杂的结构会转为 JSON 格式的字符串
+将值转换为StdBase64编码的字符串。error类型的数据也会把error的原因进行编码,复杂的结构会转为JSON格式的字符串
函数签名: diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index dab680f..2b41f7a 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -93,6 +93,7 @@ import ( - [KeyBy](#KeyBy) - [Join](#Join) - [Partition](#Partition) +- [SetToDefaultIf](#SetToDefaultIf) @@ -2568,4 +2569,35 @@ func main() { // Output: // okk } +``` + +### SetToDefaultIf + +根据给定给定的predicate判定函数来修改切片中的元素。对于满足的元素,将其替换为指定的默认值,同时保持元素在切片中的位置不变。函数返回修改后的切片以及被修改的元素个数。
+ +函数签名: + +```go +func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "a", "c", "d", "a"} + modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s }) + + fmt.Println(modifiedStrs) + fmt.Println(count) + + // Output: + // [ b c d ] + // 3 +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/convertor.md b/docs/en/api/packages/convertor.md index c1ed5ea..e437f0e 100644 --- a/docs/en/api/packages/convertor.md +++ b/docs/en/api/packages/convertor.md @@ -955,7 +955,7 @@ func main() { ### ToUrlBase64 -Convert a value to a string encoded in Url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.
+Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.
Signature: diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index e113471..9c46ce5 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -2565,4 +2565,35 @@ func main() { // Output: // okk } +``` + +### SetToDefaultIf + +Sets elements to their default value if they match the given predicate. It retains the positions of the elements in the slice. It returns slice of T and the count of modified slice items
+ +Signature: + +```go +func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "a", "c", "d", "a"} + modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s }) + + fmt.Println(modifiedStrs) + fmt.Println(count) + + // Output: + // [ b c d ] + // 3 +} ``` \ No newline at end of file