mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 22:22:29 +08:00
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fafb59dad6 | ||
|
|
e36c01cac9 | ||
|
|
aa0afa8d94 | ||
|
|
18e0031e0e | ||
|
|
a5018c110c | ||
|
|
142deb83b2 | ||
|
|
ccc0188352 | ||
|
|
98dba83107 | ||
|
|
9c6aff9030 | ||
|
|
7fd9a94922 | ||
|
|
dd3b1f3aed | ||
|
|
f9dce592e8 | ||
|
|
c4b0967623 | ||
|
|
41613061d5 | ||
|
|
50ef63e27d | ||
|
|
3ae4a35d04 | ||
|
|
89ea0ee15a | ||
|
|
85399e23ed | ||
|
|
19a6f218e6 | ||
|
|
ccaf290b63 | ||
|
|
54745d512c | ||
|
|
39234d4722 | ||
|
|
4b4310265b | ||
|
|
d8a982bf07 | ||
|
|
0334a6f02b | ||
|
|
ae0c613b8e |
21
README.md
21
README.md
@@ -4,13 +4,12 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -36,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.6. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.7. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.6 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.2.7 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -58,7 +57,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -175,6 +174,14 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
||||||
|
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix)
|
||||||
|
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow)
|
||||||
|
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat)
|
||||||
|
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601)
|
||||||
|
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)
|
||||||
|
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat)
|
||||||
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
||||||
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
||||||
|
|
||||||
### Fileutil package implements some basic functions for file operations.
|
### Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
@@ -230,9 +237,12 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Average)
|
||||||
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
||||||
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
||||||
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)
|
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)
|
||||||
|
- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Max)
|
||||||
|
- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Min)
|
||||||
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)
|
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)
|
||||||
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)
|
||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
||||||
@@ -309,6 +319,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
||||||
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
|
||||||
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
|
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
|
||||||
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
|
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
|
||||||
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
|
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
|
||||||
|
|||||||
@@ -4,13 +4,12 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -36,9 +35,9 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.6。</b>
|
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.7。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.6 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.2.7 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -58,7 +57,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -174,6 +173,14 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
||||||
|
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)
|
||||||
|
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)
|
||||||
|
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat)
|
||||||
|
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)
|
||||||
|
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)
|
||||||
|
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)
|
||||||
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
||||||
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
||||||
|
|
||||||
### fileutil包支持文件基本操作。
|
### fileutil包支持文件基本操作。
|
||||||
|
|
||||||
@@ -229,9 +236,12 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Average)
|
||||||
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
||||||
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
||||||
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)
|
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)
|
||||||
|
- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Max)
|
||||||
|
- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Min)
|
||||||
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)
|
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)
|
||||||
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)
|
||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
||||||
@@ -307,6 +317,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
||||||
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
|
||||||
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
|
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
|
||||||
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
|
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
|
||||||
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
|
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []
|
|||||||
preIndex--
|
preIndex--
|
||||||
}
|
}
|
||||||
|
|
||||||
slice[preIndex+1] = preItem
|
slice[preIndex+1] = currentItem
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
return slice
|
||||||
|
|||||||
@@ -89,10 +89,10 @@ func TestInsertionSort(t *testing.T) {
|
|||||||
asssert := internal.NewAssert(t, "TestInsertionSort")
|
asssert := internal.NewAssert(t, "TestInsertionSort")
|
||||||
|
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
sortedPeopleByAge := InsertionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{e 28} {a 20} {c 17} {b 10} {d 8}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -21,14 +22,44 @@ func ToBool(s string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToBytes convert interface to bytes
|
// ToBytes convert interface to bytes
|
||||||
func ToBytes(data any) ([]byte, error) {
|
func ToBytes(value any) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
v := reflect.ValueOf(value)
|
||||||
enc := gob.NewEncoder(&buf)
|
|
||||||
err := enc.Encode(data)
|
switch value.(type) {
|
||||||
if err != nil {
|
case int, int8, int16, int32, int64:
|
||||||
return nil, err
|
number := v.Int()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
|
number := v.Uint()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case float32:
|
||||||
|
number := float32(v.Float())
|
||||||
|
bits := math.Float32bits(number)
|
||||||
|
bytes := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case float64:
|
||||||
|
number := v.Float()
|
||||||
|
bits := math.Float64bits(number)
|
||||||
|
bytes := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case bool:
|
||||||
|
return strconv.AppendBool([]byte{}, v.Bool()), nil
|
||||||
|
case string:
|
||||||
|
return []byte(v.String()), nil
|
||||||
|
case []byte:
|
||||||
|
return v.Bytes(), nil
|
||||||
|
default:
|
||||||
|
newValue, err := json.Marshal(value)
|
||||||
|
return newValue, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToChar convert string to char slice
|
// ToChar convert string to char slice
|
||||||
|
|||||||
@@ -42,14 +42,21 @@ func TestToBytes(t *testing.T) {
|
|||||||
"1",
|
"1",
|
||||||
}
|
}
|
||||||
expected := [][]byte{
|
expected := [][]byte{
|
||||||
{3, 4, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{3, 2, 0, 0},
|
{102, 97, 108, 115, 101},
|
||||||
{4, 12, 0, 1, 49},
|
{49},
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual, _ := ToBytes(cases[i])
|
actual, _ := ToBytes(cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bytesData, err := ToBytes("abc")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
assert.Equal("abc", ToString(bytesData))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToInt(t *testing.T) {
|
func TestToInt(t *testing.T) {
|
||||||
|
|||||||
@@ -44,14 +44,11 @@ func TestDoublyLink_InsertAt(t *testing.T) {
|
|||||||
err := link.InsertAt(1, 1)
|
err := link.InsertAt(1, 1)
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
err = link.InsertAt(0, 1)
|
link.InsertAt(0, 1)
|
||||||
err = link.InsertAt(1, 2)
|
link.InsertAt(1, 2)
|
||||||
err = link.InsertAt(2, 4)
|
link.InsertAt(2, 4)
|
||||||
err = link.InsertAt(2, 3)
|
link.InsertAt(2, 3)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
link.Print()
|
link.Print()
|
||||||
|
|
||||||
expected := []int{1, 2, 3, 4}
|
expected := []int{1, 2, 3, 4}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package datastructure
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/datastructure"
|
"github.com/duke-git/lancet/v2/datastructure"
|
||||||
)
|
)
|
||||||
@@ -144,6 +145,27 @@ func (link *SinglyLink[T]) DeleteAt(index int) error {
|
|||||||
return errors.New("delete error")
|
return errors.New("delete error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteValue delete value in singly linklist
|
||||||
|
func (link *SinglyLink[T]) DeleteValue(value T) {
|
||||||
|
if link.Head == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dummyHead := datastructure.NewLinkNode(value)
|
||||||
|
dummyHead.Next = link.Head
|
||||||
|
current := dummyHead
|
||||||
|
|
||||||
|
for current.Next != nil {
|
||||||
|
if reflect.DeepEqual(current.Next.Value, value) {
|
||||||
|
current.Next = current.Next.Next
|
||||||
|
link.length--
|
||||||
|
} else {
|
||||||
|
current = current.Next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
link.Head = dummyHead.Next
|
||||||
|
}
|
||||||
|
|
||||||
// Reverse the linked list
|
// Reverse the linked list
|
||||||
func (link *SinglyLink[T]) Reverse() {
|
func (link *SinglyLink[T]) Reverse() {
|
||||||
var pre, next *datastructure.LinkNode[T]
|
var pre, next *datastructure.LinkNode[T]
|
||||||
|
|||||||
@@ -45,20 +45,28 @@ func TestSinglyLink_InsertAt(t *testing.T) {
|
|||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
err = link.InsertAt(0, 1)
|
err = link.InsertAt(0, 1)
|
||||||
err = link.InsertAt(1, 2)
|
|
||||||
err = link.InsertAt(2, 4)
|
|
||||||
err = link.InsertAt(2, 3)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
err = link.InsertAt(1, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
err = link.InsertAt(2, 4)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
err = link.InsertAt(2, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
link.Print()
|
link.Print()
|
||||||
|
|
||||||
expected := []int{1, 2, 3, 4}
|
expected := []int{1, 2, 3, 4}
|
||||||
values := link.Values()
|
values := link.Values()
|
||||||
|
|
||||||
assert.Equal(expected, values)
|
assert.Equal(expected, values)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSinglyLink_DeleteAtHead(t *testing.T) {
|
func TestSinglyLink_DeleteAtHead(t *testing.T) {
|
||||||
@@ -103,6 +111,24 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
|
|||||||
assert.Equal(expected, values)
|
assert.Equal(expected, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSinglyLink_DeleteValue(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteValue")
|
||||||
|
|
||||||
|
link := NewSinglyLink[int]()
|
||||||
|
|
||||||
|
link.InsertAtTail(1)
|
||||||
|
link.InsertAtTail(2)
|
||||||
|
link.InsertAtTail(2)
|
||||||
|
link.InsertAtTail(3)
|
||||||
|
link.InsertAtTail(4)
|
||||||
|
|
||||||
|
link.DeleteValue(2)
|
||||||
|
assert.Equal([]int{1, 3, 4}, link.Values())
|
||||||
|
|
||||||
|
link.DeleteValue(1)
|
||||||
|
assert.Equal([]int{3, 4}, link.Values())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSinglyLink_DeleteAt(t *testing.T) {
|
func TestSinglyLink_DeleteAt(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
||||||
|
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func TestPopFirst(t *testing.T) {
|
|||||||
assert.Equal([]int{2, 3}, list.Data())
|
assert.Equal([]int{2, 3}, list.Data())
|
||||||
|
|
||||||
list2 := NewList([]int{})
|
list2 := NewList([]int{})
|
||||||
v, ok = list2.PopFirst()
|
_, ok = list2.PopFirst()
|
||||||
assert.Equal(false, ok)
|
assert.Equal(false, ok)
|
||||||
assert.Equal([]int{}, list2.Data())
|
assert.Equal([]int{}, list2.Data())
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ func TestPopLast(t *testing.T) {
|
|||||||
assert.Equal([]int{1, 2}, list.Data())
|
assert.Equal([]int{1, 2}, list.Data())
|
||||||
|
|
||||||
list2 := NewList([]int{})
|
list2 := NewList([]int{})
|
||||||
v, ok = list2.PopLast()
|
_, ok = list2.PopLast()
|
||||||
assert.Equal(false, ok)
|
assert.Equal(false, ok)
|
||||||
assert.Equal([]int{}, list2.Data())
|
assert.Equal([]int{}, list2.Data())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package datastructure implements some data structure. eg. list, linklist, stack, queue, tree, graph.
|
||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
// LinkNode is a linkedlist node, which have a Value and Pre points to previous node, Next points to a next node of the link.
|
// LinkNode is a linkedlist node, which have a Value and Pre points to previous node, Next points to a next node of the link.
|
||||||
|
|||||||
@@ -40,14 +40,14 @@ func TestLinkedQueue_Front(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedQueue_Front")
|
assert := internal.NewAssert(t, "TestLinkedQueue_Front")
|
||||||
|
|
||||||
queue := NewLinkedQueue[int]()
|
queue := NewLinkedQueue[int]()
|
||||||
val, err := queue.Front()
|
_, err := queue.Front()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
queue.EnQueue(1)
|
queue.EnQueue(1)
|
||||||
queue.EnQueue(2)
|
queue.EnQueue(2)
|
||||||
queue.EnQueue(3)
|
queue.EnQueue(3)
|
||||||
|
|
||||||
val, err = queue.Front()
|
val, err := queue.Front()
|
||||||
assert.Equal(1, *val)
|
assert.Equal(1, *val)
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
}
|
}
|
||||||
@@ -56,14 +56,14 @@ func TestLinkedQueue_Back(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedQueue_Back")
|
assert := internal.NewAssert(t, "TestLinkedQueue_Back")
|
||||||
|
|
||||||
queue := NewLinkedQueue[int]()
|
queue := NewLinkedQueue[int]()
|
||||||
val, err := queue.Back()
|
_, err := queue.Back()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
queue.EnQueue(1)
|
queue.EnQueue(1)
|
||||||
queue.EnQueue(2)
|
queue.EnQueue(2)
|
||||||
queue.EnQueue(3)
|
queue.EnQueue(3)
|
||||||
|
|
||||||
val, err = queue.Back()
|
val, err := queue.Back()
|
||||||
assert.Equal(3, *val)
|
assert.Equal(3, *val)
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,14 +26,14 @@ func TestArrayStack_Pop(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestArrayStack_Pop")
|
assert := internal.NewAssert(t, "TestArrayStack_Pop")
|
||||||
|
|
||||||
stack := NewArrayStack[int]()
|
stack := NewArrayStack[int]()
|
||||||
topItem, err := stack.Pop()
|
_, err := stack.Pop()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Pop()
|
topItem, err := stack.Pop()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
@@ -45,14 +45,14 @@ func TestArrayStack_Peak(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestArrayStack_Peak")
|
assert := internal.NewAssert(t, "TestArrayStack_Peak")
|
||||||
|
|
||||||
stack := NewArrayStack[int]()
|
stack := NewArrayStack[int]()
|
||||||
topItem, err := stack.Peak()
|
_, err := stack.Peak()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Peak()
|
topItem, err := stack.Peak()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ func TestLinkedStack_Pop(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedStack_Pop")
|
assert := internal.NewAssert(t, "TestLinkedStack_Pop")
|
||||||
|
|
||||||
stack := NewLinkedStack[int]()
|
stack := NewLinkedStack[int]()
|
||||||
topItem, err := stack.Pop()
|
_, err := stack.Pop()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Pop()
|
topItem, err := stack.Pop()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
@@ -48,14 +48,14 @@ func TestLinkedStack_Peak(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedStack_Peak")
|
assert := internal.NewAssert(t, "TestLinkedStack_Peak")
|
||||||
|
|
||||||
stack := NewLinkedStack[int]()
|
stack := NewLinkedStack[int]()
|
||||||
topItem, err := stack.Peak()
|
_, err := stack.Peak()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Peak()
|
topItem, err := stack.Peak()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
|
|||||||
60
datetime/conversion.go
Normal file
60
datetime/conversion.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license.
|
||||||
|
|
||||||
|
package datetime
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnixNow return unix timestamp of current time
|
||||||
|
func NewUnixNow() *theTime {
|
||||||
|
return &theTime{unix: time.Now().Unix()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnix return unix timestamp of specified time
|
||||||
|
func NewUnix(unix int64) *theTime {
|
||||||
|
return &theTime{unix: unix}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss"
|
||||||
|
func NewFormat(t string) (*theTime, error) {
|
||||||
|
timeLayout := "2006-01-02 15:04:05"
|
||||||
|
loc := time.FixedZone("CST", 8*3600)
|
||||||
|
tt, err := time.ParseInLocation(timeLayout, t, loc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &theTime{unix: tt.Unix()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewISO8601 return unix timestamp of specified iso8601 time string
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error) {
|
||||||
|
t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &theTime{unix: t.Unix()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToUnix return unix timestamp
|
||||||
|
func (t *theTime) ToUnix() int64 {
|
||||||
|
return t.unix
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time
|
||||||
|
func (t *theTime) ToFormat() string {
|
||||||
|
return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormatForTpl return the time string which format is specified tpl
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string {
|
||||||
|
return time.Unix(t.unix, 0).Format(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormatForTpl return iso8601 time string
|
||||||
|
func (t *theTime) ToIso8601() string {
|
||||||
|
return time.Unix(t.unix, 0).Format(time.RFC3339)
|
||||||
|
}
|
||||||
53
datetime/conversion_test.go
Normal file
53
datetime/conversion_test.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package datetime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToUnix(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToUnix")
|
||||||
|
|
||||||
|
tm1 := NewUnixNow()
|
||||||
|
unixTimestamp := tm1.ToUnix()
|
||||||
|
tm2 := NewUnix(unixTimestamp)
|
||||||
|
|
||||||
|
assert.Equal(tm1, tm2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFormat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToFormat")
|
||||||
|
|
||||||
|
_, err := NewFormat("2022/03/18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToFormat -> ", tm.ToFormat())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFormatForTpl(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToFormatForTpl")
|
||||||
|
|
||||||
|
_, err := NewFormat("2022/03/18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToFormatForTpl -> ", tm.ToFormatForTpl("2006/01/02 15:04:05"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToIso8601(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToIso8601")
|
||||||
|
|
||||||
|
_, err := NewISO8601("2022-03-18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err := NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToIso8601 -> ", tm.ToIso8601())
|
||||||
|
}
|
||||||
232
docs/datetime.md
232
docs/datetime.md
@@ -6,6 +6,7 @@ Package datetime supports date and time format and compare.
|
|||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
|
- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -28,7 +29,6 @@ import (
|
|||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
@@ -41,9 +41,19 @@ import (
|
|||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
|
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
|
- [NewUnixNow](#NewUnixNow)
|
||||||
|
- [NewUnix](#NewUnix)
|
||||||
|
- [NewFormat](#NewFormat)
|
||||||
|
- [NewISO8601](#NewISO8601)
|
||||||
|
- [ToUnix](#ToUnix)
|
||||||
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
@@ -667,3 +677,221 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
<p>Return unix timestamp of current time</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnixNow() *theTime
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
<p>Return unix timestamp of specified int64 value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnix(unix int64) *theTime
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnix(1647597438)
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewFormat(t string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm) //&{1647594245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
<p>Return unix timestamp of specified iso8601 time string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
fmt.Println(tm) //&{1136214245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
<p>Return unix timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToUnix() int64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm.ToUnix()) //1647597438
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormat() string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
<p>Return the time string which format is specified tpl.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
|
||||||
|
fmt.Println(ts) //"2022/03/18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
<p>Return iso8601 time string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToIso8601() string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
ts := tm.ToIso8601()
|
||||||
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
@@ -43,6 +42,15 @@ import (
|
|||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
|
- [NewUnixNow](#NewUnixNow)
|
||||||
|
- [NewUnix](#NewUnix)
|
||||||
|
- [NewFormat](#NewFormat)
|
||||||
|
- [NewISO8601](#NewISO8601)
|
||||||
|
- [ToUnix](#ToUnix)
|
||||||
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
@@ -665,4 +673,221 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
<p>创建一个当前时间的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnixNow() *theTime
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
<p>创建一个unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnix(unix int64) *theTime
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnix(1647597438)
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewFormat(t string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm) //&{1647594245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
fmt.Println(tm) //&{1136214245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
<p>返回unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToUnix() int64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm.ToUnix()) //1647597438
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormat() string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
<p>返回tpl格式指定的日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
|
||||||
|
fmt.Println(ts) //"2022/03/18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
<p>返回iso8601日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToIso8601() string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
ts := tm.ToIso8601()
|
||||||
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -20,9 +20,13 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
- [Average](#Average)
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
|
- [Max](#Max)
|
||||||
|
- [Min](#Min)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
@@ -33,6 +37,35 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Average">Average</span>
|
||||||
|
<p>Return average value of numbers. Maybe call RoundToFloat to round result.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Average[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Average(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Average(1, 1)) //1
|
||||||
|
avg := mathutil.Average(1.2, 1.4) //1.2999999998
|
||||||
|
roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
<p>Calculate x to the nth power.</p>
|
<p>Calculate x to the nth power.</p>
|
||||||
|
|
||||||
@@ -117,6 +150,60 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Max">Max</span>
|
||||||
|
<p>Return max value of numbers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Max[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Max(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Max(1, 2, 3)) //3
|
||||||
|
fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Min">Min</span>
|
||||||
|
<p>Return min value of numbers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Min[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Min(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Min(1, 2, 3)) //1
|
||||||
|
fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,13 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
- [Average](#Average)
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
|
- [Max](#Max)
|
||||||
|
- [Min](#Min)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
@@ -34,6 +37,33 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Average">Average</span>
|
||||||
|
<p>计算平均数. 可能需要对结果调用RoundToFloat方法四舍五入</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Average[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Average(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Average(1, 1)) //1
|
||||||
|
avg := mathutil.Average(1.2, 1.4) //1.2999999998
|
||||||
|
roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
<p>指数计算(x的n次方)</p>
|
<p>指数计算(x的n次方)</p>
|
||||||
|
|
||||||
@@ -117,6 +147,59 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Max">Max</span>
|
||||||
|
<p>返回参数中的最大数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Max[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Max(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Max(1, 2, 3)) //3
|
||||||
|
fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Min">Min</span>
|
||||||
|
<p>返回参数中的最小数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Min[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Min(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Min(1, 2, 3)) //1
|
||||||
|
fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
<p>计算百分比,保留n位小数</p>
|
<p>计算百分比,保留n位小数</p>
|
||||||
|
|||||||
@@ -37,8 +37,9 @@ import (
|
|||||||
- [FindLast](#FindLast)
|
- [FindLast](#FindLast)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
|
- [GroupWith](#GroupWith)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -564,6 +565,34 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GroupWith">GroupWith</span>
|
||||||
|
<p>Return a map composed of keys generated from the results of running each element of slice thru iteratee.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
res := slice.GroupWith(nums, floor)
|
||||||
|
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
<p>Convert interface slice to int slice.</p>
|
<p>Convert interface slice to int slice.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import (
|
|||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
|
- [GroupWith](#GroupWith)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -565,6 +566,32 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GroupWith">GroupWith</span>
|
||||||
|
<p>创建一个map,key是iteratee遍历slice中的每个元素返回的结果。 分组值的顺序是由他们出现在slice中的顺序确定的。每个键对应的值负责生成key的元素组成的数组。iteratee调用1个参数: (value)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
res := slice.GroupWith(nums, floor)
|
||||||
|
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
<p>将接口切片转换为int切片</p>
|
<p>将接口切片转换为int切片</p>
|
||||||
|
|||||||
@@ -76,10 +76,10 @@ func TestCopyFile(t *testing.T) {
|
|||||||
func TestListFileNames(t *testing.T) {
|
func TestListFileNames(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestListFileNames")
|
assert := internal.NewAssert(t, "TestListFileNames")
|
||||||
|
|
||||||
filesInPath, err := ListFileNames("../datetime/")
|
filesInPath, err := ListFileNames("./")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
expected := []string{"datetime.go", "datetime_test.go"}
|
expected := []string{"file.go", "file_test.go"}
|
||||||
assert.Equal(expected, filesInPath)
|
assert.Equal(expected, filesInPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,3 +11,8 @@ type Comparator interface {
|
|||||||
// Descending order: should return 1 -> v1 < v2, 0 -> v1 = v2, -1 -> v1 > v2
|
// Descending order: should return 1 -> v1 < v2, 0 -> v1 = v2, -1 -> v1 > v2
|
||||||
Compare(v1, v2 any) int
|
Compare(v1, v2 any) int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Number contains all types of number and uintptr, used for generics constraint
|
||||||
|
type Number interface {
|
||||||
|
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Exponent calculate x^n
|
// Exponent calculate x^n
|
||||||
@@ -90,3 +92,40 @@ func TruncRound(x float64, n int) float64 {
|
|||||||
res, _ := strconv.ParseFloat(newFloat, 64)
|
res, _ := strconv.ParseFloat(newFloat, 64)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Max return max value of params
|
||||||
|
func Max[T lancetconstraints.Number](numbers ...T) T {
|
||||||
|
max := numbers[0]
|
||||||
|
|
||||||
|
for _, v := range numbers {
|
||||||
|
if max < v {
|
||||||
|
max = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min return min value of params
|
||||||
|
func Min[T lancetconstraints.Number](numbers ...T) T {
|
||||||
|
min := numbers[0]
|
||||||
|
|
||||||
|
for _, v := range numbers {
|
||||||
|
if min > v {
|
||||||
|
min = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
|
||||||
|
// Average return average value of params
|
||||||
|
func Average[T lancetconstraints.Number](numbers ...T) T {
|
||||||
|
var sum T
|
||||||
|
n := T(len(numbers))
|
||||||
|
|
||||||
|
for _, v := range numbers {
|
||||||
|
sum += v
|
||||||
|
}
|
||||||
|
return sum / n
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,3 +70,29 @@ func TestTruncRound(t *testing.T) {
|
|||||||
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
||||||
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAverage(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAverage")
|
||||||
|
|
||||||
|
assert.Equal(Average(0, 0), 0)
|
||||||
|
assert.Equal(Average(1, 1), 1)
|
||||||
|
avg := Average(1.2, 1.4)
|
||||||
|
t.Log(avg)
|
||||||
|
assert.Equal(1.3, RoundToFloat(avg, 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMax(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMax")
|
||||||
|
|
||||||
|
assert.Equal(Max(0, 0), 0)
|
||||||
|
assert.Equal(Max(1, 2, 3), 3)
|
||||||
|
assert.Equal(Max(1.2, 1.4, 1.1, 1.4), 1.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMin(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMin")
|
||||||
|
|
||||||
|
assert.Equal(Min(0, 0), 0)
|
||||||
|
assert.Equal(Min(1, 2, 3), 1)
|
||||||
|
assert.Equal(Min(1.2, 1.4, 1.1, 1.4), 1.1)
|
||||||
|
}
|
||||||
|
|||||||
@@ -253,6 +253,25 @@ func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) {
|
|||||||
return groupA, groupB
|
return groupA, groupB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GroupWith return a map composed of keys generated from the results of running each element of slice thru iteratee.
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
||||||
|
if iteratee == nil {
|
||||||
|
panic("iteratee func is missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
res := make(map[U][]T)
|
||||||
|
|
||||||
|
for _, v := range slice {
|
||||||
|
key := iteratee(v)
|
||||||
|
if _, ok := res[key]; !ok {
|
||||||
|
res[key] = []T{}
|
||||||
|
}
|
||||||
|
res[key] = append(res[key], v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
||||||
// If return T is nil then no items matched the predicate func
|
// If return T is nil then no items matched the predicate func
|
||||||
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) {
|
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package slice
|
package slice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -142,6 +143,20 @@ func TestGroupBy(t *testing.T) {
|
|||||||
assert.Equal(expectedOdd, odd)
|
assert.Equal(expectedOdd, odd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGroupWith(t *testing.T) {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
expected := map[float64][]float64{
|
||||||
|
4: {4.2},
|
||||||
|
6: {6.1, 6.3},
|
||||||
|
}
|
||||||
|
actual := GroupWith(nums, floor)
|
||||||
|
assert := internal.NewAssert(t, "TestGroupWith")
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCount(t *testing.T) {
|
func TestCount(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 4, 5, 6}
|
nums := []int{1, 2, 3, 4, 5, 6}
|
||||||
evenFunc := func(i, num int) bool {
|
evenFunc := func(i, num int) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user