fx 27.0.0 → 28.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 +17 -1
  2. package/index.js +32 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -81,7 +81,7 @@ Fx has a special symbol **skip** for skipping the printing of the result.
81
81
  ls | fx -r '.includes(".md") ? this : skip'
82
82
  ```
83
83
 
84
- Fx comes with a set of useful functions: **uniq**, **sort**, **groupBy**.
84
+ Fx comes with a set of useful functions: **uniq**, **sort**, **groupBy**, **chunk**, **zip**.
85
85
 
86
86
  ```sh
87
87
  cat file.json | fx 'uniq' 'sort' 'groupBy(x => x.name)'
@@ -113,6 +113,22 @@ Fx has a special syntax for the flatMap function. Fox example,
113
113
  curl https://api.github.com/repos/kubernetes/kubernetes/issues | fx '.[].labels[].name'
114
114
  ```
115
115
 
116
+ ### .fxrc.js
117
+
118
+ Fx supports `.fxrc.js` file in the current directory or in the home directory.
119
+
120
+ Put the next code in the `.fxrc.js` file to make `myFunction` available in the fx.
121
+
122
+ ```js
123
+ global.myFunction = x => x + 1
124
+ ```
125
+
126
+ Now you can use `myFunction` in the fx.
127
+
128
+ ```sh
129
+ echo '1' | fx 'myFunction'
130
+ ```
131
+
116
132
  ## License
117
133
 
118
134
  [MIT](../LICENSE)
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  'use strict'
3
3
 
4
4
  void async function main() {
5
+ const os = await import('node:os')
5
6
  const process = await import('node:process')
6
7
  let flagHelp = false
7
8
  let flagRaw = false
@@ -16,7 +17,8 @@ void async function main() {
16
17
  }
17
18
  if (flagHelp || (args.length === 0 && process.stdin.isTTY))
18
19
  return printUsage()
19
-
20
+ await importFxrc(process.cwd())
21
+ await importFxrc(os.homedir())
20
22
  const stdin = await readStdinGenerator()
21
23
  const input = flagRaw ? readLine(stdin) : parseJson(stdin)
22
24
  if (flagSlurp) {
@@ -137,6 +139,26 @@ async function transform(json, code) {
137
139
  return grouped
138
140
  }
139
141
  }
142
+
143
+ function chunk(size) {
144
+ return function (arr) {
145
+ const res = []
146
+ let i = 0
147
+ while (i < arr.length) {
148
+ res.push(arr.slice(i, i += size))
149
+ }
150
+ return res
151
+ }
152
+ }
153
+
154
+ function zip(...arrays) {
155
+ const length = Math.min(...arrays.map(a => a.length))
156
+ const res = []
157
+ for (let i = 0; i < length; i++) {
158
+ res.push(arrays.map(a => a[i]))
159
+ }
160
+ return res
161
+ }
140
162
  }
141
163
 
142
164
  async function readStdinGenerator() {
@@ -535,6 +557,15 @@ function stringify(value, isPretty = false) {
535
557
  return stringifyValue(value)
536
558
  }
537
559
 
560
+ async function importFxrc(path) {
561
+ const {join} = await import('node:path')
562
+ try {
563
+ await import(join(path, '.fxrc.js'))
564
+ } catch (err) {
565
+ if (err.code !== 'ERR_MODULE_NOT_FOUND') throw err
566
+ }
567
+ }
568
+
538
569
  function printUsage() {
539
570
  const usage = `Usage
540
571
  fx [flags] [code...]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fx",
3
- "version": "27.0.0",
3
+ "version": "28.0.0",
4
4
  "bin": {
5
5
  "fx": "index.js"
6
6
  },