diff --git a/internal/complete/complete.go b/internal/complete/complete.go index 2e6e128..abab15d 100644 --- a/internal/complete/complete.go +++ b/internal/complete/complete.go @@ -53,6 +53,7 @@ var globals = []string{ "keys", "values", "skip", + "list", } //go:embed prelude.js diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 60e4b2f..86b338d 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -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()) diff --git a/internal/engine/prelude.js b/internal/engine/prelude.js index b1fb47e..60c3cb9 100644 --- a/internal/engine/prelude.js +++ b/internal/engine/prelude.js @@ -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(' ')) }, diff --git a/internal/engine/stdlib.js b/internal/engine/stdlib.js index 1a95e0e..0d537bc 100644 --- a/internal/engine/stdlib.js +++ b/internal/engine/stdlib.js @@ -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}`) +} diff --git a/npm/index.js b/npm/index.js index 6543114..76e0b60 100644 --- a/npm/index.js +++ b/npm/index.js @@ -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) { diff --git a/reduce.go b/reduce.go index 0a5b4d2..1e33a2b 100644 --- a/reduce.go +++ b/reduce.go @@ -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") diff --git a/version.go b/version.go index 5b3a023..07d0e77 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package main -const version = "33.0.0" +const version = "34.0.0"