Add list builtin

pull/304/head
Anton Medvedev 1 month ago
parent c9a336fe18
commit bf8fe0a9ef
No known key found for this signature in database

@ -53,6 +53,7 @@ var globals = []string{
"keys",
"values",
"skip",
"list",
}
//go:embed prelude.js

@ -91,7 +91,7 @@ func Reduce(args []string) {
for _, fn := range fns {
code.WriteString(Transform(fn))
}
code.WriteString("JSON.stringify(json)")
code.WriteString(`json === skip ? '__skip__' : JSON.stringify(json)`)
vm := goja.New()
vm.Set("println", func(s string) any {
@ -111,6 +111,10 @@ func Reduce(args []string) {
return
}
if output == "__skip__" {
return
}
node, err := jsonx.Parse([]byte(output))
if err != nil {
println(err.Error())

@ -2,7 +2,13 @@ const console = {
log: function (...args) {
const parts = []
for (const arg of args) {
parts.push(typeof arg === 'string' ? arg : JSON.stringify(arg, null, 2))
if (typeof arg === 'undefined') {
parts.push('undefined')
} else if (typeof arg === 'string') {
parts.push(arg)
} else {
parts.push(JSON.stringify(arg, null, 2))
}
}
println(parts.join(' '))
},

@ -1,3 +1,5 @@
const skip = Symbol('skip')
function apply(fn, ...args) {
if (typeof fn === 'function') return fn(...args)
return fn
@ -89,3 +91,11 @@ function values(x) {
if (typeof x === 'object' && x !== null) return Object.values(x)
throw new Error(`Cannot get values of ${typeof x}`)
}
function list(x) {
if (Array.isArray(x)) {
for (const y of x) console.log(y)
return skip
}
throw new Error(`Cannot list ${typeof x}`)
}

@ -233,6 +233,14 @@ async function run(json, code) {
if (typeof x === 'object' && x !== null) return Object.values(x)
throw new Error(`Cannot get values of ${typeof x}`)
}
function list(x) {
if (Array.isArray(x)) {
for (const y of x) console.log(y)
return skip
}
throw new Error(`Cannot list ${typeof x}`)
}
}
async function read(fd = 0) {

@ -14,8 +14,12 @@ import (
var src []byte
func reduce(fns []string) {
var deno bool
if _, ok := os.LookupEnv("FX_JS"); ok {
engine.Reduce(fns)
return
}
var deno bool
bin, err := exec.LookPath("node")
if err != nil {
bin, err = exec.LookPath("deno")

@ -1,3 +1,3 @@
package main
const version = "33.0.0"
const version = "34.0.0"

Loading…
Cancel
Save