node package manager

kthulhu

Build Status NPM Downloads

Synopsis | Install | Examples | License

// Just passing through! ^(;,;)^ 
process.stdin.pipe($(function (chunk) {
  return chunk
})).pipe(process.stdout)
 
// Swap all `a`'s for `z`'s 
fs.createReadStream("aaa.txt")
  .pipe($(function (chunk) {
    return chunk.map(function (value) {
      return (value == 97) ? 122 : value
    })
   }))
  .pipe(fs.createWriteStream("zzz.txt"))
 
// Using ES6 arrow functions (Use node --harmony) 
fs.createReadStream("aaa.txt")
  .pipe($((chunk) => chunk.map((value) => value == 97 ? 122 : value))
  .pipe(fs.createWriteStream("zzz.txt"))

Synopsis

kthulhu is a through2-like simpler transform/passthru Stream wrapper. Uses readable-stream to help fight global warming and city crime.

To push contents to the buffer simply return chunk inside your transform callback.

You can also return a Promise and whatever value is resolved will be pushed to the buffer.

Install

npm install kthulhu

Examples

See the streams documentation to learn more about Streams.

Simple Transformation

var $ = require("kthulhu")
 
process.stdin // ^(;,;)^ 
  .pipe($(function (chunk) {
    return chunk.toString().split("").reverse().join("")
  }))
  .pipe(process.stdout)
» echo "uhluhtk" | ./example.js
kthulhu⏎

Using Promises

return chunk is used to notify the stream implementation we are done processing the supplied chunk. If your transformation is asynchrounous, you can return a Promise instead and kthulhu will apply its resolved value to the buffer (whenever that happens).

var $ = require("kthulhu") //^(;,;)^ 
 
process.stdin
  .pipe($(function (chunk) {
    return chunk.toString().toUpperCase()
  }))
  .pipe($(function (chunk) {
    // complicate it with a Promise 
    return new Promise(function(resolve) {
      setTimeout(function () {
        resolve(chunk.toString() + ">>>>>")
      }, 1000)
    })
  }))
  .pipe(process.stdout)
 
» echo "hello!" | ./example.js
HELLO!
>>>>>

Simple CSS Selector with Trumpet

function css(clss, transform) {
  var tr = require('trumpet')()
  var $ = require('kthulhu')
 
  clss = tr.select(clss).createStream()
  clss.pipe($(function (buf) {
    return transform(buf)
  })).pipe(clss)
 
  return tr
}
 
/*
./source.html
 
  <html>
  <head>
    <title></title>
  </head>
  <body>
    <div class="section">
      This is what you'll get!
    </div>
  </body>
  </html>
*/
 
var fs = require("fs")
var rs = fs.createReadStream(__dirname + "/source.html")
  .pipe(css(".section", function (html) {
    return /* ^(;,;)^ transform html */
  }))
  .pipe(process.stdout)

License

MIT