fx 30.2.0 → 31.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +26 -23
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -48,9 +48,14 @@ const skip = Symbol('skip')
48
48
 
49
49
  async function runTransforms(json, args, theme) {
50
50
  const process = await import('node:process')
51
- let i, code, output = json
51
+ let i, code, jsCode, output = json
52
52
  for ([i, code] of args.entries()) try {
53
- output = await transform(output, code)
53
+ jsCode = transpile(code)
54
+ const fn = `(function () {
55
+ const x = this
56
+ return ${jsCode}
57
+ })`
58
+ output = await run(output, fn)
54
59
  } catch (err) {
55
60
  printErr(err)
56
61
  process.exit(1)
@@ -73,52 +78,50 @@ async function runTransforms(json, args, theme) {
73
78
  console.error(
74
79
  `\n ${pre} ${code} ${post}\n` +
75
80
  ` ${' '.repeat(pre.length + 1)}${'^'.repeat(code.length)}\n` +
81
+ (jsCode !== code ? `\n${jsCode}\n` : ``) +
76
82
  `\n${err.stack || err}`,
77
83
  )
78
84
  }
79
85
  }
80
86
 
81
- async function transform(json, code) {
87
+ function transpile(code) {
82
88
  if ('.' === code)
83
- return json
89
+ return 'x'
84
90
 
85
91
  if (/^(\.\w*)+\[]/.test(code))
86
- return eval(`(function () {
87
- return (${fold(code.split('[]'))})(this)
88
- })`).call(json)
92
+ return `(${fold(code.split('[]'))})(x)`
89
93
 
90
94
  function fold(s) {
91
95
  if (s.length === 1)
92
96
  return 'x => x' + s[0]
93
97
  let obj = s.shift()
94
98
  obj = obj === '.' ? 'x' : 'x' + obj
95
- return `x => Object.values(${obj}).flatMap(${fold(s)})`
99
+ return `x => ${obj}.flatMap(${fold(s)})`
96
100
  }
97
101
 
98
102
  if (/^\.\[/.test(code))
99
- return eval(`(function () {
100
- const x = this
101
- return this${code.substring(1)}
102
- })`).call(json)
103
+ return `x${code.substring(1)}`
103
104
 
104
105
  if (/^\./.test(code))
105
- return eval(`(function () {
106
- const x = this
107
- return this${code}
108
- })`).call(json)
106
+ return `x${code}`
109
107
 
108
+ // deprecated
110
109
  if (/^map\(.+?\)$/i.test(code)) {
111
110
  let s = code.substring(4, code.length - 1)
112
111
  if (s[0] === '.') s = 'x' + s
113
- return eval(`(function () {
114
- return this.map((x, i) => apply(${s}, x, i))
115
- })`).call(json)
112
+ return `x.map((x, i) => apply(${s}, x, i))`
116
113
  }
117
114
 
118
- const fn = eval(`(function () {
119
- const x = this
120
- return ${code}
121
- })`).call(json)
115
+ if (/^@/.test(code)) {
116
+ const jsCode = transpile(code.substring(1))
117
+ return `x.map((x, i) => apply(${jsCode}, x, i))`
118
+ }
119
+
120
+ return code
121
+ }
122
+
123
+ async function run(json, code) {
124
+ const fn = eval(code).call(json)
122
125
 
123
126
  return apply(fn, json)
124
127
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fx",
3
- "version": "30.2.0",
3
+ "version": "31.0.0",
4
4
  "bin": {
5
5
  "fx": "index.js"
6
6
  },