add filepath support

This commit is contained in:
Star 2024-10-12 13:45:51 +08:00
parent 27f0e1718b
commit cc8fce2708
3 changed files with 100 additions and 33 deletions

View File

@ -24,38 +24,47 @@ function main(args){
## module.exports
```ts
function json(data:any): string {return ''}
function jsonP(data:any): string {return ''}
function unJson(data:string): any {return null}
function yaml(data:any): string {return ''}
function unYaml(data:string): any {return null}
function load(filename:string): any {return null}
function save(filename:string, data:any) {}
function base64(data:any): string {return ''}
function unBase64(data:string): any {return null}
function urlBase64(data:any): string {return ''}
function unUrlBase64(data:string): any {return null}
function hex(data:any): string {return ''}
function unHex(data:string): any {return null}
function aes(data:any, key:string, iv:string): string {return ''}
function unAes(data:string, key:string, iv:string): any {return null}
function gzip(data:any): string {return ''}
function gunzip(data:string): any {return null}
function id(): string {return ''}
function uniqueId(): string {return ''}
function token(size:number): string {return ''}
function md5(data:any): string {return ''}
function sha1(data:any): string {return ''}
function sha256(data:any): string {return ''}
function sha512(data:any): string {return ''}
function tpl(text:string, data:any, functions?:Object): string {return ''}
function sleep(ms:number): void {}
function setTimeout(callback:()=>void, ms?:number, ...args:any): void {}
function shell(cmd:string, ...args:string[]): string[] {return []}
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 json(data:any)
function jsonP(data:any)
function unJson(data:string)
function yaml(data:any)
function unYaml(data:string)
function load(filename:string)
function save(filename:string, data:any)
function base64(data:any)
function unBase64(data:string)
function urlBase64(data:any)
function unUrlBase64(data:string)
function hex(data:any)
function unHex(data:string)
function aes(data:any, key:string, iv:string)
function unAes(data:string, key:string, iv:string)
function gzip(data:any)
function gunzip(data:string)
function id()
function uniqueId()
function token(size:number)
function md5(data:any)
function sha1(data:any)
function sha256(data:any)
function sha512(data:any)
function tpl(text:string, data:any, functions?:Object)
function sleep(ms:number)
function setTimeout(callback:()=>void, ms?:number, ...args:any)
function shell(cmd:string, ...args:string[])
function toDatetime(timestamp:number)
function fromDatetime(datetimeStr:string)
function toDate(timestamp:number)
function fromDate(dateStr:string)
function os()
function arch()
function joinPath(...paths:string[])
function getPathDir(path:string)
function getPathBase(path:string)
function getPathVolume(path:string)
function absPath(path:string)
function cleanPath(path:string)
function isLocalPath(path:string)
```
## full api see [util.ts](https://apigo.cc/gojs/util/util.ts)

40
util.go
View File

@ -5,6 +5,8 @@ import (
_ "embed"
"encoding/hex"
"encoding/json"
"path/filepath"
"runtime"
"strings"
"text/template"
"time"
@ -298,6 +300,44 @@ func init() {
panic(vm.NewGoError(err))
}
},
"os": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(runtime.GOOS)
},
"arch": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(runtime.GOARCH)
},
"joinPath": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(filepath.Join(args.StrArray(0)...))
},
"getPathDir": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(filepath.Dir(args.Str(0)))
},
"getPathBase": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(filepath.Base(args.Str(0)))
},
"getPathVolume": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(filepath.VolumeName(args.Str(0)))
},
"absPath": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
r, err := filepath.Abs(args.Str(0))
if err != nil {
panic(vm.NewGoError(err))
}
return vm.ToValue(r)
},
"cleanPath": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(filepath.Clean(args.Str(0)))
},
"isLocalPath": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
return vm.ToValue(filepath.IsLocal(args.Str(0)))
},
}
gojs.Register("apigo.cc/gojs/util", gojs.Module{

20
util.ts
View File

@ -31,7 +31,16 @@ export default {
toDatetime,
fromDatetime,
toDate,
fromDate
fromDate,
os,
arch,
joinPath,
getPathDir,
getPathBase,
getPathVolume,
absPath,
cleanPath,
isLocalPath
}
function json(data:any): string {return ''}
@ -66,3 +75,12 @@ 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 os(): string {return ''}
function arch(): string {return ''}
function joinPath(...paths:string[]): string {return ''}
function getPathDir(path:string): string {return ''}
function getPathBase(path:string): string {return ''}
function getPathVolume(path:string): string {return ''}
function absPath(path:string): string {return ''}
function cleanPath(path:string): string {return ''}
function isLocalPath(path:string): boolean {return false}