[add] utils.now.*, utils.time.*, utils.str.*
This commit is contained in:
parent
0c1f74fa49
commit
319bc667e7
5 changed files with 113 additions and 2 deletions
32
utils/datetime/now.go
Normal file
32
utils/datetime/now.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package datetime
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
// ProcessTimestamp utils.now.Timestamp
|
||||
func ProcessTimestamp(process *gou.Process) interface{} {
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
// ProcessTimestampms utils.now.Timestampms
|
||||
func ProcessTimestampms(process *gou.Process) interface{} {
|
||||
return time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// ProcessDate utils.now.Date
|
||||
func ProcessDate(process *gou.Process) interface{} {
|
||||
return time.Now().Local().Format("2006-01-02")
|
||||
}
|
||||
|
||||
// ProcessTime utils.now.Time
|
||||
func ProcessTime(process *gou.Process) interface{} {
|
||||
return time.Now().Local().Format("15:04:05")
|
||||
}
|
||||
|
||||
// ProcessDateTime utils.now.DateTime
|
||||
func ProcessDateTime(process *gou.Process) interface{} {
|
||||
return time.Now().Local().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
17
utils/datetime_test.go
Normal file
17
utils/datetime_test.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
func TestProcessNow(t *testing.T) {
|
||||
assert.LessOrEqual(t, time.Now().Unix(), gou.NewProcess("utils.now.Timestamp").Run().(int64))
|
||||
assert.LessOrEqual(t, time.Now().UnixMilli(), gou.NewProcess("utils.now.Timestampms").Run().(int64))
|
||||
assert.NotNil(t, gou.NewProcess("utils.now.Date").Run())
|
||||
assert.NotNil(t, gou.NewProcess("utils.now.Time").Run())
|
||||
assert.NotNil(t, gou.NewProcess("utils.now.DateTime").Run())
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
package utils
|
||||
|
||||
import "github.com/yaoapp/gou"
|
||||
import (
|
||||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/yao/utils/datetime"
|
||||
"github.com/yaoapp/yao/utils/str"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gou.AliasProcess("xiang.helper.Captcha", "yao.utils.Captcha") // deprecated
|
||||
|
|
@ -45,6 +49,8 @@ func init() {
|
|||
|
||||
// String
|
||||
gou.AliasProcess("xiang.helper.StrConcat", "utils.str.Concat")
|
||||
gou.RegisterProcessHandler("utils.str.Join", str.ProcessJoin)
|
||||
gou.RegisterProcessHandler("utils.str.JoinPath", str.ProcessJoinPath)
|
||||
|
||||
// Array
|
||||
gou.AliasProcess("xiang.helper.ArrayPluck", "utils.arr.Pluck")
|
||||
|
|
@ -68,5 +74,9 @@ func init() {
|
|||
|
||||
// Time
|
||||
gou.AliasProcess("xiang.flow.Sleep", "utils.time.Sleep")
|
||||
|
||||
gou.RegisterProcessHandler("utils.now.Time", datetime.ProcessTime)
|
||||
gou.RegisterProcessHandler("utils.now.Date", datetime.ProcessDate)
|
||||
gou.RegisterProcessHandler("utils.now.DateTime", datetime.ProcessDateTime)
|
||||
gou.RegisterProcessHandler("utils.now.Timestamp", datetime.ProcessTimestamp)
|
||||
gou.RegisterProcessHandler("utils.now.Timestampms", datetime.ProcessTimestampms)
|
||||
}
|
||||
|
|
|
|||
31
utils/str/str.go
Normal file
31
utils/str/str.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package str
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
// ProcessJoin utils.str.Join
|
||||
func ProcessJoin(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
args := process.ArgsArray(0)
|
||||
sep := process.ArgsString(1)
|
||||
strs := []string{}
|
||||
for i := range args {
|
||||
strs = append(strs, fmt.Sprintf("%v", args[i]))
|
||||
}
|
||||
return strings.Join(strs, sep)
|
||||
}
|
||||
|
||||
// ProcessJoinPath utils.str.JoinPath
|
||||
func ProcessJoinPath(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
paths := []string{}
|
||||
for _, arg := range process.Args {
|
||||
paths = append(paths, fmt.Sprintf("%v", arg))
|
||||
}
|
||||
return filepath.Join(paths...)
|
||||
}
|
||||
21
utils/str_test.go
Normal file
21
utils/str_test.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
func TestProcessStrJoin(t *testing.T) {
|
||||
res := gou.NewProcess("utils.str.Join", []interface{}{"FOO", 20, "BAR"}, ",").Run().(string)
|
||||
assert.Equal(t, "FOO,20,BAR", res)
|
||||
}
|
||||
|
||||
func TestProcessStrJoinPath(t *testing.T) {
|
||||
res := gou.NewProcess("utils.str.JoinPath", "data", 20, "app").Run().(string)
|
||||
shouldBe := fmt.Sprintf("data%s20%sapp", string(os.PathSeparator), string(os.PathSeparator))
|
||||
assert.Equal(t, shouldBe, res)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue