mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 06:02:27 +08:00
refactor: refact Comma function
This commit is contained in:
@@ -121,37 +121,45 @@ func UpperSnakeCase(s string) string {
|
||||
|
||||
// Before create substring in source string before position when char first appear
|
||||
func Before(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
i := strings.Index(s, char)
|
||||
|
||||
if s == "" || char == "" || i == -1 {
|
||||
return s
|
||||
}
|
||||
i := strings.Index(s, char)
|
||||
|
||||
return s[0:i]
|
||||
}
|
||||
|
||||
// BeforeLast create substring in source string before position when char last appear
|
||||
func BeforeLast(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
i := strings.LastIndex(s, char)
|
||||
|
||||
if s == "" || char == "" || i == -1 {
|
||||
return s
|
||||
}
|
||||
i := strings.LastIndex(s, char)
|
||||
|
||||
return s[0:i]
|
||||
}
|
||||
|
||||
// After create substring in source string after position when char first appear
|
||||
func After(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
i := strings.Index(s, char)
|
||||
|
||||
if s == "" || char == "" || i == -1 {
|
||||
return s
|
||||
}
|
||||
i := strings.Index(s, char)
|
||||
|
||||
return s[i+len(char):]
|
||||
}
|
||||
|
||||
// AfterLast create substring in source string after position when char last appear
|
||||
func AfterLast(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
i := strings.LastIndex(s, char)
|
||||
|
||||
if s == "" || char == "" || i == -1 {
|
||||
return s
|
||||
}
|
||||
i := strings.LastIndex(s, char)
|
||||
|
||||
return s[i+len(char):]
|
||||
}
|
||||
|
||||
@@ -522,3 +530,14 @@ func RemoveWhiteSpace(str string, repalceAll bool) string {
|
||||
|
||||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
// SubInBetween return substring between the start and end position(excluded) of source string.
|
||||
func SubInBetween(str string, start string, end string) string {
|
||||
if _, after, ok := strings.Cut(str, start); ok {
|
||||
if before, _, ok := strings.Cut(after, end); ok {
|
||||
return before
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -207,6 +207,7 @@ func TestBefore(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestBefore")
|
||||
|
||||
assert.Equal("lancet", Before("lancet", ""))
|
||||
assert.Equal("lancet", Before("lancet", "abcdef"))
|
||||
assert.Equal("github.com", Before("github.com/test/lancet", "/"))
|
||||
assert.Equal("github.com/", Before("github.com/test/lancet", "test"))
|
||||
}
|
||||
@@ -215,6 +216,7 @@ func TestBeforeLast(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestBeforeLast")
|
||||
|
||||
assert.Equal("lancet", BeforeLast("lancet", ""))
|
||||
assert.Equal("lancet", BeforeLast("lancet", "abcdef"))
|
||||
assert.Equal("github.com/test", BeforeLast("github.com/test/lancet", "/"))
|
||||
assert.Equal("github.com/test/", BeforeLast("github.com/test/test/lancet", "test"))
|
||||
|
||||
@@ -225,6 +227,7 @@ func TestAfter(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestAfter")
|
||||
|
||||
assert.Equal("lancet", After("lancet", ""))
|
||||
assert.Equal("lancet", After("lancet", "abcdef"))
|
||||
assert.Equal("test/lancet", After("github.com/test/lancet", "/"))
|
||||
assert.Equal("/lancet", After("github.com/test/lancet", "test"))
|
||||
}
|
||||
@@ -233,6 +236,7 @@ func TestAfterLast(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestAfterLast")
|
||||
|
||||
assert.Equal("lancet", AfterLast("lancet", ""))
|
||||
assert.Equal("lancet", AfterLast("lancet", "abcdef"))
|
||||
assert.Equal("lancet", AfterLast("github.com/test/lancet", "/"))
|
||||
assert.Equal("/lancet", AfterLast("github.com/test/lancet", "test"))
|
||||
assert.Equal("/lancet", AfterLast("github.com/test/test/lancet", "test"))
|
||||
@@ -465,3 +469,15 @@ func TestRemoveWhiteSpace(t *testing.T) {
|
||||
assert.Equal("helloworld", RemoveWhiteSpace(str, true))
|
||||
assert.Equal("hello world", RemoveWhiteSpace(str, false))
|
||||
}
|
||||
|
||||
func TestSubInBetween(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestSubInBetween")
|
||||
|
||||
str := "abcde"
|
||||
|
||||
assert.Equal("", SubInBetween(str, "", ""))
|
||||
assert.Equal("ab", SubInBetween(str, "", "c"))
|
||||
assert.Equal("bc", SubInBetween(str, "a", "d"))
|
||||
assert.Equal("", SubInBetween(str, "a", ""))
|
||||
assert.Equal("", SubInBetween(str, "a", "f"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user