From 71e914019b0cae855cc9ec4851e0c3452cc959b8 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 3 Sep 2024 14:36:12 +0800 Subject: [PATCH] feat: add Ellipsis for string --- docs/api/packages/strutil.md | 35 ++++++++++++++++++++ docs/en/api/packages/strutil.md | 58 ++++++++++++++++++++++++++------- strutil/string.go | 20 ++++++++++++ strutil/string_example_test.go | 15 +++++++++ strutil/string_test.go | 24 ++++++++++++++ 5 files changed, 140 insertions(+), 12 deletions(-) diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 645a2cd..57b05bf 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -63,6 +63,7 @@ import ( - [SubInBetween](#SubInBetween) - [HammingDistance](#HammingDistance) - [Concat](#Concat) +- [Ellipsis](#Ellipsis)
@@ -1563,4 +1564,38 @@ func main() { // Go Language // An apple a day,keeps the doctor away } +``` + +### Ellipsis + +

将字符串截断到指定长度,并在末尾添加省略号。

+ +函数签名: + +```go +func Ellipsis(str string, length int) string +``` + +示例:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := strutil.Ellipsis("hello world", 5) + result2 := strutil.Ellipsis("你好,世界!", 2) + result3 := strutil.Ellipsis("😀😃😄😁😆", 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // hello... + // 你好... + // 😀😃😄... +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 471e523..8adf874 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -63,6 +63,7 @@ import ( - [SubInBetween](#SubInBetween) - [HammingDistance](#HammingDistance) - [Concat](#Concat) +- [Ellipsis](#Ellipsis)
@@ -1100,10 +1101,10 @@ import ( func main() { result1 := strutil.IsNotBlank("") - result2 := strutil.IsNotBlank(" ") + result2 := strutil.IsNotBlank(" ") result3 := strutil.IsNotBlank("\t\v\f\n") result4 := strutil.IsNotBlank(" 中文") - result5 := strutil.IsNotBlank(" world ") + result5 := strutil.IsNotBlank(" world ") fmt.Println(result1) fmt.Println(result2) @@ -1553,17 +1554,50 @@ import ( ) func main() { + result1 := strutil.Concat(12, "Hello", " ", "World", "!") + result2 := strutil.Concat(11, "Go", " ", "Language") + result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away") + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - result1 := strutil.Concat(12, "Hello", " ", "World", "!") - result2 := strutil.Concat(11, "Go", " ", "Language") - result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away") - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) + // Output: + // Hello World! + // Go Language + // An apple a day,keeps the doctor away +} +``` - // Output: - // Hello World! - // Go Language - // An apple a day,keeps the doctor away +### Ellipsis + +

Truncates a string to a specified length and appends an ellipsis.

+ +Signature: + +```go +func Ellipsis(str string, length int) string +``` + +Example:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := strutil.Ellipsis("hello world", 5) + result2 := strutil.Ellipsis("你好,世界!", 2) + result3 := strutil.Ellipsis("😀😃😄😁😆", 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // hello... + // 你好... + // 😀😃😄... } ``` \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index 72846f8..f5473eb 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -621,6 +621,8 @@ func HammingDistance(a, b string) (int, error) { // Concat uses the strings.Builder to concatenate the input strings. // - `length` is the expected length of the concatenated string. // - if you are unsure about the length of the string to be concatenated, please pass 0 or a negative number. +// +// Play: todo func Concat(length int, str ...string) string { if len(str) == 0 { return "" @@ -638,3 +640,21 @@ func Concat(length int, str ...string) string { } return sb.String() } + +// Ellipsis truncates a string to a specified length and appends an ellipsis. +// Play: todo +func Ellipsis(str string, length int) string { + str = strings.TrimSpace(str) + + if length <= 0 { + return "" + } + + runes := []rune(str) + + if len(runes) <= length { + return str + } + + return string(runes[:length]) + "..." +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 8eac765..514e904 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -694,3 +694,18 @@ func ExampleConcat() { // Go Language // An apple a day,keeps the doctor away } + +func ExampleEllipsis() { + result1 := Ellipsis("hello world", 5) + result2 := Ellipsis("你好,世界!", 2) + result3 := Ellipsis("😀😃😄😁😆", 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // hello... + // 你好... + // 😀😃😄... +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 6b2c8be..56150ea 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -620,3 +620,27 @@ func TestConcat(t *testing.T) { assert.Equal("你好,世界!", Concat(0, "你好", ",", "", "世界!", "")) assert.Equal("Hello World!", Concat(0, "Hello", " Wo", "r", "ld!", "")) } + +func TestEllipsis(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestEllipsis") + + tests := []struct { + input string + length int + want string + }{ + {"", 0, ""}, + {"hello world", 0, ""}, + {"hello world", -1, ""}, + {"hello world", 5, "hello..."}, + {"hello world", 11, "hello world"}, + {"你好,世界!", 2, "你好..."}, + {"😀😃😄😁😆", 3, "😀😃😄..."}, + {"This is a test.", 10, "This is a ..."}, + } + + for _, tt := range tests { + assert.Equal(tt.want, Ellipsis(tt.input, tt.length)) + } +}