Skip to content

Commit

Permalink
Merge pull request #2 from lilith44/develop
Browse files Browse the repository at this point in the history
feat: add Underscore() and Camel()
  • Loading branch information
lilith44 authored Sep 7, 2023
2 parents 9fb2b1a + 26ec2b7 commit 00fa01f
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 17 deletions.
49 changes: 33 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ m := slicex.ToMap(s)

// Output: map[1:{}, 2:{}, 3:{}]
fmt.Println(m)

```

### ToMapFunc
Expand All @@ -89,7 +88,7 @@ type School struct {
Name string
}

s := []*School{
s := []School{
{
Id: 1,
Name: "小学",
Expand All @@ -100,10 +99,10 @@ s := []*School{
},
}

m := slicex.ToMapFunc(s, func(school *School) int64 { return school.Id })
m := slicex.ToMapFunc(s, func(school School) int64 { return school.Id })

// Output: map[1:{Id:1 Name:小学}, 2:{Id:2 Name:"中学"}]
fmt.Println(m)
fmt.Printf("%+v\n", m)
```

### ToSliceFunc
Expand Down Expand Up @@ -155,7 +154,7 @@ type School struct {
Name string
}

s := []*School{
s := []School{
{
Id: 1,
Name: "小学",
Expand All @@ -166,10 +165,10 @@ s := []*School{
},
}

r := slicex.DeduplicateFunc(s, func(school *School) int64 { return school.Id })
r := slicex.DeduplicateFunc(s, func(school School) int64 { return school.Id })

// Output: [{Id:1 Name:小学}]
fmt.Println(r)
fmt.Printf("%+v\n", r)
```

### Concat
Expand All @@ -193,7 +192,7 @@ fmt.Println(r)
s1 := []int{1, 2, 2, 3}
s2 := []int{1, 2, 3}

// Output: false, true
// Output: false true
fmt.Println(slicex.IsUnique(s1), slicex.IsUnique(s2))
```

Expand Down Expand Up @@ -243,7 +242,7 @@ fmt.Println(r)
``` go
s := []int{1, 2, 2, 3}

// Output: [1, 2, 2], []
// Output: [1, 2, 2] []
fmt.Println(slicex.Paging(s, 1, 3), slicex.Paging(s, 3, 2))
```

Expand Down Expand Up @@ -282,15 +281,15 @@ type Paper struct {
}

p := &Paper{
Scores: easy.Float64s{1.23, 2.34, 3.45}
Scores: easy.Float64s{1.23, 2.34, 3.45},
}

data, err := json.Marshal(p)
if err != nil {
return err
}

// Output: {scores: ["1.23", "2.34", "3.45"]}
// Output: {"scores":["1.23","2.34","3.45"]}
fmt.Println(string(data))
```

Expand Down Expand Up @@ -322,7 +321,7 @@ Gid函数返回当前的goroutine的id。官方没有提供对应的方法。
使用示例:

``` go
intervals := []easy.Intervarl[int, int]{
intervals := []easy.Interval[int, int]{
{
Left: 900,
Right: 960,
Expand All @@ -332,12 +331,12 @@ intervals := []easy.Intervarl[int, int]{
Left: 930,
Right: 990,
Power: 50,
}
},
}

r := easy.MergeIntervals(intervals...)

// Output: [{Left:900 Right:930 Power:100}, {Left:930 Right:960 Power:150}, {Left:960 Right:990 Power:50}]
// Output: [{Left:900 Right:930 Power:100} {Left:930 Right:960 Power:150} {Left:960 Right:990 Power:50}]
fmt.Println(r)
```

Expand Down Expand Up @@ -368,13 +367,13 @@ snowflake := easy.NewSnowflake(unique())
fmt.Println(snowflake.NextId())
```

### HttpError
## HttpError
携带http状态码、业务错误码、信息的结构体
``` go
var ErrUserNotFound = easy.NewHttpError(http.StatusNotFound, 1001, "用户不存在!")
```

### HttpResponse
## HttpResponse
http接口返回的通用结构体
``` go
func (u *user) Add(c echo.Context) error {
Expand All @@ -393,4 +392,22 @@ switch e := err.(type) {
case x:
return c.JSON(statusCode, easy.Fail(e.Error()))
}
```

## Underscore
将驼峰式的字符串转为下划线式
``` go
s := "blueEyes"

// Output: blue_eyes
fmt.Println(easy.Underscore(s))
```

## Camel
将下划线式的字符串转为驼峰式
``` go
s := "blue_eyes"

// Output: blueEyes
fmt.Println(easy.Underscore(s))
```
48 changes: 47 additions & 1 deletion string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package easy

import "unsafe"
import (
"bytes"
"unsafe"
)

// ByteToString converts bytes to string.
func ByteToString(bytes []byte) string {
Expand All @@ -11,3 +14,46 @@ func ByteToString(bytes []byte) string {
func StringToByte(str string) []byte {
return *(*[]byte)(unsafe.Pointer(&str))
}

// Underscore converts string to under-score case.
func Underscore(str string) string {
if str == "" {
return ""
}

buf := bytes.Buffer{}
buf.Grow(len(str) + 1)
for i := range str {
if str[i] >= 'A' && str[i] <= 'Z' {
buf.WriteByte('_')
buf.WriteByte(str[i] - 'A' + 'a')
continue
}
buf.WriteByte(str[i])
}
return buf.String()
}

// Camel converts string to camel case.
func Camel(str string) string {
if str == "" {
return ""
}

buf := bytes.Buffer{}
buf.Grow(len(str))
toUpper := false
for i := range str {
if str[i] == '_' {
toUpper = true
continue
}
b := str[i]
if toUpper {
b = b - 'a' + 'A'
}
buf.WriteByte(b)
toUpper = false
}
return buf.String()
}
74 changes: 74 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package easy

import (
"testing"
)

type custom struct {
input string
want string
}

var underscoreTests = []*custom{
{
input: "",
want: "",
},
{
input: "black_magician",
want: "black_magician",
},
{
input: "blackMagicianGirl",
want: "black_magician_girl",
},
{
input: "BlueEyesChaosMaxDragon",
want: "_blue_eyes_chaos_max_dragon",
},
{
input: "appV1.1.0",
want: "app_v1.1.0",
},
}

func TestUnderscore(t *testing.T) {
for _, test := range underscoreTests {
got := Underscore(test.input)
if got != test.want {
t.Errorf("Underscore(%s) = %s, want %s", test.input, got, test.want)
}
}
}

var camelTests = []*custom{
{
input: "",
want: "",
},
{
input: "black_magician",
want: "blackMagician",
},
{
input: "black_magician_girl",
want: "blackMagicianGirl",
},
{
input: "_blue_eyes_chaos_max_dragon",
want: "BlueEyesChaosMaxDragon",
},
{
input: "app_v1.1.0",
want: "appV1.1.0",
},
}

func TestCamel(t *testing.T) {
for _, test := range camelTests {
got := Camel(test.input)
if got != test.want {
t.Errorf("Camel(%s) = %s, want %s", test.input, got, test.want)
}
}
}

0 comments on commit 00fa01f

Please sign in to comment.