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

mktransform

v1.0.6

Published

Custom stream transformations

Downloads

39

Readme

Stream Transform

Build Status npm version Coverage Status

Inject custom stream transformations

Accepts a list of functions that should return stream classes to be injected into the transformation pipeline.

The highlight transform implementation serves as a good example; it highlights code blocks with info strings and rewrites the document stream.

Install

npm i mktransform --save

For the command line interface install mkdoc globally (npm i -g mkdoc).



Usage

Create a file for the transform function like upper.js:

// converts level one headings to upper case
function upper(through, ast) {
  var Node = ast.Node
    , collect = ast.NodeWalker.collect
    , i
    , text;

  function transform(chunk, encoding, cb) {
    if(Node.is(chunk, Node.HEADING) && chunk.level === 1) {
      text = collect(chunk, Node.TEXT);
      for(i = 0;i < text.length;i++) {
        text[i].literal = text[i].literal.toUpperCase();
      }
    }
    this.push(chunk);
    cb();
  }

  return through.transform(transform);
}

module.exports = upper;

And pass in the transform function:

var tfm = require('mktransform')
  , ast = require('mkast')
  , upper = require('./upper');

ast.src('# Project\n\nThis is a paragraph.\n\n## Install')
  .pipe(tfm(upper))
  .pipe(ast.stringify({indent: 2}))
  .pipe(process.stdout);

Example

Run a custom stream transformation:

mkcat README.md | mktransform doc/upper.js | mkout

Run multiple transformations:

mkcat README.md | mktransform test/fixtures/upper1.js test/fixtures/upper2.js | mkout

Stream Functions

A stream function has the signature:

function(through, ast, opts)

It is passed the through module so you can easily create stream transform classes and ast so you may easily inspect nodes; the opts object is the original options object. The function must return a transform stream subclass.

The input and output data should always be abstract syntax tree nodes.

To create a transform stream subclass:

function transformer(through) {

  function transform(chunk, encoding, cb) {
    // pass through stream
    cb(null, chunk);
  }

  // return the stream subclass
  return through.transform(transform);
}

module.exports = transformer;

If you also need a flush function:

function transformer(through) {

  function transform(chunk, encoding, cb) {
    cb(null, chunk);
  }

  function flush(cb) {
    cb(); 
  }

  return through.transform(transform, flush);
}

module.exports = transformer;

To use a specific constructor:

function transformer(through) {

  function Component(opts) {
    this.opts = opts || {}; 
  }

  function transform(chunk, encoding, cb) {
    cb(null, chunk);
  }

  return through.transform(transform, {ctor: Component});
}

module.exports = transformer;

See through, ast and the api docs for more detail.

Help

Usage: mktransform [files...]

  Custom stream transformations.

Options
  -h, --help              Display help and exit
  --version               Print the version and exit

[email protected]

API

transform

transform(opts[, cb])

Injects custom stream transform classes into the pipeline.

Accepts a single function, array of functions or an object with a transforms array.

Functions have the signature:

function(through, ast, opts)

They are passed the through and ast modules to help with creating stream subclasses and inspecting nodes and the original options.

If you are using multiple stream transformations the options are shared between them so take care to avoid name collisions.

Each function must return a transform stream subclass.

The returned subclass is instantiated and when multiple transform functions are being used a pipeline is created between the streams in the order supplied.

When a single stream is being created it is returned otherwise an array of all the created streams is returned.

If both the input and output options are given additional wrapper streams are created that parse JSON from the input stream and write JSON to the output stream, in this instance you may pass a callback function which is added as a listener for the error and finish events on the output stream; the output stream is returned.

Returns an output stream or array of streams.

  • opts Function|Array|Object processing options.
  • cb Function callback function.

Options

  • transforms Array list of transform functions.
  • input Readable input stream.
  • output Writable output stream.

Throws

  • TypeError if the target is not a function.
  • TypeError if the return value is not a function.
  • TypeError if the stream instance has no pipe function.

License

MIT


Created by mkdoc on April 18, 2016