How to use the through2.ctor function in through2

To help you get started, we’ve selected a few through2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hybridables / always-done / test / streams.js View on Github external
*/

/* jshint asi:true */

'use strict'

var fs = require('fs')
var path = require('path')
var test = require('assertit')
var through2 = require('through2')
var alwaysDone = require('../index')

var exists = path.join(__dirname, '../.gitignore')
var notExists = path.join(__dirname, '../not_exists')

var EndStream = through2.ctor(function (chunk, enc, cb) {
  this.push(chunk)
  cb()
}, function (cb) {
  this.emit('end', 2)
  cb()
})

function success () {
  var read = fs.createReadStream(exists)
  return read.pipe(new EndStream())
}

function failure () {
  var read = fs.createReadStream(notExists)
  return read.pipe(new EndStream())
}
github alexanderGugel / jdi / index.js View on Github external
// filename of the processed file itself.
	const file = this.options.file
	// The `.md` file will be in the same directory as the source file. We only
	// need the basename.
	const basename = path.basename(file)
	const date = String(new Date())
	this.push('------------------------\n')
	this.push(`Generated _${date}_ from [Ⓢ ${basename}](${basename} "View in source")\n`)
	this.push('\n')
	cb()
}

// ## `Transform`
// Instead of returning a `stream.Transform` instance, through2.ctor() returns a
// constructor for our custom Transform that operates on a line-by-line basis.
const Transform = through2.ctor(transformFunction, flushFunction)

// ## `doc`
// `doc` accepts a filename, creates a corresponding read stream, processes the
// file and writes the resulting `.md` file to disk.
const doc = file =>
	fs.createReadStream(file)
		// [`split`](https://www.npmjs.com/package/split) is a transform that
		// generates chunks separated by new-line.
		.pipe(split())
		// Here we invoke our custom `Transform` instance used for processing
		// the separated line-chunks.
		.pipe(new Transform({
			// If there is no file extension, we assume that the source file is
			// a JavaScript file. In some cases, we could also determine the
			// extension name from the first line of the file, e.g.
			// `#!/usr/bin/env node`.
github brycebaril / through2-reduce / index.js View on Github external
function ctor(options, fn, initial) {
  if (typeof options == "function") {
    initial = fn
    fn = options
    options = {}
  }

  var Reduce = through2.ctor(options, function (chunk, encoding, callback) {
    if (this.options.wantStrings) chunk = chunk.toString()

    // First chunk with no initial value set
    if (this._reduction === undefined && this._index == 0) {
      this._reduction = chunk
      return callback()
    }

    try {
      this._reduction = fn.call(this, this._reduction, chunk, this._index++)
    } catch (e) {
      var err = e
    }
    return callback(err)
  }, function (callback) {
    this.push(this._reduction)
github gulpjs / async-done / test / streams.js View on Github external
'use strict';

var expect = require('expect');

var fs = require('fs');
var path = require('path');
var through = require('through2');
var pumpify = require('pumpify');

var asyncDone = require('../');

var exists = path.join(__dirname, '../.gitignore');
var notExists = path.join(__dirname, '../not_exists');

var EndStream = through.ctor(function(chunk, enc, cb) {
  this.push(chunk);
  cb();
}, function(cb) {
  this.emit('end', 2);
  cb();
});

function success() {
  var read = fs.createReadStream(exists);
  return read.pipe(new EndStream());
}

function failure() {
  var read = fs.createReadStream(notExists);
  return read.pipe(new EndStream());
}
github sciencefair-land / sciencefair / app / client / lib / datasource.js View on Github external
to: 'name'
    })

    const addsource = require('./stream-obj-xtend')({
      source: self.key
    })

    const filtermeta = require('./through-filter')(
      data => data.type ==='file' && /^(\/|\\)meta(\/|\\).*json$/.test(data.name)
    )

    const getentry = require('./hyperdrive-sync-entry')(self.metadata)

    let n = 0
    let lastpcdone = 0
    const getprogress = through.ctor({ objectMode: true }, (data, _, next) => {
      n++
      if (n % 30 === 0 || n === self.articleCount) {
        const progress = {
          done: n,
          total: self.articleCount
        }
        const pcdone = Math.round((progress.done / progress.total) * 100)
        if (pcdone !== lastpcdone) {
          self.stats.get('metadataSync').assign(progress).write()
          self.emit('progress', progress)
        }
        lastprogress = progress
      }
      next(null, data)
    })
github brycebaril / through2-filter / index.js View on Github external
function ctor(options, fn) {
  if (typeof options == "function") {
    fn = options
    options = {}
  }

  var Filter = through2.ctor(options, function (chunk, encoding, callback) {
    if (this.options.wantStrings) chunk = chunk.toString()
    try {
      if (fn.call(this, chunk, this._index++)) this.push(chunk)
      return callback()
    } catch (e) {
      return callback(e)
    }
  })
  Filter.prototype._index = 0
  return Filter
}
github brycebaril / through2-map / index.js View on Github external
function ctor(options, fn) {
  if (typeof options == "function") {
    fn = options
    options = {}
  }

  var Map = through2.ctor(options, function (chunk, encoding, callback) {
    if (this.options.wantStrings) chunk = chunk.toString()
    try {
      this.push(fn.call(this, chunk, this._index++))
      return callback()
    } catch (e) {
      return callback(e)
    }
  })
  Map.prototype._index = 0
  return Map
}
github brycebaril / through2-spy / index.js View on Github external
function ctor(options, fn) {
  if (typeof options == "function") {
    fn = options
    options = {}
  }
  return through2.ctor(options, function (chunk, encoding, callback) {
    if (this.options.wantStrings) chunk = chunk.toString()
    var err = fn.call(this, chunk, encoding)
    if (err instanceof Error) return callback(err)
    this.push(chunk)
    return callback(null)
  })
}

through2

A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise

MIT
Latest version published 4 years ago

Package Health Score

74 / 100
Full package analysis