With red-maple you can map/reduce one stream to another, using JavaScript code for map and reduce functions.
npm install red-maple
Given a new-line delimited JSON file such as this:
{"value":5}
{"value":3}
You can pipe the contents into the CLI tool and pipe the output to your destination
cat input.ndjson | red-maple --map "item * 2" --reduce "(memo || 0) + item" > output.txt
const redMaple = require('red-maple')
redMaple({
input: 'stdin',
output: 'stdout',
initial: 0,
map (item) {
return item * 2
},
reduce (memo, item) {
return memo + item
}
})
There's just a single function, which takes a few very powerful options.
This can be a named input pipe (stdin), a file path, or a readable stream. Will throw if the value is none of those.
This can be a named output pipe (stdout, stderr), a file path, or a writable stream. Will throw if the value is none of those.
This is the map function to transform your input records.
It can be a plain function, a json code fragment, or a path to a commonjs module or simple JavaScript file with just a single function in it. See the ensure-function module for more information about valid uses.
This is an optional reduce function to reduce your transformed records to a single result.
It can be a plain function, a json code fragment, or a path to a commonjs module or simple JavaScript file with just a single function in it. See the ensure-function module for more information about valid uses.
The initial memo
state for the reduce function to use.
A custom parser stream can be given to consume data from any format. By default, it will use ndjson.parser()
.
A custom serializer stream can be given to product data in any format. By default, it will use ndjson.serializer()
.