Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f1528bb80c | ||
b184e96f64 |
6
go.mod
6
go.mod
@ -1,9 +1,9 @@
|
||||
module apigo.cc/gojs/util
|
||||
|
||||
go 1.23.0
|
||||
go 1.24
|
||||
|
||||
require (
|
||||
apigo.cc/gojs v0.0.21
|
||||
apigo.cc/gojs v0.0.25
|
||||
apigo.cc/gojs/console v0.0.2
|
||||
github.com/ZZMarquis/gm v1.3.2
|
||||
github.com/emmansun/gmsm v0.30.1
|
||||
@ -18,7 +18,7 @@ require (
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||
github.com/google/pprof v0.0.0-20250630185457-6e76a2b096b5 // indirect
|
||||
github.com/ssgo/config v1.7.9 // indirect
|
||||
github.com/ssgo/log v1.7.7 // indirect
|
||||
github.com/ssgo/log v1.7.9 // indirect
|
||||
github.com/ssgo/standard v1.7.7 // indirect
|
||||
github.com/ssgo/tool v0.4.29 // indirect
|
||||
golang.org/x/crypto v0.40.0 // indirect
|
||||
|
4
util.go
4
util.go
@ -838,6 +838,10 @@ func init() {
|
||||
TsCode: utilTS,
|
||||
Example: utilMD,
|
||||
JsCode: `
|
||||
$MOD$.parseDate = function (v) {
|
||||
return new Date($MOD$.timestampMS(v))
|
||||
}
|
||||
|
||||
$MOD$.keysBy = function (obj, ...fieldAndValues) {
|
||||
let keys = []
|
||||
for (let k in obj) {
|
||||
|
18
util.ts
18
util.ts
@ -59,12 +59,12 @@ export default {
|
||||
hmacSM3,
|
||||
tpl,
|
||||
formatDate,
|
||||
addDate,
|
||||
timestampMS,
|
||||
timestamp,
|
||||
toDatetime,
|
||||
fromDatetime,
|
||||
toDate,
|
||||
fromDate,
|
||||
parseDate,
|
||||
joinPath,
|
||||
getPathDir,
|
||||
getPathBase,
|
||||
@ -163,13 +163,13 @@ function hmacSHA256(key: any, ...data: any[]): any { return '' }
|
||||
function hmacSHA512(key: any, ...data: any[]): any { return '' }
|
||||
function hmacSM3(key: any, ...data: any[]): any { return '' }
|
||||
function tpl(text: string, data: any, functions?: Object): string { return '' }
|
||||
function formatDate(format: string, timestamp?: number): string { return '' }
|
||||
function timestampMS(): number { return 0 }
|
||||
function timestamp(): number { return 0 }
|
||||
function toDatetime(timestamp?: number): string { return '' }
|
||||
function fromDatetime(datetimeStr: string): number { return 0 }
|
||||
function toDate(timestamp?: number): string { return '' }
|
||||
function fromDate(dateStr: string): number { return 0 }
|
||||
function formatDate(format: string, value?: any): string { return '' }
|
||||
function addDate(add: string, value?: any): string { return '' }
|
||||
function timestampMS(value?: any): number { return 0 }
|
||||
function timestamp(value?: any): number { return 0 }
|
||||
function toDatetime(value?: any): string { return '' }
|
||||
function toDate(value?: any): string { return '' }
|
||||
function parseDate(value: any): Date { return new Date() }
|
||||
function joinPath(...paths: string[]): string { return '' }
|
||||
function getPathDir(path: string): string { return '' }
|
||||
function getPathBase(path: string): string { return '' }
|
||||
|
14
util_test.go
14
util_test.go
@ -37,10 +37,6 @@ func TestHash(t *testing.T) {
|
||||
sm4R, _ := vm.RunCode("util.hex(util.sm4('hello 123','12345678901234567','12345678901234567'))")
|
||||
testIsSame(vm, t, "util.unSM4('"+u.String(sm4R)+"','12345678901234567','12345678901234567')", "hello 123")
|
||||
testIsSame(vm, t, "util.gzip('hello 123')", u.GzipN([]byte("hello 123")))
|
||||
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-01 00:00:00", time.Local)
|
||||
testIsSame(vm, t, "util.timestampMS('2024-01-01 00:00:00')", tm.UnixMilli())
|
||||
testIsSame(vm, t, "util.timestampMS('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')", tm.UnixMilli())
|
||||
testIsSame(vm, t, "util.timestampMS(new Date("+u.String(tm.UnixMilli())+"))", tm.UnixMilli())
|
||||
}
|
||||
|
||||
func TestECDSA(t *testing.T) {
|
||||
@ -127,6 +123,16 @@ func TestRSA(t *testing.T) {
|
||||
fmt.Println(u.Green("ecdsa test passed"))
|
||||
}
|
||||
|
||||
func TestDate(t *testing.T) {
|
||||
vm := gojs.New()
|
||||
vm.RunCode("import util from 'apigo.cc/gojs/util'\nimport co from 'apigo.cc/gojs/console'")
|
||||
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-01 00:00:00", time.Local)
|
||||
testIsSame(vm, t, "util.timestampMS('2024-01-01 00:00:00')", tm.UnixMilli())
|
||||
testIsSame(vm, t, "util.timestampMS('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')", tm.UnixMilli())
|
||||
testIsSame(vm, t, "util.timestampMS(new Date("+u.String(tm.UnixMilli())+"))", tm.UnixMilli())
|
||||
testIsSame(vm, t, "util.parseDate(new Date('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')).getTime()", tm.UnixMilli())
|
||||
}
|
||||
|
||||
func testIsSame(vm *gojs.Runtime, t *testing.T, code string, checkValue any) {
|
||||
r, err := vm.RunCode(code)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user