fx 30.0.3 → 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.
Files changed (2) hide show
  1. package/index.js +96 -55
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -18,6 +18,7 @@ void async function main() {
18
18
  }
19
19
  if (flagHelp || (args.length === 0 && process.stdin.isTTY))
20
20
  return printUsage()
21
+ const theme = themes(process.stdout.isTTY ? (process.env.FX_THEME || '1') : '0')
21
22
  await importFxrc(process.cwd())
22
23
  await importFxrc(os.homedir())
23
24
  let fd = 0 // stdin
@@ -35,17 +36,17 @@ void async function main() {
35
36
  for (const json of input) {
36
37
  array.push(json)
37
38
  }
38
- await runTransforms(array, args)
39
+ await runTransforms(array, args, theme)
39
40
  } else {
40
41
  for (const json of input) {
41
- await runTransforms(json, args)
42
+ await runTransforms(json, args, theme)
42
43
  }
43
44
  }
44
45
  }()
45
46
 
46
47
  const skip = Symbol('skip')
47
48
 
48
- async function runTransforms(json, args) {
49
+ async function runTransforms(json, args, theme) {
49
50
  const process = await import('node:process')
50
51
  let i, code, output = json
51
52
  for ([i, code] of args.entries()) try {
@@ -62,7 +63,7 @@ async function runTransforms(json, args) {
62
63
  else if (output === skip)
63
64
  return
64
65
  else
65
- console.log(stringify(output, process.stdout.isTTY))
66
+ console.log(stringify(output, theme))
66
67
 
67
68
  function printErr(err) {
68
69
  let pre = args.slice(0, i).join(' ')
@@ -89,6 +90,14 @@ async function transform(json, code) {
89
90
  return (${fold(code.split('[]'))})(this)
90
91
  })`).call(json)
91
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
+
92
101
  if (/^\.\[/.test(code))
93
102
  return eval(`(function () {
94
103
  return this${code.substring(1)}
@@ -99,7 +108,7 @@ async function transform(json, code) {
99
108
  return this${code}
100
109
  })`).call(json)
101
110
 
102
- if (/^map\(.+?\)$/.test(code)) {
111
+ if (/^[a-z]+\(.+?\)$/i.test(code)) {
103
112
  let s = code.substring(4, code.length - 1)
104
113
  if (s[0] === '.') s = 'x' + s
105
114
  return eval(`(function () {
@@ -112,36 +121,48 @@ async function transform(json, code) {
112
121
  return ${code}
113
122
  })`).call(json)
114
123
 
115
- return apply(fn, json)
116
-
117
124
  function apply(fn, ...args) {
118
125
  if (typeof fn === 'function') return fn(...args)
119
126
  return fn
120
127
  }
121
128
 
122
- function fold(s) {
123
- if (s.length === 1)
124
- return 'x => x' + s[0]
125
- let obj = s.shift()
126
- obj = obj === '.' ? 'x' : 'x' + obj
127
- return `x => Object.values(${obj}).flatMap(${fold(s)})`
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}`)
128
136
  }
129
137
 
130
- function uniq(array) {
131
- return [...new Set(array)]
138
+ function uniq(x) {
139
+ if (Array.isArray(x)) return [...new Set(x)]
140
+ throw new Error(`Cannot get unique values of ${typeof x}`)
132
141
  }
133
142
 
134
- function sort(array) {
135
- return array.sort()
143
+ function sort(x) {
144
+ if (Array.isArray(x)) return x.sort()
145
+ throw new Error(`Cannot sort ${typeof x}`)
146
+ }
147
+
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
+ }
136
157
  }
137
158
 
138
- function groupBy(keyOrFunction) {
139
- return array => {
159
+ function groupBy(keyFn) {
160
+ return function (x) {
140
161
  const grouped = {}
141
- for (const item of array) {
142
- const key = typeof keyOrFunction === 'function'
143
- ? keyOrFunction(item)
144
- : item[keyOrFunction]
162
+ for (const item of x) {
163
+ const key = typeof keyFn === 'function'
164
+ ? keyFn(item)
165
+ : item[keyFn]
145
166
  if (!grouped.hasOwnProperty(key))
146
167
  grouped[key] = []
147
168
  grouped[key].push(item)
@@ -151,24 +172,34 @@ async function transform(json, code) {
151
172
  }
152
173
 
153
174
  function chunk(size) {
154
- return function (arr) {
175
+ return function (x) {
155
176
  const res = []
156
177
  let i = 0
157
- while (i < arr.length) {
158
- res.push(arr.slice(i, i += size))
178
+ while (i < x.length) {
179
+ res.push(x.slice(i, i += size))
159
180
  }
160
181
  return res
161
182
  }
162
183
  }
163
184
 
164
- function zip(...arrays) {
165
- const length = Math.min(...arrays.map(a => a.length))
185
+ function zip(...x) {
186
+ const length = Math.min(...x.map(a => a.length))
166
187
  const res = []
167
188
  for (let i = 0; i < length; i++) {
168
- res.push(arrays.map(a => a[i]))
189
+ res.push(x.map(a => a[i]))
169
190
  }
170
191
  return res
171
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
+ }
172
203
  }
173
204
 
174
205
  async function read(fd = 0) {
@@ -520,14 +551,10 @@ function* parseJson(gen) {
520
551
  }
521
552
  }
522
553
 
523
- function stringify(value, isPretty = false) {
524
- const colors = {
525
- key: isPretty ? '\x1b[1;34m' : '',
526
- string: isPretty ? '\x1b[32m' : '',
527
- number: isPretty ? '\x1b[36m' : '',
528
- boolean: isPretty ? '\x1b[35m' : '',
529
- null: isPretty ? '\x1b[38;5;243m' : '',
530
- reset: isPretty ? '\x1b[0m' : '',
554
+ function stringify(value, theme) {
555
+ function color(id, str) {
556
+ if (theme[id] === '') return str
557
+ return `\x1b[${theme[id]}m${str}\x1b[0m`
531
558
  }
532
559
 
533
560
  function getIndent(level) {
@@ -536,38 +563,35 @@ function stringify(value, isPretty = false) {
536
563
 
537
564
  function stringifyValue(value, level = 0) {
538
565
  if (typeof value === 'string') {
539
- return `${colors.string}${JSON.stringify(value)}${colors.reset}`
566
+ return color(2, JSON.stringify(value))
540
567
  } else if (typeof value === 'number') {
541
- return `${colors.number}${value}${colors.reset}`
568
+ return color(3, `${value}`)
542
569
  } else if (typeof value === 'bigint') {
543
- return `${colors.number}${value}${colors.reset}`
570
+ return color(3, `${value}`)
544
571
  } else if (typeof value === 'boolean') {
545
- return `${colors.boolean}${value}${colors.reset}`
572
+ return color(4, `${value}`)
546
573
  } else if (value === null || typeof value === 'undefined') {
547
- return `${colors.null}null${colors.reset}`
574
+ return color(5, `null`)
548
575
  } else if (Array.isArray(value)) {
549
576
  if (value.length === 0) {
550
- return `[]`
577
+ return color(0, `[]`)
551
578
  }
552
579
  const items = value
553
- .map((v) => `${getIndent(level + 1)}${stringifyValue(v, level + 1)}`)
554
- .join(',\n')
555
- return `[\n${items}\n${getIndent(level)}]`
580
+ .map((v) => getIndent(level + 1) + stringifyValue(v, level + 1))
581
+ .join(color(0, ',') + '\n')
582
+ return color(0, '[') + '\n' + items + '\n' + getIndent(level) + color(0, ']')
556
583
  } else if (typeof value === 'object') {
557
584
  const keys = Object.keys(value)
558
585
  if (keys.length === 0) {
559
- return `{}`
586
+ return color(0, '{}')
560
587
  }
561
588
  const entries = keys
562
- .map(
563
- (key) =>
564
- `${getIndent(level + 1)}${colors.key}"${key}"${colors.reset}: ${stringifyValue(
565
- value[key],
566
- level + 1,
567
- )}`,
589
+ .map((key) =>
590
+ getIndent(level + 1) + color(1, `"${key}"`) + color(0, ': ') +
591
+ stringifyValue(value[key], level + 1),
568
592
  )
569
- .join(',\n')
570
- return `{\n${entries}\n${getIndent(level)}}`
593
+ .join(color(0, ',') + '\n')
594
+ return color(0, '{') + '\n' + entries + '\n' + getIndent(level) + color(0, '}')
571
595
  }
572
596
  throw new Error(`Unsupported value type: ${typeof value}`)
573
597
  }
@@ -575,6 +599,23 @@ function stringify(value, isPretty = false) {
575
599
  return stringifyValue(value)
576
600
  }
577
601
 
602
+ function themes(id) {
603
+ const themes = {
604
+ '0': ['', '', '', '', '', ''],
605
+ '1': ['', '1;34', '32', '36', '35', '38;5;243'],
606
+ '2': ['', '32', '34', '36', '35', '38;5;243'],
607
+ '3': ['', '95', '93', '96', '31', '38;5;243'],
608
+ '4': ['', '38;5;50', '38;5;39', '38;5;98', '38;5;205', '38;5;243'],
609
+ '5': ['', '38;5;230', '38;5;221', '38;5;209', '38;5;209', '38;5;243'],
610
+ '6': ['', '38;5;69', '38;5;78', '38;5;221', '38;5;203', '38;5;243'],
611
+ '7': ['', '1;38;5;42', '1;38;5;213', '1;38;5;201', '1;38;5;201', '38;5;243'],
612
+ '8': ['', '1;38;5;51', '38;5;195', '38;5;123', '38;5;50', '38;5;243'],
613
+ '🔵': ['1;38;5;33', '38;5;33', '', '', '', ''],
614
+ '🥝': ['38;5;179', '1;38;5;154', '38;5;82', '38;5;226', '38;5;226', '38;5;230'],
615
+ }
616
+ return themes[id] || themes['1']
617
+ }
618
+
578
619
  async function importFxrc(path) {
579
620
  const {join} = await import('node:path')
580
621
  const {pathToFileURL} = await import('node:url')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fx",
3
- "version": "30.0.3",
3
+ "version": "30.1.1",
4
4
  "bin": {
5
5
  "fx": "index.js"
6
6
  },