npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@voliware/node-data-transform

v1.2.1

Published

Transforms data in a stream. Supports append, prepend, replace, erase, and compare

Downloads

20

Readme

node-data-transform

Transforms data in a stream. Supports append, prepend, replace, erase, and compare. In the future it will support split.

Install

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, compare data, or replace data in the stream, you need this.

What is it?

An enhanced 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. DataTransform is able to process the chunks perfectly, whether you receive massive chunks at a time, or literally one byte at a time.

How do I use it?

Create one or many DataTransform objects that do as many of the following actions

  1. append(match, content)
  2. prepend(match, content)
  3. replace(match, content)
  4. erase(match)
  5. compare(match)

Here, match means what are we looking for in the stream, and content is what we will append, prepend, or replace it with. For erase and compare, we don't need any content. erase simply removes the data while compare emits an event.

Example

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

<div>
    <!-- append -->
    <!-- prepend -->
</div>
<div>
    <!-- replace -->
    <!-- erase -->
</div>

Setup

let readable = Fs.createReadStream("base.html");
let writable = Fs.createWriteStream("index.html");
let datatransform = new DataTransform()
    .erase('<!-- erase -->')                             // erase <!-- erase --> 
    .append('<!-- append -->', {file: "append.html"})    // append with contents of a file
    .prepend('<!-- prepend -->', {file: "prepend.html"}) // prepend with contents of a file
    .replace('<!-- replace -->', {string: "Replaced!"}); // replace with text

The result will be

<div>
    <!-- append --><div id="append"></div>
    <div id="prepend"></div><!-- prepend -->
</div>
<div>
    Replaced!
    
</div>

What can I use it for?

A minifier

let datatransform = new DataTransform()
    .erase(' ')                                     // erase spaces
    .erase(Buffer.from([0x0d, 0x0a]))               // erase new lines 

An HTML injector

let datatransform = new DataTransform()
    .append('<!-- templates -->', {file: "templates.html"}) // append with contents of a file

Search and replace in a file

let datatransform = new DataTransform()
    .replace('youre', {string: "you're"})                      // search and replace 

Options

There are two options when creating a DataTransform.

  1. concat
    • 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.
  2. modifiers
    • 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", "compare", "replace", or "erase"
      • match - the string or buffer to match against
      • content - the string or buffer or filepath to append, prepend, or replace with

Example 1

Append " Senior" each time we find "Joe".

Prepend "Mr. " each time we find "Joe".

Replace "dog" with "cat".

Erase all "bad words"!

let datatransform = new DataTransform({
    concat: false,
    modifiers: [
        new Modifier("append", "Joe", {string: " Senior"}),
        new Modifier("prepend", "Joe", {string: " Mr."}),
        new Modifier("replace", "dog", {string: "cat"}),
        new Modifier("erase", {string: "bad words"})
    ]
});

Example 2

Even better, you can use an array to, for example, append many files or strings after one match.

let datatransform = new DataTransform({
    concat: false,
    modifiers: [
        new Modifier("append", "<!--templates-->", [
            {file: './src/html/template1.html'},
            {file: './src/html/template2.html'},
            {string: '<template id="abc">ABC</template>'}
        ])
    ]
});