mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-05 21:32:27 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c4b512084 | ||
|
|
4848647918 | ||
|
|
45fc84d1f2 |
11
README.md
11
README.md
@@ -5,7 +5,6 @@
|
||||
</p>
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
English | [简体中文](./README_zh-CN.md)
|
||||
@@ -53,7 +52,7 @@ func main() {
|
||||
|
||||
### 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.
|
||||
- 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
|
||||
```
|
||||
|
||||
#### 2. cryptor is for data encryption and decryption.
|
||||
#### 2. cryptor is for data encryption and decryption
|
||||
|
||||
- Support md5, hmac, aes, des, ras.
|
||||
- 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
|
||||
```
|
||||
|
||||
#### 7. random is for rand string and int generation.
|
||||
#### 7. random is for rand string and int generation
|
||||
|
||||
- Generate random string and int.
|
||||
- Usage: import "github.com/duke-git/lancet/random".
|
||||
@@ -336,7 +335,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
- Function list:
|
||||
- Function list:
|
||||
|
||||
```go
|
||||
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
|
||||
func After(s, char string) string //create substring in source string after position when char first appear
|
||||
|
||||
@@ -20,7 +20,7 @@ func ToBool(s string) (bool, error) {
|
||||
return strconv.ParseBool(s)
|
||||
}
|
||||
|
||||
// ToBool convert interface to bytes
|
||||
// ToBytes convert interface to bytes
|
||||
func ToBytes(data interface{}) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
|
||||
@@ -65,7 +65,7 @@ func AesCbcEncrypt(data, key []byte) []byte {
|
||||
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
|
||||
func AesCbcDecrypt(encrypted, key []byte) []byte {
|
||||
block, _ := aes.NewCipher(key)
|
||||
|
||||
@@ -21,13 +21,13 @@ func Base64StdEncode(s string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(s))
|
||||
}
|
||||
|
||||
// Base64StdEncode decode a base64 encoded string
|
||||
// Base64StdDecode decode a base64 encoded string
|
||||
func Base64StdDecode(s string) string {
|
||||
b, _ := base64.StdEncoding.DecodeString(s)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// Md5Str return the md5 value of string
|
||||
// Md5String return the md5 value of string
|
||||
func Md5String(s string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(s))
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by MIT license.
|
||||
|
||||
// Package datetime implements some functions to format date and time.
|
||||
|
||||
// Note:
|
||||
// 1. `format` param in FormatTimeToStr function should be as flow:
|
||||
//"yyyy-mm-dd hh:mm:ss"
|
||||
@@ -23,7 +22,6 @@
|
||||
//"mm"
|
||||
//"hh:mm:ss"
|
||||
//"mm:ss"
|
||||
|
||||
package datetime
|
||||
|
||||
import (
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by MIT license.
|
||||
|
||||
// Package fileutil implements some basic functions for file operations
|
||||
|
||||
package fileutil
|
||||
|
||||
import (
|
||||
@@ -12,7 +11,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// IsFileExists checks if a file or directory exists
|
||||
// IsExist checks if a file or directory exists
|
||||
func IsExist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
@@ -35,7 +34,7 @@ func CreateFile(path string) bool {
|
||||
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 {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil {
|
||||
@@ -70,9 +69,8 @@ func CopyFile(srcFilePath string, dstFilePath string) error {
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package fileutil
|
||||
|
||||
import (
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/utils"
|
||||
)
|
||||
|
||||
func TestIsExist(t *testing.T) {
|
||||
|
||||
@@ -14,4 +14,4 @@ func Comma(v interface{}, symbol string) string {
|
||||
return symbol + commaString(s[:dotIndex]) + s[dotIndex:]
|
||||
}
|
||||
return symbol + commaString(s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/utils"
|
||||
)
|
||||
|
||||
func TestComma(t *testing.T) {
|
||||
comma(t, "", "","")
|
||||
comma(t, "aa", "","")
|
||||
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.6789, "","12,345.6789")
|
||||
comma(t, "", "", "")
|
||||
comma(t, "aa", "", "")
|
||||
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.6789, "", "12,345.6789")
|
||||
}
|
||||
|
||||
func comma(t *testing.T, test interface{}, symbol string, expected interface{}) {
|
||||
res:= Comma(test, symbol)
|
||||
func comma(t *testing.T, test interface{}, symbol string, expected interface{}) {
|
||||
res := Comma(test, symbol)
|
||||
if res != expected {
|
||||
utils.LogFailedTestInfo(t, "Comma", test, expected, res)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,22 +18,23 @@ func numString(value interface{}) string {
|
||||
switch reflect.TypeOf(value).Kind() {
|
||||
case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64:
|
||||
return fmt.Sprintf("%v", value)
|
||||
case reflect.String: {
|
||||
sv := fmt.Sprintf("%v", value)
|
||||
if strings.Contains(sv, ".") {
|
||||
_, err := strconv.ParseFloat(sv, 64)
|
||||
if err == nil {
|
||||
return sv
|
||||
}
|
||||
}else {
|
||||
_, err := strconv.ParseInt(sv, 10, 64)
|
||||
if err == nil {
|
||||
return sv
|
||||
case reflect.String:
|
||||
{
|
||||
sv := fmt.Sprintf("%v", value)
|
||||
if strings.Contains(sv, ".") {
|
||||
_, err := strconv.ParseFloat(sv, 64)
|
||||
if err == nil {
|
||||
return sv
|
||||
}
|
||||
} else {
|
||||
_, err := strconv.ParseInt(sv, 10, 64)
|
||||
if err == nil {
|
||||
return sv
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
|
||||
return &ip, nil
|
||||
}
|
||||
|
||||
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
|
||||
type PublicIpInfo struct {
|
||||
Status string `json:"status"`
|
||||
Country string `json:"country"`
|
||||
|
||||
@@ -2,9 +2,10 @@ package netutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/utils"
|
||||
)
|
||||
|
||||
func TestGetInternalIp(t *testing.T) {
|
||||
|
||||
@@ -4,9 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/utils"
|
||||
)
|
||||
|
||||
func TestHttpGet(t *testing.T) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// LogFailedTestInfo log test failed info for internal use
|
||||
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)
|
||||
t.Error(errInfo)
|
||||
|
||||
@@ -123,9 +123,9 @@ func IsChinesePhone(phone string) bool {
|
||||
|
||||
// IsCreditCard check if the string is credit card.
|
||||
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})$`
|
||||
reg := regexp.MustCompile(pattern)
|
||||
return reg.MatchString(creditCart)
|
||||
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)
|
||||
return reg.MatchString(creditCart)
|
||||
}
|
||||
|
||||
// IsBase64 check if the string is base64 string.
|
||||
@@ -191,4 +191,3 @@ func IsWeakPassword(password string) bool {
|
||||
|
||||
return (num || letter) && !special
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user