55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"syscall/js"
|
|
|
|
promises "codeberg.org/danjones000/go-promises"
|
|
"codeberg.org/danjones000/go-promises/internal"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("Some go")
|
|
|
|
js.Global().Set("add", promises.PromisifyGoFunc(func(_ js.Value, args []js.Value) (any, error) {
|
|
if len(args) < 2 {
|
|
return nil, errors.New("add called with too few arguments")
|
|
}
|
|
a := args[0]
|
|
b := args[1]
|
|
|
|
if a.Type() != js.TypeNumber {
|
|
return nil, fmt.Errorf("First argument must be a number. %s given", a.Type())
|
|
}
|
|
|
|
if b.Type() != js.TypeNumber {
|
|
return nil, fmt.Errorf("secong argument must be a number. %s given", b.Type())
|
|
}
|
|
|
|
return internal.Add(a.Int(), b.Int()), nil
|
|
}))
|
|
|
|
js.Global().Set("getUser", promises.PromisifyGoFunc(func(this js.Value, args []js.Value) (any, error) {
|
|
if len(args) < 2 {
|
|
return nil, errors.New("getUser called with too few arguments")
|
|
}
|
|
name := args[0]
|
|
age := args[1]
|
|
|
|
if name.Type() != js.TypeString {
|
|
return nil, fmt.Errorf("First argument must be a string. %s given", name.Type())
|
|
}
|
|
if age.Type() != js.TypeNumber {
|
|
return nil, fmt.Errorf("Second argument must be a string. %s given", age.Type())
|
|
}
|
|
return internal.GetUser(name.String(), age.Int()), nil
|
|
}))
|
|
|
|
js.Global().Get("String").Get("prototype").Set("toTitleCase", js.FuncOf(func(this js.Value, _ []js.Value) any {
|
|
return strings.Title(this.String())
|
|
}))
|
|
|
|
<-make(chan struct{})
|
|
}
|