fx 30.1.1 → 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.
Files changed (2) hide show
  1. package/index.js +27 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -82,9 +82,6 @@ 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)
@@ -100,15 +97,17 @@ async function transform(json, code) {
100
97
 
101
98
  if (/^\.\[/.test(code))
102
99
  return eval(`(function () {
100
+ const x = this
103
101
  return this${code.substring(1)}
104
102
  })`).call(json)
105
103
 
106
104
  if (/^\./.test(code))
107
105
  return eval(`(function () {
106
+ const x = this
108
107
  return this${code}
109
108
  })`).call(json)
110
109
 
111
- if (/^[a-z]+\(.+?\)$/i.test(code)) {
110
+ if (/^map\(.+?\)$/i.test(code)) {
112
111
  let s = code.substring(4, code.length - 1)
113
112
  if (s[0] === '.') s = 'x' + s
114
113
  return eval(`(function () {
@@ -121,13 +120,13 @@ async function transform(json, code) {
121
120
  return ${code}
122
121
  })`).call(json)
123
122
 
123
+ return apply(fn, json)
124
+
124
125
  function apply(fn, ...args) {
125
126
  if (typeof fn === 'function') return fn(...args)
126
127
  return fn
127
128
  }
128
129
 
129
- return apply(fn, json)
130
-
131
130
  function len(x) {
132
131
  if (Array.isArray(x)) return x.length
133
132
  if (typeof x === 'string') return x.length
@@ -145,11 +144,18 @@ async function transform(json, code) {
145
144
  throw new Error(`Cannot sort ${typeof x}`)
146
145
  }
147
146
 
148
- function sortBy(func) {
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
+ }
152
+ }
153
+
154
+ function sortBy(fn) {
149
155
  return function (x) {
150
156
  if (Array.isArray(x)) return x.sort((a, b) => {
151
- const fa = func(a)
152
- const fb = func(b)
157
+ const fa = fn(a)
158
+ const fb = fn(b)
153
159
  return fa < fb ? -1 : fa > fb ? 1 : 0
154
160
  })
155
161
  throw new Error(`Cannot sort ${typeof x}`)
@@ -160,11 +166,8 @@ async function transform(json, code) {
160
166
  return function (x) {
161
167
  const grouped = {}
162
168
  for (const item of x) {
163
- const key = typeof keyFn === 'function'
164
- ? keyFn(item)
165
- : item[keyFn]
166
- if (!grouped.hasOwnProperty(key))
167
- grouped[key] = []
169
+ const key = typeof keyFn === 'function' ? keyFn(item) : item[keyFn]
170
+ if (!grouped.hasOwnProperty(key)) grouped[key] = []
168
171
  grouped[key].push(item)
169
172
  }
170
173
  return grouped
@@ -200,6 +203,16 @@ async function transform(json, code) {
200
203
  if (Array.isArray(x)) return x.reverse()
201
204
  throw new Error(`Cannot reverse ${typeof x}`)
202
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
+ }
203
216
  }
204
217
 
205
218
  async function read(fd = 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fx",
3
- "version": "30.1.1",
3
+ "version": "30.2.0",
4
4
  "bin": {
5
5
  "fx": "index.js"
6
6
  },