Skip to content

Commit

Permalink
Merge branch 'master' of github.com:samber/lo
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Mar 20, 2023
2 parents 91570dd + 43cb1bd commit cccebf9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ matching := lo.FilterMap([]string{"cpu", "gpu", "mouse", "keyboard"}, func(x str

### FlatMap

Manipulates a slice and transforms and flattens it to a slice of another type.
Manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a `nil`, and in the `nil` case no value is added to the final slice.

```go
lo.FlatMap([]int{0, 1, 2}, func(x int, _ int) []string {
Expand Down
6 changes: 3 additions & 3 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func Buffer[T any](ch <-chan T, size int) (collection []T, length int, readTime

// Batch creates a slice of n elements from a channel. Returns the slice and the slice length.
//
// Deprecated: Use lo.Buffer instead.
// Deprecated: Use [Buffer] instead.
func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool) {
return Buffer(ch, size)
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func BufferWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (col

// BatchWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.
//
// Deprecated: Use lo.BufferWithTimeout instead.
// Deprecated: Use [BufferWithTimeout] instead.
func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool) {
return BufferWithTimeout(ch, size, timeout)
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func FanIn[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T {
// ChannelMerge collects messages from multiple input channels into a single buffered channel.
// Output messages has no priority. When all upstream channels reach EOF, downstream channel closes.
//
// Deprecated: Use lo.FanIn instead.
// Deprecated: Use [FanIn] instead.
func ChannelMerge[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T {
return FanIn(channelBufferCap, upstreams...)
}
Expand Down
14 changes: 7 additions & 7 deletions concurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Synchronize(opt ...sync.Locker) *synchronize {

// Async executes a function in a goroutine and returns the result in a channel.
func Async[A any](f func() A) chan A {
ch := make(chan A)
ch := make(chan A, 1)
go func() {
ch <- f()
}()
Expand All @@ -36,7 +36,7 @@ func Async[A any](f func() A) chan A {

// Async0 executes a function in a goroutine and returns a channel set once the function finishes.
func Async0(f func()) chan struct{} {
ch := make(chan struct{})
ch := make(chan struct{}, 1)
go func() {
f()
ch <- struct{}{}
Expand All @@ -51,7 +51,7 @@ func Async1[A any](f func() A) chan A {

// Async2 has the same behavior as Async, but returns the 2 results as a tuple inside the channel.
func Async2[A any, B any](f func() (A, B)) chan Tuple2[A, B] {
ch := make(chan Tuple2[A, B])
ch := make(chan Tuple2[A, B], 1)
go func() {
ch <- T2(f())
}()
Expand All @@ -60,7 +60,7 @@ func Async2[A any, B any](f func() (A, B)) chan Tuple2[A, B] {

// Async3 has the same behavior as Async, but returns the 3 results as a tuple inside the channel.
func Async3[A any, B any, C any](f func() (A, B, C)) chan Tuple3[A, B, C] {
ch := make(chan Tuple3[A, B, C])
ch := make(chan Tuple3[A, B, C], 1)
go func() {
ch <- T3(f())
}()
Expand All @@ -69,7 +69,7 @@ func Async3[A any, B any, C any](f func() (A, B, C)) chan Tuple3[A, B, C] {

// Async4 has the same behavior as Async, but returns the 4 results as a tuple inside the channel.
func Async4[A any, B any, C any, D any](f func() (A, B, C, D)) chan Tuple4[A, B, C, D] {
ch := make(chan Tuple4[A, B, C, D])
ch := make(chan Tuple4[A, B, C, D], 1)
go func() {
ch <- T4(f())
}()
Expand All @@ -78,7 +78,7 @@ func Async4[A any, B any, C any, D any](f func() (A, B, C, D)) chan Tuple4[A, B,

// Async5 has the same behavior as Async, but returns the 5 results as a tuple inside the channel.
func Async5[A any, B any, C any, D any, E any](f func() (A, B, C, D, E)) chan Tuple5[A, B, C, D, E] {
ch := make(chan Tuple5[A, B, C, D, E])
ch := make(chan Tuple5[A, B, C, D, E], 1)
go func() {
ch <- T5(f())
}()
Expand All @@ -87,7 +87,7 @@ func Async5[A any, B any, C any, D any, E any](f func() (A, B, C, D, E)) chan Tu

// Async6 has the same behavior as Async, but returns the 6 results as a tuple inside the channel.
func Async6[A any, B any, C any, D any, E any, F any](f func() (A, B, C, D, E, F)) chan Tuple6[A, B, C, D, E, F] {
ch := make(chan Tuple6[A, B, C, D, E, F])
ch := make(chan Tuple6[A, B, C, D, E, F], 1)
go func() {
ch <- T6(f())
}()
Expand Down
6 changes: 4 additions & 2 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lo

import (
"math/rand"
"strings"
"unicode/utf8"
)

Expand Down Expand Up @@ -36,7 +37,8 @@ func RandomString(size int, charset []rune) string {
// Substring return part of a string.
// Play: https://go.dev/play/p/TQlxQi82Lu1
func Substring[T ~string](str T, offset int, length uint) T {
size := len(str)
rs := []rune(str)
size := len(rs)

if offset < 0 {
offset = size + offset
Expand All @@ -53,7 +55,7 @@ func Substring[T ~string](str T, offset int, length uint) T {
length = uint(size - offset)
}

return str[offset : offset+int(length)]
return T(strings.Replace(string(rs[offset:offset+int(length)]), "\x00", "", -1))
}

// ChunkString returns an array of strings split into groups the length of size. If array can't be split evenly,
Expand Down
2 changes: 2 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestSubstring(t *testing.T) {
str10 := Substring("hello", -2, 4)
str11 := Substring("hello", -4, 1)
str12 := Substring("hello", -4, math.MaxUint)
str13 := Substring("🏠🐶🐱", 0, 2)

is.Equal("", str1)
is.Equal("", str2)
Expand All @@ -87,6 +88,7 @@ func TestSubstring(t *testing.T) {
is.Equal("lo", str10)
is.Equal("e", str11)
is.Equal("ello", str12)
is.Equal("🏠🐶", str13)
}

func TestRuneLength(t *testing.T) {
Expand Down

0 comments on commit cccebf9

Please sign in to comment.