Skip to content

Commit

Permalink
fix: optimize strings package
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 6, 2024
1 parent e166806 commit 358573c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 6 additions & 10 deletions string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ var (

// Join joins strings
Join = F.Curry2(F.Bind2nd[[]string, string, string])(strings.Join)

// Equals returns a predicate that tests if a string is equal
Equals = F.Curry2(Eq)

// Includes returns a predicate that tests for the existence of the search string
Includes = F.Curry2(F.Swap(strings.Contains))
)

func Eq(left string, right string) bool {
Expand All @@ -61,16 +67,6 @@ func Size(s string) int {
return len(s)
}

// Includes returns a predicate that tests for the existence of the search string
func Includes(searchString string) func(string) bool {
return F.Bind2nd(strings.Contains, searchString)
}

// Equals returns a predicate that tests if a string is equal
func Equals(other string) func(string) bool {
return F.Bind2nd(Eq, other)
}

// Format applies a format string to an arbitrary value
func Format[T any](format string) func(t T) string {
return func(t T) string {
Expand Down
12 changes: 12 additions & 0 deletions string/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ func TestJoin(t *testing.T) {
assert.Equal(t, "a", Join(",")(A.From("a")))
assert.Equal(t, "", Join(",")(A.Empty[string]()))
}

func TestEquals(t *testing.T) {
assert.True(t, Equals("a")("a"))
assert.False(t, Equals("a")("b"))
assert.False(t, Equals("b")("a"))
}

func TestIncludes(t *testing.T) {
assert.True(t, Includes("a")("bab"))
assert.False(t, Includes("bab")("a"))
assert.False(t, Includes("b")("a"))
}

0 comments on commit 358573c

Please sign in to comment.