1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

Compare commits

..

3 Commits

Author SHA1 Message Date
dudaodong
0c4b512084 fmt: fix some gofmt issue 2021-11-29 13:01:46 +08:00
dudaodong
4848647918 add go.mod 2021-11-29 11:48:12 +08:00
dudaodong
45fc84d1f2 update readme file and remove go.mod 2021-11-29 11:47:36 +08:00
15 changed files with 51 additions and 50 deletions

View File

@@ -5,7 +5,6 @@
</p> </p>
<div align="center" style="text-align: center;"> <div align="center" style="text-align: center;">
</div> </div>
English | [简体中文](./README_zh-CN.md) English | [简体中文](./README_zh-CN.md)
@@ -53,7 +52,7 @@ func main() {
### API Documentation ### API Documentation
#### 1. convertor contains some functions for data convertion. #### 1. convertor contains some functions for data convertion
- Support conversion between commonly used data types. - Support conversion between commonly used data types.
- Usage: import "github.com/duke-git/lancet/cryptor" - Usage: import "github.com/duke-git/lancet/cryptor"
@@ -91,7 +90,7 @@ func ToString(value interface{}) string //convert value to string
func StructToMap(value interface{}) (map[string]interface{}, error) //convert struct to map, only convert exported field, tag `json` should be set func StructToMap(value interface{}) (map[string]interface{}, error) //convert struct to map, only convert exported field, tag `json` should be set
``` ```
#### 2. cryptor is for data encryption and decryption. #### 2. cryptor is for data encryption and decryption
- Support md5, hmac, aes, des, ras. - Support md5, hmac, aes, des, ras.
- Usage: import "github.com/duke-git/lancet/cryptor" - Usage: import "github.com/duke-git/lancet/cryptor"
@@ -284,7 +283,7 @@ func HttpPatch(url string, params ...interface{}) (*http.Response, error) //http
func ConvertMapToQueryString(param map[string]interface{}) string //convert map to url query string func ConvertMapToQueryString(param map[string]interface{}) string //convert map to url query string
``` ```
#### 7. random is for rand string and int generation. #### 7. random is for rand string and int generation
- Generate random string and int. - Generate random string and int.
- Usage: import "github.com/duke-git/lancet/random". - Usage: import "github.com/duke-git/lancet/random".
@@ -336,7 +335,7 @@ func main() {
} }
``` ```
- Function list: - Function list:
```go ```go
func Contain(slice interface{}, value interface{}) bool //check if the value is in the slice or not func Contain(slice interface{}, value interface{}) bool //check if the value is in the slice or not
@@ -379,7 +378,7 @@ func main() {
} }
``` ```
- Function list: - Function list:
```go ```go
func After(s, char string) string //create substring in source string after position when char first appear func After(s, char string) string //create substring in source string after position when char first appear

View File

