Skip to content

Commit

Permalink
chore: add zh-CN readme and update some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 2, 2022
1 parent cf3e08b commit 2c84115
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 239 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# Filter

[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gookit/filter)](https://github.com/gookit/filter)
[![GoDoc](https://godoc.org/github.com/gookit/filter?status.svg)](https://godoc.org/github.com/gookit/filter)
[![GoDoc](https://pkg.go.dev/github.com/gookit/filter?status.svg)](https://pkg.go.dev/github.com/gookit/filter)
[![Actions Status](https://github.com/gookit/filter/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/filter/actions)
[![Coverage Status](https://coveralls.io/repos/github/gookit/filter/badge.svg?branch=master)](https://coveralls.io/github/gookit/filter?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/filter)](https://goreportcard.com/report/github.com/gookit/filter)

Package filter provide filtering, sanitizing, and conversion of Golang data.
`filter` - provide filtering, sanitizing, and conversion of Golang data.

> 中文说明请查看 **[README.zh-CN](README.zh-CN.md)**
## GoDoc

- [godoc for gopkg](https://godoc.org/gopkg.in/gookit/filter.v1)
- [godoc for github](https://godoc.org/github.com/gookit/filter)
- [godoc for github](https://pkg.go.dev/github.com/gookit/filter)

> TIP: 想要过滤并验证Map,Struct数据,请使用 A [gookit/validate](github.com/gookit/validate)
## Install

```shell
go get github.com/gookit/filter
```

## Func Usage

Expand Down
130 changes: 130 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Filter

[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gookit/filter)](https://github.com/gookit/filter)
[![GoDoc](https://pkg.go.dev/github.com/gookit/filter?status.svg)](https://pkg.go.dev/github.com/gookit/filter)
[![Actions Status](https://github.com/gookit/filter/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/filter/actions)
[![Coverage Status](https://coveralls.io/repos/github/gookit/filter/badge.svg?branch=master)](https://coveralls.io/github/gookit/filter?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/filter)](https://goreportcard.com/report/github.com/gookit/filter)

`filter` - 提供过滤,净化以及转换Go数据等.

> English, please see **[README](README.md)**
## GoDoc

- [godoc for github](https://pkg.go.dev/github.com/gookit/filter)

> TIP: 想要过滤并验证Map,Struct数据,请使用 A [gookit/validate](github.com/gookit/validate)
## Install

```shell
go get github.com/gookit/filter
```

## Func Usage

Quick usage:

```go
str := filter.MustString(23) // "23"

intVal, err := filter.Int("20") // int(20)
strings := filter.Str2Slice("a,b, c", ",") // []string{"a", "b", "c"}
```

## Filtration

Filtering data:

```go
data := map[string]interface{}{
"name": " inhere ",
"age": "50",
"money": "50.34",
//
"remember": "yes",
//
"sub1": []string{"1", "2"},
"tags": "go;lib",
"str1": " word ",
"ids": []int{1, 2, 2, 1},
}
f := filter.New(data)
f.AddRule("money", "float")
f.AddRule("remember", "bool")
f.AddRule("sub1", "strings2ints")
f.AddRule("tags", "str2arr:;")
f.AddRule("ids", "unique")
f.AddRule("str1", "ltrim|rtrim")
f.AddRule("not-exist", "unique")
// add multi
f.AddRules(map[string]string{
"age": "trim|int",
"name": "trim|ucFirst",
})

// apply all added rules for data.
f.Filtering()

// get filtered data
newData := f.FilteredData()
fmt.Printf("%#v\n", newData)
// f.BindStruct(&user)
```

**Output**:

```go
map[string]interface {}{
"remember":true,
"sub1":[]int{1, 2},
"tags":[]string{"go", "lib"},
"ids":[]int{2, 1},
"str1":"word",
"name":"INHERE",
"age":50,
"money":50.34
}
```

## Filters & Converters

- `ToBool/Bool(s string) (bool, error)`
- `ToFloat/Float(v interface{}) (float64, error)`
- `ToInt/Int(v interface{}) (int, error)`
- `ToUint/Uint(v interface{}) (uint64, error)`
- `ToInt64/Int64(v interface{}) (int64, error)`
- `ToString/String(v interface{}) (string, error)`
- `MustBool(s string) bool`
- `MustFloat(s string) float64`
- `MustInt(s string) int`
- `MustInt64(s string) int64`
- `MustUint(s string) uint64`
- `MustString(v interface{}) string`
- `Trim(s string, cutSet ...string) string`
- `TrimLeft(s string, cutSet ...string) string`
- `TrimRight(s string, cutSet ...string) string`
- `TrimStrings(ss []string, cutSet ...string) (ns []string)`
- `Substr(s string, pos, length int) string`
- `Lower/Lowercase(s string) string`
- `Upper/Uppercase(s string) string`
- `LowerFirst(s string) string`
- `UpperFirst(s string) string`
- `UpperWord(s string) string`
- `Camel/CamelCase(s string, sep ...string) string`
- `Snake/SnakeCase(s string, sep ...string) string`
- `Email(s string) string`
- `URLDecode(s string) string`
- `URLEncode(s string) string`
- `EscapeJS(s string) string`
- `EscapeHTML(s string) string`
- `Unique(val interface{}) interface{}` Will remove duplicate values, use for `[]int` `[]int64` `[]string`
- `StrToSlice(s string, sep ...string) []string`
- `StrToInts(s string, sep ...string) (ints []int, err error)`
- `StrToTime(s string, layouts ...string) (t time.Time, err error)`
- `StringsToInts(ss []string) (ints []int, err error)`

## License

**[MIT](LICENSE)**
104 changes: 29 additions & 75 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"text/template"
"time"

"github.com/gookit/goutil/fmtutil"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil"
)
Expand All @@ -31,9 +31,7 @@ var (
*************************************************************/

// Int convert string to int
func Int(in interface{}) (int, error) {
return ToInt(in)
}
func Int(in interface{}) (int, error) { return ToInt(in) }

// MustInt convert string to int
func MustInt(in interface{}) int {
Expand All @@ -42,14 +40,10 @@ func MustInt(in interface{}) int {
}

// ToInt convert string to int
func ToInt(in interface{}) (int, error) {
return mathutil.ToInt(in)
}
func ToInt(in interface{}) (int, error) { return mathutil.ToInt(in) }

// Uint convert string to uint
func Uint(in interface{}) (uint64, error) {
return ToUint(in)
}
func Uint(in interface{}) (uint64, error) { return ToUint(in) }

// MustUint convert string to uint
func MustUint(in interface{}) uint64 {
Expand All @@ -58,19 +52,13 @@ func MustUint(in interface{}) uint64 {
}

// ToUint convert string to uint
func ToUint(in interface{}) (uint64, error) {
return mathutil.ToUint(in)
}
func ToUint(in interface{}) (uint64, error) { return mathutil.ToUint(in) }

// Int64 convert value to int64
func Int64(in interface{}) (int64, error) {
return ToInt64(in)
}
func Int64(in interface{}) (int64, error) { return ToInt64(in) }

// ToInt64 convert value to int64
func ToInt64(val interface{}) (int64, error) {
return mathutil.ToInt64(val)
}
func ToInt64(val interface{}) (int64, error) { return mathutil.ToInt64(val) }

// MustInt64 convert value to int64
func MustInt64(in interface{}) int64 {
Expand All @@ -79,9 +67,7 @@ func MustInt64(in interface{}) int64 {
}

// Float convert string to float
func Float(s string) (float64, error) {
return ToFloat(s)
}
func Float(s string) (float64, error) { return ToFloat(s) }

// ToFloat convert string to float
func ToFloat(s string) (float64, error) {
Expand All @@ -95,15 +81,10 @@ func MustFloat(s string) float64 {
}

// ToBool convert string to bool
func ToBool(s string) (bool, error) {
return Bool(s)
}
func ToBool(s string) (bool, error) { return Bool(s) }

// Bool parse string to bool
func Bool(s string) (bool, error) {
// return strconv.ParseBool(Trim(s))
return strutil.ToBool(s)
}
func Bool(s string) (bool, error) { return strutil.ToBool(s) }

// MustBool convert.
func MustBool(s string) bool {
Expand All @@ -112,9 +93,7 @@ func MustBool(s string) bool {
}

// String convert val to string
func String(val interface{}) (string, error) {
return ToString(val)
}
func String(val interface{}) (string, error) { return ToString(val) }

// MustString convert value to string
func MustString(in interface{}) string {
Expand All @@ -123,90 +102,65 @@ func MustString(in interface{}) string {
}

// ToString convert value to string
func ToString(val interface{}) (string, error) {
return strutil.ToString(val)
}
func ToString(val interface{}) (string, error) { return strutil.ToString(val) }

/*************************************************************
* change string case
*************************************************************/

// Lowercase alias of the strings.ToLower()
func Lowercase(s string) string {
return strings.ToLower(s)
}
func Lowercase(s string) string { return strings.ToLower(s) }

// Uppercase alias of the strings.ToUpper()
func Uppercase(s string) string {
return strings.ToUpper(s)
}
func Uppercase(s string) string { return strings.ToUpper(s) }

// UpperWord Change the first character of each word to uppercase
func UpperWord(s string) string {
return strutil.UpperWord(s)
}
func UpperWord(s string) string { return strutil.UpperWord(s) }

// LowerFirst lower first char
func LowerFirst(s string) string {
return strutil.LowerFirst(s)
}
func LowerFirst(s string) string { return strutil.LowerFirst(s) }

// UpperFirst upper first char
func UpperFirst(s string) string {
return strutil.UpperFirst(s)
}
func UpperFirst(s string) string { return strutil.UpperFirst(s) }

// Snake alias of the SnakeCase
func Snake(s string, sep ...string) string {
return SnakeCase(s, sep...)
}
func Snake(s string, sep ...string) string { return SnakeCase(s, sep...) }

// SnakeCase convert. eg "RangePrice" -> "range_price"
func SnakeCase(s string, sep ...string) string {
return strutil.SnakeCase(s, sep...)
}
func SnakeCase(s string, sep ...string) string { return strutil.SnakeCase(s, sep...) }

// Camel alias of the CamelCase
func Camel(s string, sep ...string) string {
return strutil.CamelCase(s, sep...)
}
func Camel(s string, sep ...string) string { return strutil.CamelCase(s, sep...) }

// CamelCase convert string to camel case.
//
// Support:
// "range_price" -> "rangePrice"
// "range price" -> "rangePrice"
// "range-price" -> "rangePrice"
func CamelCase(s string, sep ...string) string {
return strutil.CamelCase(s, sep...)
}
//
// "range_price" -> "rangePrice"
// "range price" -> "rangePrice"
// "range-price" -> "rangePrice"
func CamelCase(s string, sep ...string) string { return strutil.CamelCase(s, sep...) }

/*************************************************************
* string to slice, time
*************************************************************/

// StrToInts split string to slice and convert item to int.
func StrToInts(s string, sep ...string) ([]int, error) {
return strutil.ToIntSlice(s, sep...)
}
func StrToInts(s string, sep ...string) ([]int, error) { return strutil.ToIntSlice(s, sep...) }

// StrToArray alias of the StrToSlice()
func StrToArray(s string, sep ...string) []string {
return StrToSlice(s, sep...)
}
func StrToArray(s string, sep ...string) []string { return StrToSlice(s, sep...) }

// StrToSlice split string to array.
func StrToSlice(s string, sep ...string) []string {
if len(sep) > 0 {
return strutil.Split(s, sep[0])
}

return strutil.Split(s, ",")
}

// StringsToInts string slice to int slice
func StringsToInts(ss []string) ([]int, error) {
return fmtutil.StringsToInts(ss)
}
func StringsToInts(ss []string) ([]int, error) { return arrutil.StringsToInts(ss) }

// StrToTime convert date string to time.Time
func StrToTime(s string, layouts ...string) (time.Time, error) {
Expand Down
Loading

0 comments on commit 2c84115

Please sign in to comment.