1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

Compare commits

...

5 Commits

Author SHA1 Message Date
dudaodong
f9e5ec9096 feat: add Rotate for string 2024-09-03 16:36:11 +08:00
dudaodong
601df5dc12 feat: add Rotate for string 2024-09-03 16:35:38 +08:00
dudaodong
63216d9b1c feat: add Shuffle for string 2024-09-03 15:54:05 +08:00
dudaodong
c32a19868d refactoring: make test clear 2024-09-03 15:33:32 +08:00
dudaodong
71e914019b feat: add Ellipsis for string 2024-09-03 14:36:12 +08:00
5 changed files with 609 additions and 179 deletions

View File

@@ -63,6 +63,9 @@ import (
- [SubInBetween](#SubInBetween)
- [HammingDistance](#HammingDistance)
- [Concat](#Concat)
- [Ellipsis](#Ellipsis)
- [Shuffle](#Shuffle)
- [Rotate](#Rotate)
<div STYLE="page-break-after: always;"></div>
@@ -1551,16 +1554,108 @@ import (
func main() {
result1 := strutil.Concat(12, "Hello", " ", "World", "!")
result2 := strutil.Concat(11, "Go", " ", "Language")
result3 := strutil.Concat(0, "An apple a ", "day", "keeps the", " doctor away")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
result1 := strutil.Concat(12, "Hello", " ", "World", "!")
result2 := strutil.Concat(11, "Go", " ", "Language")
result3 := strutil.Concat(0, "An apple a ", "day", "keeps the", " doctor away")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// Hello World!
// Go Language
// An apple a daykeeps the doctor away
// Output:
// Hello World!
// Go Language
// An apple a daykeeps the doctor away
}
```
### <span id="Ellipsis">Ellipsis</span>
<p>将字符串截断到指定长度,并在末尾添加省略号。</p>
<b>函数签名:</b>
```go
func Ellipsis(str string, length int) string
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.Ellipsis("hello world", 5)
result2 := strutil.Ellipsis("你好,世界!", 2)
result3 := strutil.Ellipsis("😀😃😄😁😆", 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// hello...
// 你好...
// 😀😃😄...
}
```
### <span id="Shuffle">Shuffle</span>
<p>打乱给定字符串中的字符顺序。</p>
<b>函数签名:</b>
```go
func Shuffle(str string) string
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result := strutil.Shuffle("hello")
fmt.Println(result) //olelh (random order)
}
```
### <span id="Rotate">Rotate</span>
<p>按指定的字符数旋转字符串。</p>
<b>函数签名:</b>
```go
func Rotate(str string, shift int) string
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := Rotate("Hello", 0)
result2 := Rotate("Hello", 1)
result3 := Rotate("Hello", 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// Hello
// oHell
// loHel
}
```

View File

@@ -63,6 +63,9 @@ import (
- [SubInBetween](#SubInBetween)
- [HammingDistance](#HammingDistance)
- [Concat](#Concat)
- [Ellipsis](#Ellipsis)
- [Shuffle](#Shuffle)
- [Rotate](#Rotate)
<div STYLE="page-break-after: always;"></div>
@@ -1100,10 +1103,10 @@ import (
func main() {
result1 := strutil.IsNotBlank("")
result2 := strutil.IsNotBlank(" ")
result2 := strutil.IsNotBlank(" ")
result3 := strutil.IsNotBlank("\t\v\f\n")
result4 := strutil.IsNotBlank(" 中文")
result5 := strutil.IsNotBlank(" world ")
result5 := strutil.IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
@@ -1553,17 +1556,107 @@ import (
)
func main() {
result1 := strutil.Concat(12, "Hello", " ", "World", "!")
result2 := strutil.Concat(11, "Go", " ", "Language")
result3 := strutil.Concat(0, "An apple a ", "day", "keeps the", " doctor away")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
result1 := strutil.Concat(12, "Hello", " ", "World", "!")
result2 := strutil.Concat(11, "Go", " ", "Language")
result3 := strutil.Concat(0, "An apple a ", "day", "keeps the", " doctor away")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// Hello World!
// Go Language
// An apple a daykeeps the doctor away
// Output:
// Hello World!
// Go Language
// An apple a daykeeps the doctor away
}
```
```
### <span id="Ellipsis">Ellipsis</span>
<p>Truncates a string to a specified length and appends an ellipsis.</p>
<b>Signature:</b>
```go
func Ellipsis(str string, length int) string
```
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.Ellipsis("hello world", 5)
result2 := strutil.Ellipsis("你好,世界!", 2)
result3 := strutil.Ellipsis("😀😃😄😁😆", 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// hello...
// 你好...
// 😀😃😄...
}
```
### <span id="Shuffle">Shuffle</span>
<p>Shuffle the order of characters of given string.</p>
<b>Signature:</b>
```go
func Shuffle(str string) string
```
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result := strutil.Shuffle("hello")
fmt.Println(result) //olelh (random order)
}
```
### <span id="Rotate">Rotate</span>
<p>Rotates the string by the specified number of characters.</p>
<b>Signature:</b>
```go
func Rotate(str string, shift int) string
```
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := Rotate("Hello", 0)
result2 := Rotate("Hello", 1)
result3 := Rotate("Hello", 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// Hello
// oHell
// loHel
}

View File

@@ -5,13 +5,18 @@ package strutil
import (
"errors"
"math/rand"
"regexp"
"strings"
"time"
"unicode"
"unicode/utf8"
"unsafe"
)
// used in `Shuffle` function
var rng = rand.New(rand.NewSource(int64(time.Now().UnixNano())))
// CamelCase coverts string to camelCase string. Non letters and numbers will be ignored.
// Play: https://go.dev/play/p/9eXP3tn2tUy
func CamelCase(s string) string {
@@ -621,6 +626,8 @@ func HammingDistance(a, b string) (int, error) {
// Concat uses the strings.Builder to concatenate the input strings.
// - `length` is the expected length of the concatenated string.
// - if you are unsure about the length of the string to be concatenated, please pass 0 or a negative number.
//
// Play: todo
func Concat(length int, str ...string) string {
if len(str) == 0 {
return ""
@@ -638,3 +645,62 @@ func Concat(length int, str ...string) string {
}
return sb.String()
}
// Ellipsis truncates a string to a specified length and appends an ellipsis.
// Play: todo
func Ellipsis(str string, length int) string {
str = strings.TrimSpace(str)
if length <= 0 {
return ""
}
runes := []rune(str)
if len(runes) <= length {
return str
}
return string(runes[:length]) + "..."
}
// Shuffle the order of characters of given string.
// Play: todo
func Shuffle(str string) string {
runes := []rune(str)
for i := len(runes) - 1; i > 0; i-- {
j := rng.Intn(i + 1)
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
// Rotate rotates the string by the specified number of characters.
// Play: todo
func Rotate(str string, shift int) string {
if shift == 0 {
return str
}
runes := []rune(str)
length := len(runes)
if length == 0 {
return str
}
shift = shift % length
if shift < 0 {
shift = length + shift
}
var sb strings.Builder
sb.Grow(length)
sb.WriteString(string(runes[length-shift:]))
sb.WriteString(string(runes[:length-shift]))
return sb.String()
}

View File

@@ -694,3 +694,33 @@ func ExampleConcat() {
// Go Language
// An apple a daykeeps the doctor away
}
func ExampleEllipsis() {
result1 := Ellipsis("hello world", 5)
result2 := Ellipsis("你好,世界!", 2)
result3 := Ellipsis("😀😃😄😁😆", 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// hello...
// 你好...
// 😀😃😄...
}
func ExampleRotate() {
result1 := Rotate("Hello", 0)
result2 := Rotate("Hello", 1)
result3 := Rotate("Hello", 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// Hello
// oHell
// loHel
}

View File

@@ -1,7 +1,6 @@
package strutil
import (
"reflect"
"testing"
"github.com/duke-git/lancet/v2/internal"
@@ -12,19 +11,22 @@ func TestCamelCase(t *testing.T) {
assert := internal.NewAssert(t, "TestCamelCase")
cases := map[string]string{
"": "",
"foobar": "foobar",
"&FOO:BAR$BAZ": "fooBarBaz",
"fooBar": "fooBar",
"FOObar": "foObar",
"$foo%": "foo",
" $#$Foo 22 bar ": "foo22Bar",
"Foo-#1😄$_%^&*(1bar": "foo11Bar",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foobar", "foobar"},
{"&FOO:BAR$BAZ", "fooBarBaz"},
{"fooBar", "fooBar"},
{"FOObar", "foObar"},
{"$foo%", "foo"},
{" $#$Foo 22 bar ", "foo22Bar"},
{"Foo-#1😄$_%^&*(1bar", "foo11Bar"},
}
for k, v := range cases {
assert.Equal(v, CamelCase(k))
for _, tt := range tests {
assert.Equal(tt.expected, CamelCase(tt.input))
}
}
@@ -33,19 +35,22 @@ func TestCapitalize(t *testing.T) {
assert := internal.NewAssert(t, "TestCapitalize")
cases := map[string]string{
"": "",
"Foo": "Foo",
"_foo": "_foo",
"foobar": "Foobar",
"fooBar": "Foobar",
"foo Bar": "Foo bar",
"foo-bar": "Foo-bar",
"$foo%": "$foo%",
tests := []struct {
input string
expected string
}{
{"", ""},
{"Foo", "Foo"},
{"_foo", "_foo"},
{"foobar", "Foobar"},
{"fooBar", "Foobar"},
{"foo Bar", "Foo bar"},
{"foo-bar", "Foo-bar"},
{"$foo%", "$foo%"},
}
for k, v := range cases {
assert.Equal(v, Capitalize(k))
for _, tt := range tests {
assert.Equal(tt.expected, Capitalize(tt.input))
}
}
@@ -54,23 +59,26 @@ func TestKebabCase(t *testing.T) {
assert := internal.NewAssert(t, "TestKebabCase")
cases := map[string]string{
"": "",
"foo-bar": "foo-bar",
"--Foo---Bar-": "foo-bar",
"Foo Bar-": "foo-bar",
"foo_Bar": "foo-bar",
"fooBar": "foo-bar",
"FOOBAR": "foobar",
"FOO_BAR": "foo-bar",
"__FOO_BAR__": "foo-bar",
"$foo@Bar": "foo-bar",
" $#$Foo 22 bar ": "foo-22-bar",
"Foo-#1😄$_%^&*(1bar": "foo-1-1-bar",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foo-bar", "foo-bar"},
{"--Foo---Bar-", "foo-bar"},
{"Foo Bar-", "foo-bar"},
{"foo_Bar", "foo-bar"},
{"fooBar", "foo-bar"},
{"FOOBAR", "foobar"},
{"FOO_BAR", "foo-bar"},
{"__FOO_BAR__", "foo-bar"},
{"$foo@Bar", "foo-bar"},
{" $#$Foo 22 bar ", "foo-22-bar"},
{"Foo-#1😄$_%^&*(1bar", "foo-1-1-bar"},
}
for k, v := range cases {
assert.Equal(v, KebabCase(k))
for _, tt := range tests {
assert.Equal(tt.expected, KebabCase(tt.input))
}
}
@@ -79,23 +87,26 @@ func TestUpperKebabCase(t *testing.T) {
assert := internal.NewAssert(t, "TestUpperKebabCase")
cases := map[string]string{
"": "",
"foo-bar": "FOO-BAR",
"--Foo---Bar-": "FOO-BAR",
"Foo Bar-": "FOO-BAR",
"foo_Bar": "FOO-BAR",
"fooBar": "FOO-BAR",
"FOOBAR": "FOOBAR",
"FOO_BAR": "FOO-BAR",
"__FOO_BAR__": "FOO-BAR",
"$foo@Bar": "FOO-BAR",
" $#$Foo 22 bar ": "FOO-22-BAR",
"Foo-#1😄$_%^&*(1bar": "FOO-1-1-BAR",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foo-bar", "FOO-BAR"},
{"--Foo---Bar-", "FOO-BAR"},
{"Foo Bar-", "FOO-BAR"},
{"foo_Bar", "FOO-BAR"},
{"fooBar", "FOO-BAR"},
{"FOOBAR", "FOOBAR"},
{"FOO_BAR", "FOO-BAR"},
{"__FOO_BAR__", "FOO-BAR"},
{"$foo@Bar", "FOO-BAR"},
{" $#$Foo 22 bar ", "FOO-22-BAR"},
{"Foo-#1😄$_%^&*(1bar", "FOO-1-1-BAR"},
}
for k, v := range cases {
assert.Equal(v, UpperKebabCase(k))
for _, tt := range tests {
assert.Equal(tt.expected, UpperKebabCase(tt.input))
}
}
@@ -104,23 +115,26 @@ func TestSnakeCase(t *testing.T) {
assert := internal.NewAssert(t, "TestSnakeCase")
cases := map[string]string{
"": "",
"foo-bar": "foo_bar",
"--Foo---Bar-": "foo_bar",
"Foo Bar-": "foo_bar",
"foo_Bar": "foo_bar",
"fooBar": "foo_bar",
"FOOBAR": "foobar",
"FOO_BAR": "foo_bar",
"__FOO_BAR__": "foo_bar",
"$foo@Bar": "foo_bar",
" $#$Foo 22 bar ": "foo_22_bar",
"Foo-#1😄$_%^&*(1bar": "foo_1_1_bar",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foo-bar", "foo_bar"},
{"--Foo---Bar-", "foo_bar"},
{"Foo Bar-", "foo_bar"},
{"foo_Bar", "foo_bar"},
{"fooBar", "foo_bar"},
{"FOOBAR", "foobar"},
{"FOO_BAR", "foo_bar"},
{"__FOO_BAR__", "foo_bar"},
{"$foo@Bar", "foo_bar"},
{" $#$Foo 22 bar ", "foo_22_bar"},
{"Foo-#1😄$_%^&*(1bar", "foo_1_1_bar"},
}
for k, v := range cases {
assert.Equal(v, SnakeCase(k))
for _, tt := range tests {
assert.Equal(tt.expected, SnakeCase(tt.input))
}
}
@@ -129,23 +143,26 @@ func TestUpperSnakeCase(t *testing.T) {
assert := internal.NewAssert(t, "TestUpperSnakeCase")
cases := map[string]string{
"": "",
"foo-bar": "FOO_BAR",
"--Foo---Bar-": "FOO_BAR",
"Foo Bar-": "FOO_BAR",
"foo_Bar": "FOO_BAR",
"fooBar": "FOO_BAR",
"FOOBAR": "FOOBAR",
"FOO_BAR": "FOO_BAR",
"__FOO_BAR__": "FOO_BAR",
"$foo@Bar": "FOO_BAR",
" $#$Foo 22 bar ": "FOO_22_BAR",
"Foo-#1😄$_%^&*(1bar": "FOO_1_1_BAR",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foo-bar", "FOO_BAR"},
{"--Foo---Bar-", "FOO_BAR"},
{"Foo Bar-", "FOO_BAR"},
{"foo_Bar", "FOO_BAR"},
{"fooBar", "FOO_BAR"},
{"FOOBAR", "FOOBAR"},
{"FOO_BAR", "FOO_BAR"},
{"__FOO_BAR__", "FOO_BAR"},
{"$foo@Bar", "FOO_BAR"},
{" $#$Foo 22 bar ", "FOO_22_BAR"},
{"Foo-#1😄$_%^&*(1bar", "FOO_1_1_BAR"},
}
for k, v := range cases {
assert.Equal(v, UpperSnakeCase(k))
for _, tt := range tests {
assert.Equal(tt.expected, UpperSnakeCase(tt.input))
}
}
@@ -154,16 +171,19 @@ func TestUpperFirst(t *testing.T) {
assert := internal.NewAssert(t, "TestLowerFirst")
cases := map[string]string{
"": "",
"foo": "Foo",
"bAR": "BAR",
"FOo": "FOo",
"fOo大": "FOo大",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foo", "Foo"},
{"bAR", "BAR"},
{"FOo", "FOo"},
{"fOo大", "FOo大"},
}
for k, v := range cases {
assert.Equal(v, UpperFirst(k))
for _, tt := range tests {
assert.Equal(tt.expected, UpperFirst(tt.input))
}
}
@@ -172,16 +192,19 @@ func TestLowerFirst(t *testing.T) {
assert := internal.NewAssert(t, "TestLowerFirst")
cases := map[string]string{
"": "",
"foo": "foo",
"bAR": "bAR",
"FOo": "fOo",
"fOo大": "fOo大",
tests := []struct {
input string
expected string
}{
{"", ""},
{"foo", "foo"},
{"bAR", "bAR"},
{"FOo", "fOo"},
{"fOo大", "fOo大"},
}
for k, v := range cases {
assert.Equal(v, LowerFirst(k))
for _, tt := range tests {
assert.Equal(tt.expected, LowerFirst(tt.input))
}
}
@@ -190,23 +213,48 @@ func TestPad(t *testing.T) {
assert := internal.NewAssert(t, "TestPad")
assert.Equal("a ", Pad("a", 2, ""))
assert.Equal("a", Pad("a", 1, "b"))
assert.Equal("ab", Pad("a", 2, "b"))
assert.Equal("mabcdm", Pad("abcd", 6, "m"))
tests := []struct {
input string
padSize int
padChar string
expected string
}{
{"", 0, "", ""},
{"a", 0, "", "a"},
{"a", 1, "", "a"},
{"a", 2, "", "a "},
{"a", 1, "b", "a"},
{"a", 2, "b", "ab"},
{"abcd", 6, "m", "mabcdm"},
}
for _, tt := range tests {
assert.Equal(tt.expected, Pad(tt.input, tt.padSize, tt.padChar))
}
}
func TestPadEnd(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestPadEnd")
assert.Equal("a ", PadEnd("a", 2, " "))
assert.Equal("a", PadEnd("a", 1, "b"))
assert.Equal("ab", PadEnd("a", 2, "b"))
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
assert.Equal("abcdmm", PadEnd("abcd", 6, "m"))
assert.Equal("abcaba", PadEnd("abc", 6, "ab"))
tests := []struct {
input string
padSize int
padChar string
expected string
}{
{"a", 2, " ", "a "},
{"a", 1, "b", "a"},
{"a", 2, "b", "ab"},
{"abcd", 6, "mno", "abcdmn"},
{"abcd", 6, "m", "abcdmm"},
{"abcd", 6, "ab", "abcdab"},
}
assert.NotEqual("ba", PadEnd("a", 2, "b"))
for _, tt := range tests {
assert.Equal(tt.expected, PadEnd(tt.input, tt.padSize, tt.padChar))
}
}
func TestPadStart(t *testing.T) {
@@ -214,13 +262,22 @@ func TestPadStart(t *testing.T) {
assert := internal.NewAssert(t, "TestPadStart")
assert.Equal("a", PadStart("a", 1, "b"))
assert.Equal("ba", PadStart("a", 2, "b"))
assert.Equal("mnabcd", PadStart("abcd", 6, "mno"))
assert.Equal("mmabcd", PadStart("abcd", 6, "m"))
assert.Equal("abaabc", PadStart("abc", 6, "ab"))
tests := []struct {
input string
padSize int
padChar string
expected string
}{
{"a", 1, "b", "a"},
{"a", 2, "b", "ba"},
{"abcd", 6, "mno", "mnabcd"},
{"abcd", 6, "m", "mmabcd"},
{"abc", 6, "ab", "abaabc"},
}
assert.NotEqual("ab", PadStart("a", 2, "b"))
for _, tt := range tests {
assert.Equal(tt.expected, PadStart(tt.input, tt.padSize, tt.padChar))
}
}
func TestBefore(t *testing.T) {
@@ -228,12 +285,21 @@ func TestBefore(t *testing.T) {
assert := internal.NewAssert(t, "TestBefore")
assert.Equal("lancet", Before("lancet", ""))
assert.Equal("", Before("lancet", "lancet"))
assert.Equal("lancet", Before("lancet", "abcdef"))
tests := []struct {
input string
char string
expected string
}{
{"lancet", "", "lancet"},
{"lancet", "lancet", ""},
{"lancet", "abcdef", "lancet"},
{"github.com/test/lancet", "/", "github.com"},
{"github.com/test/lancet", "test", "github.com/"},
}
assert.Equal("github.com", Before("github.com/test/lancet", "/"))
assert.Equal("github.com/", Before("github.com/test/lancet", "test"))
for _, tt := range tests {
assert.Equal(tt.expected, Before(tt.input, tt.char))
}
}
func TestBeforeLast(t *testing.T) {
@@ -243,7 +309,6 @@ func TestBeforeLast(t *testing.T) {
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"))
}
@@ -257,7 +322,6 @@ func TestAfter(t *testing.T) {
assert.Equal("", After("lancet", "lancet"))
assert.Equal("test/lancet", After("github.com/test/lancet", "/"))
assert.Equal("/lancet", After("github.com/test/lancet", "test"))
assert.Equal("lancet", After("lancet", "abcdef"))
}
@@ -403,10 +467,8 @@ func TestStringToBytes(t *testing.T) {
assert := internal.NewAssert(t, "TestStringToBytes")
str := "abc"
bytes := StringToBytes(str)
assert.Equal(reflect.DeepEqual(bytes, []byte{'a', 'b', 'c'}), true)
bytes := StringToBytes("abc")
assert.Equal(bytes, []byte{'a', 'b', 'c'})
}
func TestBytesToString(t *testing.T) {
@@ -414,16 +476,15 @@ func TestBytesToString(t *testing.T) {
assert := internal.NewAssert(t, "TestBytesToString")
bytes := []byte{'a', 'b', 'c'}
str := BytesToString(bytes)
assert.Equal(str == "abc", true)
str := BytesToString([]byte{'a', 'b', 'c'})
assert.Equal("abc", str)
}
func TestIsBlank(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsBlank")
assert.Equal(IsBlank(""), true)
assert.Equal(IsBlank("\t\v\f\n"), true)
@@ -434,6 +495,7 @@ func TestIsNotBlank(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsBlank")
assert.Equal(IsNotBlank(""), false)
assert.Equal(IsNotBlank(" "), false)
assert.Equal(IsNotBlank("\t\v\f\n"), false)
@@ -446,6 +508,7 @@ func TestHasPrefixAny(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestHasPrefixAny")
str := "foo bar"
prefixes := []string{"fo", "xyz", "hello"}
notMatches := []string{"oom", "world"}
@@ -458,6 +521,7 @@ func TestHasSuffixAny(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestHasSuffixAny")
str := "foo bar"
suffixes := []string{"bar", "xyz", "hello"}
notMatches := []string{"oom", "world"}
@@ -503,6 +567,7 @@ func TestTrim(t *testing.T) {
assert.Equal("$ ab cd $", Trim(str1))
assert.Equal("ab cd", Trim(str1, "$"))
assert.Equal("abcd", Trim("\nabcd"))
}
@@ -525,19 +590,27 @@ func TestHideString(t *testing.T) {
assert := internal.NewAssert(t, "TestTrim")
str := "13242658976"
tests := []struct {
input string
start int
end int
replacedChar string
expected string
}{
{"13242658976", 0, -1, "*", "13242658976"},
{"13242658976", 0, 0, "*", "13242658976"},
{"13242658976", 0, 4, "*", "****2658976"},
{"13242658976", 3, 3, "*", "13242658976"},
{"13242658976", 3, 4, "*", "132*2658976"},
{"13242658976", 3, 7, "*", "132****8976"},
{"13242658976", 3, 11, "*", "132********"},
{"13242658976", 7, 100, "*", "1324265****"},
{"13242658976", 100, 100, "*", "13242658976"},
}
assert.Equal("13242658976", HideString(str, 0, -1, "*"))
assert.Equal("13242658976", HideString(str, 0, 0, "*"))
assert.Equal("****2658976", HideString(str, 0, 4, "*"))
assert.Equal("13242658976", HideString(str, 3, 3, "*"))
assert.Equal("132*2658976", HideString(str, 3, 4, "*"))
assert.Equal("132****8976", HideString(str, 3, 7, "*"))
assert.Equal("1324265****", HideString(str, 7, 11, "*"))
assert.Equal("1324265****", HideString(str, 7, 100, "*"))
assert.Equal("13242658976", HideString(str, 100, 100, "*"))
for _, tt := range tests {
assert.Equal(tt.expected, HideString(tt.input, tt.start, tt.end, tt.replacedChar))
}
}
func TestContainsAll(t *testing.T) {
@@ -593,23 +666,34 @@ func TestHammingDistance(t *testing.T) {
return c
}
assert.Equal(0, hd(" ", " "))
assert.Equal(1, hd(" ", "c"))
assert.Equal(1, hd("a", "d"))
assert.Equal(1, hd("a", " "))
assert.Equal(1, hd("a", "f"))
tests := []struct {
strA string
strB string
hammingDistance int
}{
{" ", " ", 0},
{" ", "c", 1},
{"a", "d", 1},
{"a", " ", 1},
{"a", "f", 1},
assert.Equal(0, hd("", ""))
assert.Equal(-1, hd("abc", "ab"))
assert.Equal(3, hd("abc", "def"))
assert.Equal(-1, hd("kitten", "sitting"))
assert.Equal(1, hd("ö", "ü"))
assert.Equal(0, hd("日本語", "日本語"))
assert.Equal(3, hd("日本語", "語日本"))
{"", "", 0},
{"abc", "ab", -1},
{"abc", "def", 3},
{"kitten", "sitting", -1},
{"ö", "ü", 1},
{"日本語", "日本語", 0},
{"日本語", "語日本", 3},
}
for _, tt := range tests {
assert.Equal(tt.hammingDistance, hd(tt.strA, tt.strB))
}
}
func TestConcat(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestConcat")
assert.Equal("", Concat(0))
@@ -620,3 +704,65 @@ func TestConcat(t *testing.T) {
assert.Equal("你好,世界!", Concat(0, "你好", "", "", "世界!", ""))
assert.Equal("Hello World!", Concat(0, "Hello", " Wo", "r", "ld!", ""))
}
func TestEllipsis(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestEllipsis")
tests := []struct {
input string
length int
expected string
}{
{"", 0, ""},
{"hello world", 0, ""},
{"hello world", -1, ""},
{"hello world", 5, "hello..."},
{"hello world", 11, "hello world"},
{"你好,世界!", 2, "你好..."},
{"😀😃😄😁😆", 3, "😀😃😄..."},
{"This is a test.", 10, "This is a ..."},
}
for _, tt := range tests {
assert.Equal(tt.expected, Ellipsis(tt.input, tt.length))
}
}
func TestShuffle(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestShuffle")
assert.Equal("", Shuffle(""))
assert.Equal("a", Shuffle("a"))
str := "hello"
shuffledStr := Shuffle(str)
assert.Equal(5, len(shuffledStr))
}
func TestRotate(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestRotate")
tests := []struct {
input string
shift int
expected string
}{
{"", 1, ""},
{"a", 0, "a"},
{"a", 1, "a"},
{"a", -1, "a"},
{"Hello", -2, "lloHe"},
{"Hello", 1, "oHell"},
{"Hello, world!", 3, "ld!Hello, wor"},
}
for _, tt := range tests {
assert.Equal(tt.expected, Rotate(tt.input, tt.shift))
}
}