diff --git a/go.mod b/go.mod index eed65bf..19b37c7 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module apigo.cc/gojs/util go 1.18 require ( - apigo.cc/gojs v0.0.4 + apigo.cc/gojs v0.0.7 github.com/ZZMarquis/gm v1.3.2 - github.com/emmansun/gmsm v0.29.2 + github.com/emmansun/gmsm v0.29.3 github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9 github.com/ssgo/u v1.7.11 gopkg.in/yaml.v3 v3.0.1 diff --git a/util.go b/util.go index a5ec94c..308e7c7 100644 --- a/util.go +++ b/util.go @@ -604,16 +604,75 @@ func init() { panic(vm.NewGoError(err)) } }, - "id": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { - return vm.ToValue(u.Id12()) - }, "uniqueId": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { - return vm.ToValue(u.UniqueId()) + args := gojs.MakeArgs(&argsIn, vm) + size := args.Int(0) + var id string + if size >= 20 { + id = u.UniqueId() + } else if size >= 16 { + id = u.UniqueId()[0:16] + } else if size >= 14 { + id = u.ShortUniqueId() + } else if size >= 12 { + id = u.ShortUniqueId()[0:12] + } else { + id = u.UniqueId() + } + return vm.ToValue(id) + }, + "uniqueIdL": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { + args := gojs.MakeArgs(&argsIn, vm) + size := args.Int(0) + var id string + if size >= 20 { + id = u.UniqueId() + } else if size >= 16 { + id = u.UniqueId()[0:16] + } else if size >= 14 { + id = u.ShortUniqueId() + } else if size >= 12 { + id = u.ShortUniqueId()[0:12] + } else { + id = u.UniqueId() + } + return vm.ToValue(strings.ToLower(id)) + }, + "id": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { + args := gojs.MakeArgs(&argsIn, vm) + size := args.Int(0) + var id string + if size >= 12 { + id = u.Id12() + } else if size >= 10 { + id = u.Id10() + } else if size >= 8 { + id = u.Id8() + } else { + id = u.Id6() + } + return vm.ToValue(id) + }, + "idL": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { + args := gojs.MakeArgs(&argsIn, vm) + size := args.Int(0) + var id string + if size >= 12 { + id = u.Id12() + } else if size >= 10 { + id = u.Id10() + } else if size >= 8 { + id = u.Id8() + } else { + id = u.Id6() + } + return vm.ToValue(strings.ToLower(id)) }, "token": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { - size := 20 - if len(argsIn.Arguments) > 0 { - size = u.Int(argsIn.Arguments[0].Export()) + args := gojs.MakeArgs(&argsIn, vm) + size := args.Int(0) + if size == 0 { + size = 20 } return vm.ToValue(u.MakeToken(size)) }, diff --git a/util.ts b/util.ts index d68f4ec..5be590a 100644 --- a/util.ts +++ b/util.ts @@ -45,7 +45,9 @@ export default { gzip, gunzip, id, + idL, uniqueId, + uniqueIdL, token, md5, sha1, @@ -127,8 +129,10 @@ function encryptRSA(data: any, pubKey: any): any { return '' } function decryptRSA(data: any, priKey: any): any { return '' } function gzip(data: any): string { return '' } function gunzip(data: string): any { return null } -function id(): string { return '' } -function uniqueId(): string { return '' } +function id(size?: number): string { return '' } +function idL(size?: number): string { return '' } +function uniqueId(size?: number): string { return '' } +function uniqueIdL(size?: number): string { return '' } function token(size: number): any { return '' } function md5(...data: any[]): any { return '' } function sha1(...data: any[]): any { return '' }