Transforms data in a stream. Supports append, prepend, replace, erase, and compare
npm install @voliware/node-data-transform
Why do I need it?
If you have any stream in Node.js - a file, a TCP stream, a native Node request or response object - and you need to append data, prepend data, erase data, or replace data in the stream, you need this.
What is it?
Transform object, which is a native Nodejs stream object. Transforms are passed to the
pipe() function when dealing with readable streams. When data flows through the readable stream, it also flows through the transform. The transform can actually change the data as it goes down the pipe.
How do I use it?
Create one or many
DataTransform objects that do as many of the following actions
match means what are we looking for in the stream, and
content is what we will append, prepend, or replace it with.
In this example, we will create just one
DataTransform that will erase data, append some data with a file, prepend some data with a file, and replace some data with text. Note that the matches are named the same as the functions for clarity.
Input HTML file
<!-- append --><!-- prepend --><!-- replace --><!-- erase -->
let readable = Fs;let writable = Fs;let datatransform =// erase <!-- erase -->// append with contents of a file// prepend with contents of a file; // replace with text
The result will be
<!-- append --><!-- prepend -->Replaced!
What can I use it for?
let datatransform =// erase spaces// erase new lines
An HTML injector
let datatransform =// append with contents of a file
Search and replace in a file
let datatransform =// search and replace
There are two options when creating a
- If true [default], all chunks are concatenated before processing. This is usually what you want.
- If false, chunks are processed and sent downstream as they arrive.
- Instead of using the API you can pass along an array of modifiers to the constructor.
- Each modifier object has the properties of
action- "append", "prepend", "replace", or "erase"
match- the string or buffer to match against
content- the string or buffer to append, prepend, or replace with
let datatransform =concat: falsemodifiers:action: "append" match: "Joe" contents: " Senior"action: "prepend" match: "Joe" contents: "Mr. "action: "replace" match: "Joe" contents: "Matt"action: "erase" match: "bad words";