mirror of
				https://github.com/samber/lo.git
				synced 2025-10-31 11:36:30 +08:00 
			
		
		
		
	feat: adding Elipse (#470)
This commit is contained in:
		
							
								
								
									
										24
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								README.md
									
									
									
									
									
								
							| @@ -152,6 +152,13 @@ Supported helpers for strings: | |||||||
| - [Substring](#substring) | - [Substring](#substring) | ||||||
| - [ChunkString](#chunkstring) | - [ChunkString](#chunkstring) | ||||||
| - [RuneLength](#runelength) | - [RuneLength](#runelength) | ||||||
|  | - [PascalCase](#pascalcase) | ||||||
|  | - [CamelCase](#camelcase) | ||||||
|  | - [KebabCase](#kebabcase) | ||||||
|  | - [SnakeCase](#snakecase) | ||||||
|  | - [Words](#words) | ||||||
|  | - [Capitalize](#capitalize) | ||||||
|  | - [Elipse](#elipse) | ||||||
|  |  | ||||||
| Supported helpers for tuples: | Supported helpers for tuples: | ||||||
|  |  | ||||||
| @@ -1395,11 +1402,24 @@ str := lo.Words("helloWorld") | |||||||
| Converts the first character of string to upper case and the remaining to lower case. | Converts the first character of string to upper case and the remaining to lower case. | ||||||
|  |  | ||||||
| ```go | ```go | ||||||
| str := lo.PascalCase("heLLO") | str := lo.Capitalize("heLLO") | ||||||
| // 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 | ### T2 -> T9 | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								string.go
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								string.go
									
									
									
									
									
								
							| @@ -1,13 +1,14 @@ | |||||||
| package lo | package lo | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"golang.org/x/text/cases" |  | ||||||
| 	"golang.org/x/text/language" |  | ||||||
| 	"math/rand" | 	"math/rand" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"unicode" | 	"unicode" | ||||||
| 	"unicode/utf8" | 	"unicode/utf8" | ||||||
|  |  | ||||||
|  | 	"golang.org/x/text/cases" | ||||||
|  | 	"golang.org/x/text/language" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @@ -162,3 +163,15 @@ func Words(str string) []string { | |||||||
| func Capitalize(str string) string { | func Capitalize(str string) string { | ||||||
| 	return cases.Title(language.English).String(str) | 	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 { | ||||||
|  | 		if len(str) < 3 || length < 3 { | ||||||
|  | 			return "..." | ||||||
|  | 		} | ||||||
|  | 		return str[0:length-3] + "..." | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return str | ||||||
|  | } | ||||||
|   | |||||||
| @@ -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)) | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Samuel Berthe
					Samuel Berthe