fx 24.1.0 → 25.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 (3) hide show
  1. package/README.md +22 -4
  2. package/index.js +28 -14
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -41,20 +41,38 @@ access the input data without a `x => x` part.
41
41
  echo '{"name": "world"}' | fx '.name' '`Hello, ${this}!`'
42
42
  ```
43
43
 
44
- Fx has a shortcut for the map function. Fox example `.map(x => x.commit.message)`
45
- can be written without leading dot and without `x =>` parts.
44
+ Use other JS functions to process the data.
46
45
 
47
46
  ```sh
48
- curl https://api.github.com/repos/antonmedv/fx/commits | fx 'map(x.commit.message)'
47
+ echo `{"name": "world"}` | fx 'Object.keys'
48
+ ```
49
+
50
+ ## Advanced Usage
51
+
52
+ Fx has a shortcut for the map function. Fox example, `this.map(x => x.commit.message)`
53
+ can be rewritten without leading dot and without `x => x` parts.
54
+
55
+ ```sh
56
+ curl https://api.github.com/repos/antonmedv/fx/commits | fx 'map(.commit.message)'
57
+ ```
58
+
59
+ ```sh
60
+ echo '[{"name": "world"}]' | fx 'map(`Hello, ${x.name}!`)'
49
61
  ```
50
62
 
51
63
  Fx has a special syntax for the flatMap function. Fox example,
52
- `.flatMap(x => x.labels.flatMap(x => x.name))` can be written in the next way.
64
+ `.flatMap(x => x.labels.flatMap(x => x.name))` can be rewritten in the next way.
53
65
 
54
66
  ```sh
55
67
  curl https://api.github.com/repos/kubernetes/kubernetes/issues | fx '.[].labels[].name'
56
68
  ```
57
69
 
70
+ Fx works with promises.
71
+
72
+ ```sh
73
+ echo '"https://medv.io/*"' | fx 'fetch' '.text()'
74
+ ```
75
+
58
76
  ## License
59
77
 
60
78
  [MIT](../LICENSE)
package/index.js CHANGED
@@ -1,26 +1,22 @@
1
1
  void async function main() {
2
2
  const process = await import('node:process')
3
+ const args = process.argv.slice(2)
3
4
  let input = ''
4
5
  process.stdin.setEncoding('utf8')
5
6
  for await (const chunk of process.stdin)
6
7
  input += chunk
7
- const args = process.argv.slice(2)
8
8
 
9
9
  let json
10
10
  try {
11
11
  json = JSON.parse(input)
12
12
  } catch (err) {
13
- try {
14
- json = eval(`(${input})`)
15
- } catch (_) {
16
- process.stderr.write(`Invalid JSON: ${err.message}\n`)
17
- process.exit(1)
18
- }
13
+ process.stderr.write(`Invalid JSON: ${err.message}\n`)
14
+ process.exit(1)
19
15
  }
20
16
 
21
17
  let i, code, output = json
22
18
  for ([i, code] of args.entries()) try {
23
- output = transform(output, code)
19
+ output = await transform(output, code)
24
20
  } catch (err) {
25
21
  printErr(err)
26
22
  process.exit(1)
@@ -46,7 +42,7 @@ void async function main() {
46
42
  }
47
43
  }()
48
44
 
49
- function transform(json, code) {
45
+ async function transform(json, code) {
50
46
  if ('.' === code)
51
47
  return json
52
48
 
@@ -68,20 +64,23 @@ function transform(json, code) {
68
64
  return this${code}
69
65
  })`).call(json)
70
66
 
71
- if (/^map\(.+?\)$/.test(code))
67
+ if (/^map\(.+?\)$/.test(code)) {
68
+ let s = code.substring(4, code.length - 1)
69
+ if (s[0] === '.') s = 'x' + s
72
70
  return eval(`(function () {
73
- return this.map(x => apply(x, ${code.substring(4, code.length - 1)}))
71
+ return this.map((x, i) => apply(${s}, x, i))
74
72
  })`).call(json)
73
+ }
75
74
 
76
75
  const fn = eval(`(function () {
77
76
  return ${code}
78
77
  })`).call(json)
79
78
 
80
- return apply(json, fn)
79
+ return apply(fn, json)
81
80
  }
82
81
 
83
- function apply(json, fn) {
84
- if (typeof fn === 'function') return fn(json)
82
+ function apply(fn, ...args) {
83
+ if (typeof fn === 'function') return fn(...args)
85
84
  return fn
86
85
  }
87
86
 
@@ -100,3 +99,18 @@ function uniq(array) {
100
99
  function sort(array) {
101
100
  return array.sort()
102
101
  }
102
+
103
+ function groupBy(keyOrFunction) {
104
+ return array => {
105
+ const grouped = {}
106
+ for (const item of array) {
107
+ const key = typeof keyOrFunction === 'function'
108
+ ? keyOrFunction(item)
109
+ : item[keyOrFunction]
110
+ if (!grouped.hasOwnProperty(key))
111
+ grouped[key] = []
112
+ grouped[key].push(item)
113
+ }
114
+ return grouped
115
+ }
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fx",
3
- "version": "24.1.0",
3
+ "version": "25.0.0",
4
4
  "bin": {
5
5
  "fx": "index.js"
6
6
  },