fx 36.0.4 → 37.0.1
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 +46 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -123,13 +123,6 @@ function transpile(code) {
|
|
|
123
123
|
if (/^\./.test(code))
|
|
124
124
|
return `x${code}`
|
|
125
125
|
|
|
126
|
-
// deprecated
|
|
127
|
-
if (/^map\(.+?\)$/i.test(code)) {
|
|
128
|
-
let s = code.substring(4, code.length - 1)
|
|
129
|
-
if (s[0] === '.') s = 'x' + s
|
|
130
|
-
return `x.map((x, i) => apply(${s}, x, i))`
|
|
131
|
-
}
|
|
132
|
-
|
|
133
126
|
if (/^@/.test(code)) {
|
|
134
127
|
const jsCode = transpile(code.substring(1))
|
|
135
128
|
return `x.map((x, i) => apply(${jsCode}, x, i))`
|
|
@@ -166,10 +159,54 @@ async function run(json, code) {
|
|
|
166
159
|
throw new Error(`Cannot sort ${typeof x}`)
|
|
167
160
|
}
|
|
168
161
|
|
|
162
|
+
function filter(fn) {
|
|
163
|
+
return function (x) {
|
|
164
|
+
if (Array.isArray(x)) {
|
|
165
|
+
return x.filter((v, i) => fn(v, i))
|
|
166
|
+
} else if (x !== null && typeof x === 'object') {
|
|
167
|
+
const result = {}
|
|
168
|
+
for (const [k, v] of Object.entries(x)) {
|
|
169
|
+
if (fn(v, k)) {
|
|
170
|
+
result[k] = v
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return result
|
|
174
|
+
} else {
|
|
175
|
+
throw new Error(`Cannot filter ${typeof x}`)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
169
180
|
function map(fn) {
|
|
170
181
|
return function (x) {
|
|
171
|
-
if (Array.isArray(x))
|
|
172
|
-
|
|
182
|
+
if (Array.isArray(x)) {
|
|
183
|
+
return x.map((v, i) => fn(v, i))
|
|
184
|
+
} else if (x !== null && typeof x === 'object') {
|
|
185
|
+
const result = {}
|
|
186
|
+
for (const [k, v] of Object.entries(x)) {
|
|
187
|
+
result[k] = fn(v, k)
|
|
188
|
+
}
|
|
189
|
+
return result
|
|
190
|
+
} else {
|
|
191
|
+
throw new Error(`Cannot map over ${typeof x}`)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function walk(fn) {
|
|
197
|
+
return function recurse(value, key = null) {
|
|
198
|
+
if (Array.isArray(value)) {
|
|
199
|
+
const mapped = value.map((v, i) => recurse(v, i))
|
|
200
|
+
return fn(mapped, key)
|
|
201
|
+
} else if (value !== null && typeof value === 'object') {
|
|
202
|
+
const result = {}
|
|
203
|
+
for (const [k, v] of Object.entries(value)) {
|
|
204
|
+
result[k] = recurse(v, k)
|
|
205
|
+
}
|
|
206
|
+
return fn(result, key)
|
|
207
|
+
} else {
|
|
208
|
+
return fn(value, key)
|
|
209
|
+
}
|
|
173
210
|
}
|
|
174
211
|
}
|
|
175
212
|
|