From 319bc667e79eb101486d31b1e483820cf008def0 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 14 Nov 2022 22:22:56 +0800 Subject: [PATCH] [add] utils.now.*, utils.time.*, utils.str.* --- utils/datetime/now.go | 32 ++++++++++++++++++++++++++++++++ utils/datetime_test.go | 17 +++++++++++++++++ utils/process.go | 14 ++++++++++++-- utils/str/str.go | 31 +++++++++++++++++++++++++++++++ utils/str_test.go | 21 +++++++++++++++++++++ 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 utils/datetime/now.go create mode 100644 utils/datetime_test.go create mode 100644 utils/str/str.go create mode 100644 utils/str_test.go diff --git a/utils/datetime/now.go b/utils/datetime/now.go new file mode 100644 index 00000000..14943978 --- /dev/null +++ b/utils/datetime/now.go @@ -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") +} diff --git a/utils/datetime_test.go b/utils/datetime_test.go new file mode 100644 index 00000000..52d9bbf8 --- /dev/null +++ b/utils/datetime_test.go @@ -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()) +} diff --git a/utils/process.go b/utils/process.go index 96acec4c..bc068ec2 100644 --- a/utils/process.go +++ b/utils/process.go @@ -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) } diff --git a/utils/str/str.go b/utils/str/str.go new file mode 100644 index 00000000..04459d84 --- /dev/null +++ b/utils/str/str.go @@ -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...) +} diff --git a/utils/str_test.go b/utils/str_test.go new file mode 100644 index 00000000..8c069289 --- /dev/null +++ b/utils/str_test.go @@ -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) +}