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

stream-replacer

v1.4.3

Published

A through-stream that replaces by a RegExp and an async replace function

Downloads

80

Readme

stream-replacer

npm version

A transform-stream that performs regexp search & replace on streams.

Features

Works with vinyl-streams in buffer- and stream-mode. Supports asynchronous replacement creation.

Usage

Single Data Stream

import fs from 'fs';
import streamReplacer from 'stream-replacer';

const replacer = streamReplacer({
  single: true,
  pattern: /("license": *")\w+(")/,
  substitute: function(match, tag, done) {
    // always use WTFPL
    done(null, match[1] + 'WTFPL' + match[2]);
  }
});

fs.createReadStream('package.json')
  .pipe(replacer)
  .pipe(fs.createWriteStream('new-package.json'));

Vinyl File Stream

import vinylFs from 'vinyl-fs';
import streamReplacer from 'stream-replacer';

function asyncDigest(cb) {
  // do some mysterious calculations
  cb('12bf4d');
}

const replacer = streamReplacer({
  // find some path reference
  pattern: /(src\s*=\s*)(["'])([\w\/-_]*\/)(\w+)(\.\w+)\2/,
  // prepend digest to basename
  substitute: function(match, tag, done) {
    asyncDigest(function (digest) {
      done(null,
        match[1] + match[2] + match[3] + match[4]
        + '-' + digest + match[5] + match[2]
      );
    });
  }
});

vinylFs.src(['src/**/*.html'], {buffer: false})
// works with 'buffer: true', too
  .pipe(replacer)
  .pipe(vinylFs.dest('dist'));

API

const replacer = streamReplacer(options);

Creates a new replacer. Recognized options are:

  • pattern (RegExp): the pattern to search for.
  • substitute (function (match, tag, done), required if pattern is specified): a function that returns the replacement string asynchronously. It takes:
    • match: the match-object, created by RegExp#exec.
    • tag: a tag, created by tagger (see below)
    • done: the callback to return error and replacement string: done(null, replacement). Call done() to skip replacement. Alternatively substitute may just return a replacement string synchronously (or null to skip) or a promise that resolves to a string or to null.
  • searchLwm (number, default: 1024): the search low-water-mark. This is the minimum window size that the search operates on in stream-mode (except on the end of the stream). Set this twice your maximum expected match-length to prevent search misses.
  • single (boolean, default: false): If true, create a replacer that works on a single data-stream. If false, create a replacer that works on a vinyl-file-stream. In latter case, the following additional options are recognized:
    • tagger (function(file)): a function that generates the tag from the processed vinyl-file. Defaults to a function that returns file.path.
    • optioner(function(file)): a function that returns an object to overwrite options per vinyl-file.