Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding Elipse #470

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ Supported helpers for strings:
- [Substring](#substring)
- [ChunkString](#chunkstring)
- [RuneLength](#runelength)
- [PascalCase](#pascalcase)
- [CamelCase](#camelcase)
- [KebabCase](#kebabcase)
- [SnakeCase](#snakecase)
- [Words](#words)
- [Capitalize](#capitalize)
- [Elipse](#elipse)

Supported helpers for tuples:

Expand Down Expand Up @@ -1395,11 +1402,24 @@ str := lo.Words("helloWorld")
Converts the first character of string to upper case and the remaining to lower case.

```go
str := lo.PascalCase("heLLO")
str := lo.Capitalize("heLLO")
// Hello
```

[[play](https://go.dev/play/p/jBIJ_OFtFYp)]
### Elipse

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

```go
str := lo.Elipse("Lorem Ipsum", 5)
// Lo...

str := lo.Elipse("Lorem Ipsum", 100)
// Lorem Ipsum

str := lo.Elipse("Lorem Ipsum", 3)
// ...
```

### T2 -> T9

Expand Down
17 changes: 15 additions & 2 deletions string.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package lo

import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"math/rand"
"regexp"
"strings"
"unicode"
"unicode/utf8"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

var (
Expand Down Expand Up @@ -162,3 +163,15 @@ func Words(str string) []string {
func Capitalize(str string) string {
return cases.Title(language.English).String(str)
}

// Elipse truncates a string to a specified length and appends an ellipsis if truncated.
func Elipse(str string, length int) string {
if len(str) > length {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(str) > length {
if len(str) <= length {
return str
}
if len(str) < 3 || length < 3 {
return "..."
}
return str[0:length-3] + "..."

if len(str) < 3 || length < 3 {
return "..."
}
return str[0:length-3] + "..."
}

return str
}
13 changes: 13 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,16 @@ func TestCapitalize(t *testing.T) {
})
}
}

func TestElipse(t *testing.T) {
t.Parallel()
is := assert.New(t)

is.Equal("12345", Elipse("12345", 5))
is.Equal("1...", Elipse("12345", 4))
is.Equal("12345", Elipse("12345", 6))
is.Equal("12345", Elipse("12345", 10))
is.Equal("...", Elipse("12345", 3))
is.Equal("...", Elipse("12345", 2))
is.Equal("...", Elipse("12345", -1))
}
Loading