fx 30.1.0 → 30.2.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.
- package/index.js +69 -30
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -82,25 +82,32 @@ async function transform(json, code) {
|
|
|
82
82
|
if ('.' === code)
|
|
83
83
|
return json
|
|
84
84
|
|
|
85
|
-
if ('?' === code)
|
|
86
|
-
return Object.keys(json)
|
|
87
|
-
|
|
88
85
|
if (/^(\.\w*)+\[]/.test(code))
|
|
89
86
|
return eval(`(function () {
|
|
90
87
|
return (${fold(code.split('[]'))})(this)
|
|
91
88
|
})`).call(json)
|
|
92
89
|
|
|
90
|
+
function fold(s) {
|
|
91
|
+
if (s.length === 1)
|
|
92
|
+
return 'x => x' + s[0]
|
|
93
|
+
let obj = s.shift()
|
|
94
|
+
obj = obj === '.' ? 'x' : 'x' + obj
|
|
95
|
+
return `x => Object.values(${obj}).flatMap(${fold(s)})`
|
|
96
|
+
}
|
|
97
|
+
|
|
93
98
|
if (/^\.\[/.test(code))
|
|
94
99
|
return eval(`(function () {
|
|
100
|
+
const x = this
|
|
95
101
|
return this${code.substring(1)}
|
|
96
102
|
})`).call(json)
|
|
97
103
|
|
|
98
104
|
if (/^\./.test(code))
|
|
99
105
|
return eval(`(function () {
|
|
106
|
+
const x = this
|
|
100
107
|
return this${code}
|
|
101
108
|
})`).call(json)
|
|
102
109
|
|
|
103
|
-
if (/^map\(.+?\)
|
|
110
|
+
if (/^map\(.+?\)$/i.test(code)) {
|
|
104
111
|
let s = code.substring(4, code.length - 1)
|
|
105
112
|
if (s[0] === '.') s = 'x' + s
|
|
106
113
|
return eval(`(function () {
|
|
@@ -120,35 +127,47 @@ async function transform(json, code) {
|
|
|
120
127
|
return fn
|
|
121
128
|
}
|
|
122
129
|
|
|
123
|
-
function
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return `x => Object.values(${obj}).flatMap(${fold(s)})`
|
|
130
|
+
function len(x) {
|
|
131
|
+
if (Array.isArray(x)) return x.length
|
|
132
|
+
if (typeof x === 'string') return x.length
|
|
133
|
+
if (typeof x === 'object' && x !== null) return Object.keys(x).length
|
|
134
|
+
throw new Error(`Cannot get length of ${typeof x}`)
|
|
129
135
|
}
|
|
130
136
|
|
|
131
|
-
function
|
|
132
|
-
return
|
|
137
|
+
function uniq(x) {
|
|
138
|
+
if (Array.isArray(x)) return [...new Set(x)]
|
|
139
|
+
throw new Error(`Cannot get unique values of ${typeof x}`)
|
|
133
140
|
}
|
|
134
141
|
|
|
135
|
-
function
|
|
136
|
-
return
|
|
142
|
+
function sort(x) {
|
|
143
|
+
if (Array.isArray(x)) return x.sort()
|
|
144
|
+
throw new Error(`Cannot sort ${typeof x}`)
|
|
137
145
|
}
|
|
138
146
|
|
|
139
|
-
function
|
|
140
|
-
return
|
|
147
|
+
function map(fn) {
|
|
148
|
+
return function (x) {
|
|
149
|
+
if (Array.isArray(x)) return x.map((v, i) => fn(v, i))
|
|
150
|
+
throw new Error(`Cannot map ${typeof x}`)
|
|
151
|
+
}
|
|
141
152
|
}
|
|
142
153
|
|
|
143
|
-
function
|
|
144
|
-
return
|
|
154
|
+
function sortBy(fn) {
|
|
155
|
+
return function (x) {
|
|
156
|
+
if (Array.isArray(x)) return x.sort((a, b) => {
|
|
157
|
+
const fa = fn(a)
|
|
158
|
+
const fb = fn(b)
|
|
159
|
+
return fa < fb ? -1 : fa > fb ? 1 : 0
|
|
160
|
+
})
|
|
161
|
+
throw new Error(`Cannot sort ${typeof x}`)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function groupBy(keyFn) {
|
|
166
|
+
return function (x) {
|
|
145
167
|
const grouped = {}
|
|
146
|
-
for (const item of
|
|
147
|
-
const key = typeof
|
|
148
|
-
|
|
149
|
-
: item[keyOrFunction]
|
|
150
|
-
if (!grouped.hasOwnProperty(key))
|
|
151
|
-
grouped[key] = []
|
|
168
|
+
for (const item of x) {
|
|
169
|
+
const key = typeof keyFn === 'function' ? keyFn(item) : item[keyFn]
|
|
170
|
+
if (!grouped.hasOwnProperty(key)) grouped[key] = []
|
|
152
171
|
grouped[key].push(item)
|
|
153
172
|
}
|
|
154
173
|
return grouped
|
|
@@ -156,24 +175,44 @@ async function transform(json, code) {
|
|
|
156
175
|
}
|
|
157
176
|
|
|
158
177
|
function chunk(size) {
|
|
159
|
-
return function (
|
|
178
|
+
return function (x) {
|
|
160
179
|
const res = []
|
|
161
180
|
let i = 0
|
|
162
|
-
while (i <
|
|
163
|
-
res.push(
|
|
181
|
+
while (i < x.length) {
|
|
182
|
+
res.push(x.slice(i, i += size))
|
|
164
183
|
}
|
|
165
184
|
return res
|
|
166
185
|
}
|
|
167
186
|
}
|
|
168
187
|
|
|
169
|
-
function zip(...
|
|
170
|
-
const length = Math.min(...
|
|
188
|
+
function zip(...x) {
|
|
189
|
+
const length = Math.min(...x.map(a => a.length))
|
|
171
190
|
const res = []
|
|
172
191
|
for (let i = 0; i < length; i++) {
|
|
173
|
-
res.push(
|
|
192
|
+
res.push(x.map(a => a[i]))
|
|
174
193
|
}
|
|
175
194
|
return res
|
|
176
195
|
}
|
|
196
|
+
|
|
197
|
+
function flatten(x) {
|
|
198
|
+
if (Array.isArray(x)) return x.flat()
|
|
199
|
+
throw new Error(`Cannot flatten ${typeof x}`)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function reverse(x) {
|
|
203
|
+
if (Array.isArray(x)) return x.reverse()
|
|
204
|
+
throw new Error(`Cannot reverse ${typeof x}`)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function keys(x) {
|
|
208
|
+
if (typeof x === 'object' && x !== null) return Object.keys(x)
|
|
209
|
+
throw new Error(`Cannot get keys of ${typeof x}`)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function values(x) {
|
|
213
|
+
if (typeof x === 'object' && x !== null) return Object.values(x)
|
|
214
|
+
throw new Error(`Cannot get values of ${typeof x}`)
|
|
215
|
+
}
|
|
177
216
|
}
|
|
178
217
|
|
|
179
218
|
async function read(fd = 0) {
|