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

Add RandomString #266

Merged
merged 2 commits into from Nov 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -143,6 +143,7 @@ Supported math helpers:

Supported helpers for strings:

- [RandomString](#randomstring)
- [Substring](#substring)
- [ChunkString](#chunkstring)
- [RuneLength](#runelength)
Expand Down Expand Up @@ -1207,6 +1208,17 @@ sum := lo.SumBy(strings, func(item string) int {

[[play](https://go.dev/play/p/Dz_a_7jN_ca)]

### RandomString

Returns a random string of the specified length and made of the specified charset.

```go
str := lo.RandomString(5, lo.LettersCharset)
// example: "eIGbt"
```

[[play](https://go.dev/play/p/rRseOQVVum4)]

### Substring

Return part of a string.
Expand Down
29 changes: 29 additions & 0 deletions string.go
@@ -1,9 +1,38 @@
package lo

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

var (
LowerCaseLettersCharset = []rune("abcdefghijklmnopqrstuvwxyz")
UpperCaseLettersCharset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
LettersCharset = append(LowerCaseLettersCharset, UpperCaseLettersCharset...)
NumbersCharset = []rune("0123456789")
AlphanumericCharset = append(LettersCharset, NumbersCharset...)
SpecialCharset = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?")
AllCharset = append(AlphanumericCharset, SpecialCharset...)
)

// RandomString return a random string.
// Play: https://go.dev/play/p/rRseOQVVum4
func RandomString(size int, charset []rune) string {
if size <= 0 {
panic("lo.RandomString: Size parameter must be greater than 0")
}
if len(charset) <= 0 {
panic("lo.RandomString: Charset parameter must not be empty")
}

b := make([]rune, size)
possibleCharactersCount := len(charset)
for i := range b {
b[i] = charset[rand.Intn(possibleCharactersCount)]
}
return string(b)
}

// Substring return part of a string.
// Play: https://go.dev/play/p/TQlxQi82Lu1
func Substring[T ~string](str T, offset int, length uint) T {
Expand Down
24 changes: 24 additions & 0 deletions string_test.go
Expand Up @@ -2,11 +2,35 @@ package lo

import (
"math"
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

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

rand.Seed(time.Now().UnixNano())

str1 := RandomString(100, LowerCaseLettersCharset)
is.Equal(100, RuneLength(str1))
is.Subset(LowerCaseLettersCharset, []rune(str1))

str2 := RandomString(100, LowerCaseLettersCharset)
is.NotEqual(str1, str2)

noneUtf8Charset := []rune("明1好休2林森")
str3 := RandomString(100, noneUtf8Charset)
is.Equal(100, RuneLength(str3))
is.Subset(noneUtf8Charset, []rune(str3))

is.PanicsWithValue("lo.RandomString: Charset parameter must not be empty", func() { RandomString(100, []rune{}) })
is.PanicsWithValue("lo.RandomString: Size parameter must be greater than 0", func() { RandomString(0, LowerCaseLettersCharset) })
}
Copy link
Owner

Choose a reason for hiding this comment

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

Could you please add some tests with non utf-8 runes in charset? 🙏

Such as emoji...

Copy link
Owner

Choose a reason for hiding this comment

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

Also, can you use is.PanicsWithError("lo.RandomString: Size parameter must be greater than 0", RandomString(...)) please ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure makes sense!


func TestChunkString(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down