@@ -20,7 +20,7 @@ func ToBool(s string) (bool, error) {
return strconv.ParseBool(s) return strconv.ParseBool(s)
} }
// ToBool convert interface to bytes // ToBytes convert interface to bytes
func ToBytes(data interface{}) ([]byte, error) { func ToBytes(data interface{}) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
enc := gob.NewEncoder(&buf) enc := gob.NewEncoder(&buf)

View File

@@ -65,7 +65,7 @@ func AesCbcEncrypt(data, key []byte) []byte {
return encrypted return encrypted
} }
// AesEcbDecrypt decrypt data with key use AES CBC algorithm // AesCbcDecrypt decrypt data with key use AES CBC algorithm
// len(key) should be 16, 24 or 32 // len(key) should be 16, 24 or 32
func AesCbcDecrypt(encrypted, key []byte) []byte { func AesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := aes.NewCipher(key) block, _ := aes.NewCipher(key)

View File

@@ -21,13 +21,13 @@ func Base64StdEncode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s)) return base64.StdEncoding.EncodeToString([]byte(s))
} }
// Base64StdEncode decode a base64 encoded string // Base64StdDecode decode a base64 encoded string
func Base64StdDecode(s string) string { func Base64StdDecode(s string) string {
b, _ := base64.StdEncoding.DecodeString(s) b, _ := base64.StdEncoding.DecodeString(s)
return string(b) return string(b)
} }
// Md5Str return the md5 value of string // Md5String return the md5 value of string
func Md5String(s string) string { func Md5String(s string) string {
h := md5.New() h := md5.New()
h.Write([]byte(s)) h.Write([]byte(s))

View File

@@ -2,7 +2,6 @@
// Use of this source code is governed by MIT license. // Use of this source code is governed by MIT license.
// Package datetime implements some functions to format date and time. // Package datetime implements some functions to format date and time.
// Note: // Note:
// 1. `format` param in FormatTimeToStr function should be as flow: // 1. `format` param in FormatTimeToStr function should be as flow:
//"yyyy-mm-dd hh:mm:ss" //"yyyy-mm-dd hh:mm:ss"
@@ -23,7 +22,6 @@
//"mm" //"mm"
//"hh:mm:ss" //"hh:mm:ss"
//"mm:ss" //"mm:ss"
package datetime package datetime
import ( import (

View File

@@ -2,7 +2,6 @@
// Use of this source code is governed by MIT license. // Use of this source code is governed by MIT license.
// Package fileutil implements some basic functions for file operations // Package fileutil implements some basic functions for file operations
package fileutil package fileutil
import ( import (
@@ -12,7 +11,7 @@ import (
"os" "os"
) )
// IsFileExists checks if a file or directory exists // IsExist checks if a file or directory exists
func IsExist(path string) bool { func IsExist(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { if err == nil {
@@ -35,7 +34,7 @@ func CreateFile(path string) bool {
return true return true
} }
// IsFileExists checks if the path is directy or not // IsDir checks if the path is directory or not
func IsDir(path string) bool { func IsDir(path string) bool {
file, err := os.Stat(path) file, err := os.Stat(path)
if err != nil { if err != nil {
@@ -70,9 +69,8 @@ func CopyFile(srcFilePath string, dstFilePath string) error {
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
return nil return nil
} else {
return err
} }
return err
} }
} }
} }

View File

@@ -1,10 +1,11 @@
package fileutil package fileutil
import ( import (
"github.com/duke-git/lancet/utils"
"os" "os"
"reflect" "reflect"
"testing" "testing"
"github.com/duke-git/lancet/utils"
) )
func TestIsExist(t *testing.T) { func TestIsExist(t *testing.T) {

View File

@@ -14,4 +14,4 @@ func Comma(v interface{}, symbol string) string {
return symbol + commaString(s[:dotIndex]) + s[dotIndex:] return symbol + commaString(s[:dotIndex]) + s[dotIndex:]
} }
return symbol + commaString(s) return symbol + commaString(s)
} }

View File

@@ -1,25 +1,26 @@
package formatter package formatter
import ( import (
"github.com/duke-git/lancet/utils"
"testing" "testing"
"github.com/duke-git/lancet/utils"
) )
func TestComma(t *testing.T) { func TestComma(t *testing.T) {
comma(t, "", "","") comma(t, "", "", "")
comma(t, "aa", "","") comma(t, "aa", "", "")
comma(t, "123", "","123") comma(t, "123", "", "123")
comma(t, "12345", "","12,345") comma(t, "12345", "", "12,345")
comma(t, 12345, "","12,345") comma(t, 12345, "", "12,345")
comma(t, 12345, "$","$12,345") comma(t, 12345, "$", "$12,345")
comma(t, 12345, "¥","¥12,345") comma(t, 12345, "¥", "¥12,345")
comma(t, 12345.6789, "","12,345.6789") comma(t, 12345.6789, "", "12,345.6789")
} }
func comma(t *testing.T, test interface{}, symbol string, expected interface{}) { func comma(t *testing.T, test interface{}, symbol string, expected interface{}) {
res:= Comma(test, symbol) res := Comma(test, symbol)
if res != expected { if res != expected {
utils.LogFailedTestInfo(t, "Comma", test, expected, res) utils.LogFailedTestInfo(t, "Comma", test, expected, res)
t.FailNow() t.FailNow()
} }
} }

View File

@@ -18,22 +18,23 @@ func numString(value interface{}) string {
switch reflect.TypeOf(value).Kind() { switch reflect.TypeOf(value).Kind() {
case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64: case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", value) return fmt.Sprintf("%v", value)
case reflect.String: { case reflect.String:
sv := fmt.Sprintf("%v", value) {
if strings.Contains(sv, ".") { sv := fmt.Sprintf("%v", value)
_, err := strconv.ParseFloat(sv, 64) if strings.Contains(sv, ".") {
if err == nil { _, err := strconv.ParseFloat(sv, 64)
return sv if err == nil {
} return sv
}else { }
_, err := strconv.ParseInt(sv, 10, 64) } else {
if err == nil { _, err := strconv.ParseInt(sv, 10, 64)
return sv if err == nil {
return sv
}
} }
} }
}
default: default:
return "" return ""
} }
return "" return ""
} }

View File

@@ -47,6 +47,7 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
return &ip, nil return &ip, nil
} }
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
type PublicIpInfo struct { type PublicIpInfo struct {
Status string `json:"status"` Status string `json:"status"`
Country string `json:"country"` Country string `json:"country"`

View File

@@ -2,9 +2,10 @@ package netutil
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/utils"
"net" "net"
"testing" "testing"
"github.com/duke-git/lancet/utils"
) )
func TestGetInternalIp(t *testing.T) { func TestGetInternalIp(t *testing.T) {

View File

@@ -4,9 +4,10 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"github.com/duke-git/lancet/utils"
"log" "log"
"testing" "testing"
"github.com/duke-git/lancet/utils"
) )
func TestHttpGet(t *testing.T) { func TestHttpGet(t *testing.T) {

View File

@@ -9,6 +9,7 @@ import (
"testing" "testing"
) )
// LogFailedTestInfo log test failed info for internal use
func LogFailedTestInfo(t *testing.T, testCase, input, expected, result interface{}) { func LogFailedTestInfo(t *testing.T, testCase, input, expected, result interface{}) {
errInfo := fmt.Sprintf("Test case %v: input is %+v, expected %v, but result is %v", testCase, input, expected, result) errInfo := fmt.Sprintf("Test case %v: input is %+v, expected %v, but result is %v", testCase, input, expected, result)
t.Error(errInfo) t.Error(errInfo)

View File

@@ -123,9 +123,9 @@ func IsChinesePhone(phone string) bool {
// IsCreditCard check if the string is credit card. // IsCreditCard check if the string is credit card.
func IsCreditCard(creditCart string) bool { func IsCreditCard(creditCart string) bool {
pattern := `^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$` pattern := `^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`
reg := regexp.MustCompile(pattern) reg := regexp.MustCompile(pattern)
return reg.MatchString(creditCart) return reg.MatchString(creditCart)
} }
// IsBase64 check if the string is base64 string. // IsBase64 check if the string is base64 string.
@@ -191,4 +191,3 @@ func IsWeakPassword(password string) bool {
return (num || letter) && !special return (num || letter) && !special
} }