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