this repo has no description
1import { Transform } from 'node:stream'
2
3const commaSplitter = new Transform({
4 readableObjectMode: true,
5
6 transform(chunk, encoding, callback) {
7 this.push(chunk.toString().trim().split(','))
8 callback()
9 }
10})
11
12const arrayToObject = new Transform({
13 readableObjectMode: true,
14 writableObjectMode: true,
15
16 transform(chunk, encoding, callback) {
17 const obj = {}
18 for (let i = 0; i < chunk.length; i += 2) {
19 obj[chunk[i]] = chunk[i + 1]
20 }
21 this.push(obj)
22
23 callback()
24 }
25})
26
27const objectToString = new Transform({
28 readableObjectMode: true,
29 writableObjectMode: true,
30
31 transform(chunk, encoding, callback) {
32 this.push(JSON.stringify(chunk) + '\n')
33 callback()
34 }
35})
36
37process.stdin
38 .pipe(commaSplitter)
39 .pipe(arrayToObject)
40 .pipe(objectToString)
41 .pipe(process.stdout)