mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 05:42:26 +08:00
Compare commits
6 Commits
v2.1.18
...
6e7300bbbf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e7300bbbf | ||
|
|
3685aee02b | ||
|
|
046f3e0bf9 | ||
|
|
c01c9d14b4 | ||
|
|
f198191063 | ||
|
|
8bdd46bda4 |
@@ -711,12 +711,16 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
[[play](https://go.dev/play/p/akLWz0EqOSM)]
|
[[play](https://go.dev/play/p/akLWz0EqOSM)]
|
||||||
- **<big>AngleToRadian</big>** : converts angle value to radian value.
|
- **<big>AngleToRadian</big>** : converts angle value to radian value.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#AngleToRadian)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#AngleToRadian)]
|
||||||
|
[[play](https://go.dev/play/p/CIvlICqrHql)]
|
||||||
- **<big>RadianToAngle</big>** : converts radian value to angle value.
|
- **<big>RadianToAngle</big>** : converts radian value to angle value.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RadianToAngle)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RadianToAngle)]
|
||||||
|
[[play](https://go.dev/play/p/dQtmOTUOMgi)]
|
||||||
- **<big>PointDistance</big>** : get two points distance.
|
- **<big>PointDistance</big>** : get two points distance.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#PointDistance)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#PointDistance)]
|
||||||
|
[[play](https://go.dev/play/p/RrG4JIaziM8)]
|
||||||
- **<big>IsPrime</big>** : checks if number is prime number.
|
- **<big>IsPrime</big>** : checks if number is prime number.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#IsPrime)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#IsPrime)]
|
||||||
|
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
|
||||||
|
|
||||||
### 13. Netutil package contains functions to get net information and send http request.
|
### 13. Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
@@ -1150,6 +1154,8 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/bj7_odx3vRf)]
|
[[play](https://go.dev/play/p/bj7_odx3vRf)]
|
||||||
- **<big>RemoveNonPrintable</big>** : remove non-printable characters from a string.
|
- **<big>RemoveNonPrintable</big>** : remove non-printable characters from a string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#RemoveNonPrintable)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#RemoveNonPrintable)]
|
||||||
|
[[play](https://go.dev/play/p/og47F5x_jTZ)]
|
||||||
|
|
||||||
|
|
||||||
### 19. System package contain some functions about os, runtime, shell command.
|
### 19. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
@@ -1281,8 +1287,10 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
[[play](https://go.dev/play/p/E2nt3unlmzP)]
|
[[play](https://go.dev/play/p/E2nt3unlmzP)]
|
||||||
- **<big>IsASCII</big>** : checks if string is all ASCII char.
|
- **<big>IsASCII</big>** : checks if string is all ASCII char.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsASCII)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsASCII)]
|
||||||
|
[[play](https://go.dev/play/p/hfQNPLX0jNa)]
|
||||||
- **<big>IsPrintable</big>** : checks if string is all printable chars.
|
- **<big>IsPrintable</big>** : checks if string is all printable chars.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsPrintable)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsPrintable)]
|
||||||
|
[[play](https://go.dev/play/p/Pe1FE2gdtTP)]
|
||||||
|
|
||||||
### 21. xerror package implements helpers for errors.
|
### 21. xerror package implements helpers for errors.
|
||||||
|
|
||||||
|
|||||||
@@ -710,12 +710,16 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
[[play](https://go.dev/play/p/akLWz0EqOSM)]
|
[[play](https://go.dev/play/p/akLWz0EqOSM)]
|
||||||
- **<big>AngleToRadian</big>** : 将角度值转为弧度值。
|
- **<big>AngleToRadian</big>** : 将角度值转为弧度值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#AngleToRadian)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#AngleToRadian)]
|
||||||
|
[[play](https://go.dev/play/p/CIvlICqrHql)]
|
||||||
- **<big>RadianToAngle</big>** : 将弧度值转为角度值。
|
- **<big>RadianToAngle</big>** : 将弧度值转为角度值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RadianToAngle)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RadianToAngle)]
|
||||||
|
[[play](https://go.dev/play/p/dQtmOTUOMgi)]
|
||||||
- **<big>PointDistance</big>** : 计算两个坐标点的距离。
|
- **<big>PointDistance</big>** : 计算两个坐标点的距离。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#PointDistance)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#PointDistance)]
|
||||||
|
[[play](https://go.dev/play/p/RrG4JIaziM8)]
|
||||||
- **<big>IsPrime</big>** : 判断质数。
|
- **<big>IsPrime</big>** : 判断质数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#IsPrime)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#IsPrime)]
|
||||||
|
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
|
||||||
|
|
||||||
### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
@@ -1152,6 +1156,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/bj7_odx3vRf)]
|
[[play](https://go.dev/play/p/bj7_odx3vRf)]
|
||||||
- **<big>RemoveNonPrintable</big>** : 删除字符串中不可打印的字符。
|
- **<big>RemoveNonPrintable</big>** : 删除字符串中不可打印的字符。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#RemoveNonPrintable)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#RemoveNonPrintable)]
|
||||||
|
[[play](https://go.dev/play/p/og47F5x_jTZ)]
|
||||||
|
|
||||||
### 19. system 包含 os, runtime, shell command 的相关函数。
|
### 19. system 包含 os, runtime, shell command 的相关函数。
|
||||||
|
|
||||||
@@ -1283,8 +1288,10 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
[[play](https://go.dev/play/p/E2nt3unlmzP)]
|
[[play](https://go.dev/play/p/E2nt3unlmzP)]
|
||||||
- **<big>IsASCII</big>** : 验证字符串全部为 ASCII 字符。
|
- **<big>IsASCII</big>** : 验证字符串全部为 ASCII 字符。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsASCII)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsASCII)]
|
||||||
|
[[play](https://go.dev/play/p/hfQNPLX0jNa)]
|
||||||
- **<big>IsPrintable</big>** : 检查字符串是否全部为可打印字符。
|
- **<big>IsPrintable</big>** : 检查字符串是否全部为可打印字符。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsPrintable)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsPrintable)]
|
||||||
|
[[play](https://go.dev/play/p/Pe1FE2gdtTP)]
|
||||||
|
|
||||||
### 21. xerror 包实现一些错误处理函数
|
### 21. xerror 包实现一些错误处理函数
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
- [CreateFile](#CreateFile)
|
- [CreateFile](#CreateFile)
|
||||||
- [CreateDir](#CreateDir)
|
- [CreateDir](#CreateDir)
|
||||||
- [CopyFile](#CopyFile)
|
- [CopyFile](#CopyFile)
|
||||||
|
- [CurrentPath](#CurrentPath)
|
||||||
- [FileMode](#FileMode)
|
- [FileMode](#FileMode)
|
||||||
- [MiMeType](#MiMeType)
|
- [MiMeType](#MiMeType)
|
||||||
- [IsExist](#IsExist)
|
- [IsExist](#IsExist)
|
||||||
@@ -151,6 +152,32 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="CurrentPath">CurrentPath</span>
|
||||||
|
|
||||||
|
<p>return current absolute path.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CurrentPath() string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
absPath := CurrentPath()
|
||||||
|
fmt.Println(absPath)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="FileMode">FileMode</span>
|
### <span id="FileMode">FileMode</span>
|
||||||
|
|
||||||
<p>Return file mode infomation.</p>
|
<p>Return file mode infomation.</p>
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
- [CreateFile](#CreateFile)
|
- [CreateFile](#CreateFile)
|
||||||
- [CreateDir](#CreateDir)
|
- [CreateDir](#CreateDir)
|
||||||
- [CopyFile](#CopyFile)
|
- [CopyFile](#CopyFile)
|
||||||
|
- [CurrentPath](#CurrentPath)
|
||||||
- [FileMode](#FileMode)
|
- [FileMode](#FileMode)
|
||||||
- [MiMeType](#MiMeType)
|
- [MiMeType](#MiMeType)
|
||||||
- [IsExist](#IsExist)
|
- [IsExist](#IsExist)
|
||||||
@@ -150,6 +151,32 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="CurrentPath">CurrentPath</span>
|
||||||
|
|
||||||
|
<p>返回当前位置的绝对路径。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CurrentPath() string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
absPath := CurrentPath()
|
||||||
|
fmt.Println(absPath)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="FileMode">FileMode</span>
|
### <span id="FileMode">FileMode</span>
|
||||||
|
|
||||||
<p>获取文件mode信息</p>
|
<p>获取文件mode信息</p>
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ import (
|
|||||||
- [Merge](#Merge)
|
- [Merge](#Merge)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
|
- [ReduceBy](#ReduceBy)
|
||||||
|
- [ReduceRight](#ReduceRight)
|
||||||
- [Replace](#Replace)
|
- [Replace](#Replace)
|
||||||
- [ReplaceAll](#ReplaceAll)
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Repeat](#Repeat)
|
- [Repeat](#Repeat)
|
||||||
@@ -1504,6 +1506,72 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceBy">ReduceBy</span>
|
||||||
|
|
||||||
|
<p>Produces a value from slice by accumulating the result of each element as passed through the reducer function.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := slice.ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10
|
||||||
|
// 1234
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceRight">ReduceRight</span>
|
||||||
|
|
||||||
|
<p>ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := slice.ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4321
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Replace">Replace</span>
|
### <span id="Replace">Replace</span>
|
||||||
|
|
||||||
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
|
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ import (
|
|||||||
- [Merge](#Merge)
|
- [Merge](#Merge)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
|
- [ReduceBy](#ReduceBy)
|
||||||
|
- [ReduceRight](#ReduceRight)
|
||||||
- [Replace](#Replace)
|
- [Replace](#Replace)
|
||||||
- [ReplaceAll](#ReplaceAll)
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Repeat](#Repeat)
|
- [Repeat](#Repeat)
|
||||||
@@ -1505,6 +1507,72 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceBy">ReduceBy</span>
|
||||||
|
|
||||||
|
<p>对切片中执行reduce操作。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := slice.ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10
|
||||||
|
// 1234
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceRight">ReduceRight</span>
|
||||||
|
|
||||||
|
<p>类似ReduceBy操作,迭代切片元素顺序从右至左。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := slice.ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4321
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Replace">Replace</span>
|
### <span id="Replace">Replace</span>
|
||||||
|
|
||||||
<p>返回切片的副本,其中前n个不重叠的old替换为new</p>
|
<p>返回切片的副本,其中前n个不重叠的old替换为new</p>
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -345,3 +346,15 @@ func MiMeType(file any) string {
|
|||||||
}
|
}
|
||||||
return mediatype
|
return mediatype
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CurrentPath return current absolute path.
|
||||||
|
// Play: todo
|
||||||
|
func CurrentPath() string {
|
||||||
|
var absPath string
|
||||||
|
_, filename, _, ok := runtime.Caller(1)
|
||||||
|
if ok {
|
||||||
|
absPath = path.Dir(filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
return absPath
|
||||||
|
}
|
||||||
|
|||||||
@@ -241,3 +241,8 @@ func TestListFileNames(t *testing.T) {
|
|||||||
expected := []string{"formatter.go", "formatter_example_test.go", "formatter_test.go"}
|
expected := []string{"formatter.go", "formatter_example_test.go", "formatter_test.go"}
|
||||||
assert.Equal(expected, filesInPath)
|
assert.Equal(expected, filesInPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCurrentPath(t *testing.T) {
|
||||||
|
absPath := CurrentPath()
|
||||||
|
t.Log(absPath)
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ package formatter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
"github.com/duke-git/lancet/v2/validator"
|
||||||
"golang.org/x/exp/constraints"
|
"golang.org/x/exp/constraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,54 +18,29 @@ import (
|
|||||||
// Comma("12345", "$") => "$12,345", Comma(12345, "$") => "$12,345"
|
// Comma("12345", "$") => "$12,345", Comma(12345, "$") => "$12,345"
|
||||||
// Play: https://go.dev/play/p/eRD5k2vzUVX
|
// Play: https://go.dev/play/p/eRD5k2vzUVX
|
||||||
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string {
|
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string {
|
||||||
s, err := numberToString(value)
|
if validator.IsInt(value) {
|
||||||
if err != nil {
|
v, err := convertor.ToInt(value)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return symbol + commaInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if validator.IsFloat(value) {
|
||||||
|
v, err := convertor.ToFloat(value)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return symbol + commaFloat(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strutil.IsString(value) {
|
||||||
|
v := fmt.Sprintf("%v", value)
|
||||||
|
if validator.IsNumberStr(v) {
|
||||||
|
return symbol + commaStr(v)
|
||||||
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
dotIndex := strings.Index(s, ".")
|
return ""
|
||||||
if dotIndex != -1 {
|
|
||||||
return symbol + commaString(s[:dotIndex]) + s[dotIndex:]
|
|
||||||
}
|
|
||||||
|
|
||||||
return symbol + commaString(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func commaString(s string) string {
|
|
||||||
if len(s) <= 3 {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return commaString(s[:len(s)-3]) + "," + commaString(s[len(s)-3:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func numberToString(value any) (string, error) {
|
|
||||||
switch reflect.TypeOf(value).Kind() {
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
||||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
||||||
return fmt.Sprintf("%v", value), nil
|
|
||||||
|
|
||||||
// todo: need to handle 12345678.9 => 1.23456789e+07
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
return fmt.Sprintf("%v", value), nil
|
|
||||||
|
|
||||||
case reflect.String:
|
|
||||||
{
|
|
||||||
sv := fmt.Sprintf("%v", value)
|
|
||||||
if strings.Contains(sv, ".") {
|
|
||||||
_, err := strconv.ParseFloat(sv, 64)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return sv, nil
|
|
||||||
} else {
|
|
||||||
_, err := strconv.ParseInt(sv, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
return sv, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
85
formatter/formatter_internal.go
Normal file
85
formatter/formatter_internal.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package formatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// see https://github.com/dustin/go-humanize/blob/master/comma.go
|
||||||
|
func commaInt(v int64) string {
|
||||||
|
sign := ""
|
||||||
|
|
||||||
|
// Min int64 can't be negated to a usable value, so it has to be special cased.
|
||||||
|
if v == math.MinInt64 {
|
||||||
|
return "-9,223,372,036,854,775,808"
|
||||||
|
}
|
||||||
|
|
||||||
|
if v < 0 {
|
||||||
|
sign = "-"
|
||||||
|
v = 0 - v
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := []string{"", "", "", "", "", "", ""}
|
||||||
|
j := len(parts) - 1
|
||||||
|
|
||||||
|
for v > 999 {
|
||||||
|
parts[j] = strconv.FormatInt(v%1000, 10)
|
||||||
|
switch len(parts[j]) {
|
||||||
|
case 2:
|
||||||
|
parts[j] = "0" + parts[j]
|
||||||
|
case 1:
|
||||||
|
parts[j] = "00" + parts[j]
|
||||||
|
}
|
||||||
|
v = v / 1000
|
||||||
|
j--
|
||||||
|
}
|
||||||
|
parts[j] = strconv.Itoa(int(v))
|
||||||
|
return sign + strings.Join(parts[j:], ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
func commaFloat(v float64) string {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
if v < 0 {
|
||||||
|
buf.Write([]byte{'-'})
|
||||||
|
v = 0 - v
|
||||||
|
}
|
||||||
|
|
||||||
|
comma := []byte{','}
|
||||||
|
|
||||||
|
parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
|
||||||
|
pos := 0
|
||||||
|
if len(parts[0])%3 != 0 {
|
||||||
|
pos += len(parts[0]) % 3
|
||||||
|
buf.WriteString(parts[0][:pos])
|
||||||
|
buf.Write(comma)
|
||||||
|
}
|
||||||
|
for ; pos < len(parts[0]); pos += 3 {
|
||||||
|
buf.WriteString(parts[0][pos : pos+3])
|
||||||
|
buf.Write(comma)
|
||||||
|
}
|
||||||
|
buf.Truncate(buf.Len() - 1)
|
||||||
|
|
||||||
|
if len(parts) > 1 {
|
||||||
|
buf.Write([]byte{'.'})
|
||||||
|
buf.WriteString(parts[1])
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func commaStr(s string) string {
|
||||||
|
dotIndex := strings.Index(s, ".")
|
||||||
|
if dotIndex != -1 {
|
||||||
|
return commaStrRecursive(s[:dotIndex]) + s[dotIndex:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return commaStrRecursive(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func commaStrRecursive(s string) string {
|
||||||
|
if len(s) <= 3 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return commaStrRecursive(s[:len(s)-3]) + "," + commaStrRecursive(s[len(s)-3:])
|
||||||
|
}
|
||||||
@@ -16,12 +16,13 @@ func TestComma(t *testing.T) {
|
|||||||
assert.Equal("12,345", Comma("12345", ""))
|
assert.Equal("12,345", Comma("12345", ""))
|
||||||
assert.Equal("12,345.6789", Comma("12345.6789", ""))
|
assert.Equal("12,345.6789", Comma("12345.6789", ""))
|
||||||
assert.Equal("123,456,789,000", Comma("123456789000", ""))
|
assert.Equal("123,456,789,000", Comma("123456789000", ""))
|
||||||
|
assert.Equal("12,345,678.9", Comma("12345678.9", ""))
|
||||||
|
|
||||||
assert.Equal("12,345", Comma(12345, ""))
|
assert.Equal("12,345", Comma(12345, ""))
|
||||||
assert.Equal("$12,345", Comma(12345, "$"))
|
assert.Equal("$12,345", Comma(12345, "$"))
|
||||||
assert.Equal("¥12,345", Comma(12345, "¥"))
|
assert.Equal("¥12,345", Comma(12345, "¥"))
|
||||||
assert.Equal("12,345.6789", Comma(12345.6789, ""))
|
assert.Equal("12,345.6789", Comma(12345.6789, ""))
|
||||||
assert.Equal("12,345.6789", Comma(+12345.6789, ""))
|
assert.Equal("12,345.6789", Comma(+12345.6789, ""))
|
||||||
// assert.Equal("12,345,678.9", Comma(12345678.9, ""))
|
assert.Equal("12,345,678.9", Comma(12345678.9, ""))
|
||||||
assert.Equal("123,456,789,000", Comma(123456789000, ""))
|
assert.Equal("123,456,789,000", Comma(123456789000, ""))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,21 +218,21 @@ func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AngleToRadian converts angle value to radian value.
|
// AngleToRadian converts angle value to radian value.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/CIvlICqrHql
|
||||||
func AngleToRadian(angle float64) float64 {
|
func AngleToRadian(angle float64) float64 {
|
||||||
radian := angle * (math.Pi / 180)
|
radian := angle * (math.Pi / 180)
|
||||||
return radian
|
return radian
|
||||||
}
|
}
|
||||||
|
|
||||||
// RadianToAngle converts radian value to angle value.
|
// RadianToAngle converts radian value to angle value.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/dQtmOTUOMgi
|
||||||
func RadianToAngle(radian float64) float64 {
|
func RadianToAngle(radian float64) float64 {
|
||||||
angle := radian * (180 / math.Pi)
|
angle := radian * (180 / math.Pi)
|
||||||
return angle
|
return angle
|
||||||
}
|
}
|
||||||
|
|
||||||
// PointDistance get two points distance.
|
// PointDistance get two points distance.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/RrG4JIaziM8
|
||||||
func PointDistance(x1, y1, x2, y2 float64) float64 {
|
func PointDistance(x1, y1, x2, y2 float64) float64 {
|
||||||
a := x1 - x2
|
a := x1 - x2
|
||||||
b := y1 - y2
|
b := y1 - y2
|
||||||
@@ -242,7 +242,7 @@ func PointDistance(x1, y1, x2, y2 float64) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsPrimes checks if number is prime number.
|
// IsPrimes checks if number is prime number.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/Rdd8UTHZJ7u
|
||||||
func IsPrime(n int) bool {
|
func IsPrime(n int) bool {
|
||||||
if n < 2 {
|
if n < 2 {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -492,6 +492,30 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReduceBy produces a value from slice by accumulating the result of each element as passed through the reducer function.
|
||||||
|
// Play: todo
|
||||||
|
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
|
||||||
|
accumulator := initial
|
||||||
|
|
||||||
|
for i, v := range slice {
|
||||||
|
accumulator = reducer(i, v, accumulator)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.
|
||||||
|
// Play: todo
|
||||||
|
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
|
||||||
|
accumulator := initial
|
||||||
|
|
||||||
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
|
accumulator = reducer(i, slice[i], accumulator)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}
|
||||||
|
|
||||||
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
|
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
|
||||||
// Play: https://go.dev/play/p/P5mZp7IhOFo
|
// Play: https://go.dev/play/p/P5mZp7IhOFo
|
||||||
func Replace[T comparable](slice []T, old T, new T, n int) []T {
|
func Replace[T comparable](slice []T, old T, new T, n int) []T {
|
||||||
|
|||||||
@@ -460,6 +460,34 @@ func ExampleReduce() {
|
|||||||
// 6
|
// 6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleReduceBy() {
|
||||||
|
result1 := ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10
|
||||||
|
// 1234
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleReduceRight() {
|
||||||
|
result := ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4321
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleReplace() {
|
func ExampleReplace() {
|
||||||
strs := []string{"a", "b", "c", "a"}
|
strs := []string{"a", "b", "c", "a"}
|
||||||
|
|
||||||
|
|||||||
@@ -405,6 +405,32 @@ func TestReduce(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReduceBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestReduceBy")
|
||||||
|
|
||||||
|
result1 := ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(10, result1)
|
||||||
|
assert.Equal("1234", result2)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReduceRight(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "ReduceRight")
|
||||||
|
|
||||||
|
result := ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal("4321", result)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntSlice(t *testing.T) {
|
func TestIntSlice(t *testing.T) {
|
||||||
var nums []any
|
var nums []any
|
||||||
nums = append(nums, 1, 2, 3)
|
nums = append(nums, 1, 2, 3)
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ func WordCount(s string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoveNonPrintable remove non-printable characters from a string.
|
// RemoveNonPrintable remove non-printable characters from a string.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/og47F5x_jTZ
|
||||||
func RemoveNonPrintable(str string) string {
|
func RemoveNonPrintable(str string) string {
|
||||||
result := strings.Map(func(r rune) rune {
|
result := strings.Map(func(r rune) rune {
|
||||||
if unicode.IsPrint(r) {
|
if unicode.IsPrint(r) {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ func IsAllLower(str string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsASCII checks if string is all ASCII char.
|
// IsASCII checks if string is all ASCII char.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/hfQNPLX0jNa
|
||||||
func IsASCII(str string) bool {
|
func IsASCII(str string) bool {
|
||||||
for i := 0; i < len(str); i++ {
|
for i := 0; i < len(str); i++ {
|
||||||
if str[i] > unicode.MaxASCII {
|
if str[i] > unicode.MaxASCII {
|
||||||
@@ -70,7 +70,7 @@ func IsASCII(str string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsPrintable checks if string is all printable chars.
|
// IsPrintable checks if string is all printable chars.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/Pe1FE2gdtTP
|
||||||
func IsPrintable(str string) bool {
|
func IsPrintable(str string) bool {
|
||||||
for _, r := range str {
|
for _, r := range str {
|
||||||
if !unicode.IsPrint(r) {
|
if !unicode.IsPrint(r) {
|
||||||
@@ -369,3 +369,29 @@ func IsGBK(data []byte) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsNumberStr check if the value is Number(integer, float) or not.
|
||||||
|
// Play: todo
|
||||||
|
func IsNumber(v any) bool {
|
||||||
|
return IsInt(v) || IsFloat(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsFloat check if the value is float(float32, float34) or not.
|
||||||
|
// Play: todo
|
||||||
|
func IsFloat(v any) bool {
|
||||||
|
switch v.(type) {
|
||||||
|
case float32, float64:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsInt check if the value is integer(int, unit) or not.
|
||||||
|
// Play: todo
|
||||||
|
func IsInt(v any) bool {
|
||||||
|
switch v.(type) {
|
||||||
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -449,3 +449,57 @@ func ExampleIsPrintable() {
|
|||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleIsInt() {
|
||||||
|
result1 := IsInt("")
|
||||||
|
result2 := IsInt("3")
|
||||||
|
result3 := IsInt(0.1)
|
||||||
|
result4 := IsInt(0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIsFloat() {
|
||||||
|
result1 := IsFloat("")
|
||||||
|
result2 := IsFloat("3")
|
||||||
|
result3 := IsFloat(0)
|
||||||
|
result4 := IsFloat(0.1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIsNumber() {
|
||||||
|
result1 := IsNumber("")
|
||||||
|
result2 := IsNumber("3")
|
||||||
|
result3 := IsNumber(0)
|
||||||
|
result4 := IsNumber(0.1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|||||||
@@ -100,6 +100,34 @@ func TestIsJSON(t *testing.T) {
|
|||||||
assert.Equal(false, IsJSON("&@#$%^&*"))
|
assert.Equal(false, IsJSON("&@#$%^&*"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsNumber(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsNumber")
|
||||||
|
|
||||||
|
assert.Equal(false, IsNumber(""))
|
||||||
|
assert.Equal(false, IsNumber("3"))
|
||||||
|
assert.Equal(true, IsNumber(0))
|
||||||
|
assert.Equal(true, IsNumber(0.1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsFloat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsFloat")
|
||||||
|
|
||||||
|
assert.Equal(false, IsFloat(""))
|
||||||
|
assert.Equal(false, IsFloat("3"))
|
||||||
|
assert.Equal(false, IsFloat(0))
|
||||||
|
assert.Equal(true, IsFloat(0.1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsInt(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsInt")
|
||||||
|
|
||||||
|
assert.Equal(false, IsInt(""))
|
||||||
|
assert.Equal(false, IsInt("3"))
|
||||||
|
assert.Equal(false, IsInt(0.1))
|
||||||
|
assert.Equal(true, IsInt(0))
|
||||||
|
assert.Equal(true, IsInt(-1))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsNumberStr(t *testing.T) {
|
func TestIsNumberStr(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIsNumberStr")
|
assert := internal.NewAssert(t, "TestIsNumberStr")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user