Adding upstream version 0.0~git20250409.f7acab6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
b9b5d88025
commit
21b930d007
51 changed files with 11229 additions and 0 deletions
104
util/module.go
Normal file
104
util/module.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/require"
|
||||
)
|
||||
|
||||
const ModuleName = "util"
|
||||
|
||||
type Util struct {
|
||||
runtime *goja.Runtime
|
||||
}
|
||||
|
||||
func (u *Util) format(f rune, val goja.Value, w *bytes.Buffer) bool {
|
||||
switch f {
|
||||
case 's':
|
||||
w.WriteString(val.String())
|
||||
case 'd':
|
||||
w.WriteString(val.ToNumber().String())
|
||||
case 'j':
|
||||
if json, ok := u.runtime.Get("JSON").(*goja.Object); ok {
|
||||
if stringify, ok := goja.AssertFunction(json.Get("stringify")); ok {
|
||||
res, err := stringify(json, val)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
w.WriteString(res.String())
|
||||
}
|
||||
}
|
||||
case '%':
|
||||
w.WriteByte('%')
|
||||
return false
|
||||
default:
|
||||
w.WriteByte('%')
|
||||
w.WriteRune(f)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (u *Util) Format(b *bytes.Buffer, f string, args ...goja.Value) {
|
||||
pct := false
|
||||
argNum := 0
|
||||
for _, chr := range f {
|
||||
if pct {
|
||||
if argNum < len(args) {
|
||||
if u.format(chr, args[argNum], b) {
|
||||
argNum++
|
||||
}
|
||||
} else {
|
||||
b.WriteByte('%')
|
||||
b.WriteRune(chr)
|
||||
}
|
||||
pct = false
|
||||
} else {
|
||||
if chr == '%' {
|
||||
pct = true
|
||||
} else {
|
||||
b.WriteRune(chr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, arg := range args[argNum:] {
|
||||
b.WriteByte(' ')
|
||||
b.WriteString(arg.String())
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Util) js_format(call goja.FunctionCall) goja.Value {
|
||||
var b bytes.Buffer
|
||||
var fmt string
|
||||
|
||||
if arg := call.Argument(0); !goja.IsUndefined(arg) {
|
||||
fmt = arg.String()
|
||||
}
|
||||
|
||||
var args []goja.Value
|
||||
if len(call.Arguments) > 0 {
|
||||
args = call.Arguments[1:]
|
||||
}
|
||||
u.Format(&b, fmt, args...)
|
||||
|
||||
return u.runtime.ToValue(b.String())
|
||||
}
|
||||
|
||||
func Require(runtime *goja.Runtime, module *goja.Object) {
|
||||
u := &Util{
|
||||
runtime: runtime,
|
||||
}
|
||||
obj := module.Get("exports").(*goja.Object)
|
||||
obj.Set("format", u.js_format)
|
||||
}
|
||||
|
||||
func New(runtime *goja.Runtime) *Util {
|
||||
return &Util{
|
||||
runtime: runtime,
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
require.RegisterCoreModule(ModuleName, Require)
|
||||
}
|
74
util/module_test.go
Normal file
74
util/module_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/require"
|
||||
)
|
||||
|
||||
func TestUtil_Format(t *testing.T) {
|
||||
vm := goja.New()
|
||||
util := New(vm)
|
||||
|
||||
var b bytes.Buffer
|
||||
util.Format(&b, "Test: %% %д %s %d, %j", vm.ToValue("string"), vm.ToValue(42), vm.NewObject())
|
||||
|
||||
if res := b.String(); res != "Test: % %д string 42, {}" {
|
||||
t.Fatalf("Unexpected result: '%s'", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUtil_Format_NoArgs(t *testing.T) {
|
||||
vm := goja.New()
|
||||
util := New(vm)
|
||||
|
||||
var b bytes.Buffer
|
||||
util.Format(&b, "Test: %s %d, %j")
|
||||
|
||||
if res := b.String(); res != "Test: %s %d, %j" {
|
||||
t.Fatalf("Unexpected result: '%s'", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUtil_Format_LessArgs(t *testing.T) {
|
||||
vm := goja.New()
|
||||
util := New(vm)
|
||||
|
||||
var b bytes.Buffer
|
||||
util.Format(&b, "Test: %s %d, %j", vm.ToValue("string"), vm.ToValue(42))
|
||||
|
||||
if res := b.String(); res != "Test: string 42, %j" {
|
||||
t.Fatalf("Unexpected result: '%s'", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUtil_Format_MoreArgs(t *testing.T) {
|
||||
vm := goja.New()
|
||||
util := New(vm)
|
||||
|
||||
var b bytes.Buffer
|
||||
util.Format(&b, "Test: %s %d, %j", vm.ToValue("string"), vm.ToValue(42), vm.NewObject(), vm.ToValue(42.42))
|
||||
|
||||
if res := b.String(); res != "Test: string 42, {} 42.42" {
|
||||
t.Fatalf("Unexpected result: '%s'", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSNoArgs(t *testing.T) {
|
||||
vm := goja.New()
|
||||
new(require.Registry).Enable(vm)
|
||||
|
||||
if util, ok := require.Require(vm, ModuleName).(*goja.Object); ok {
|
||||
if format, ok := goja.AssertFunction(util.Get("format")); ok {
|
||||
res, err := format(util)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := res.Export(); v != "" {
|
||||
t.Fatalf("Unexpected result: %v", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue