Skip to content

Commit

Permalink
tests, examples and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Sep 10, 2013
1 parent 8ccc8e8 commit 4582a7d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 43 deletions.
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
# dev-null-writable
[![build status](https://secure.travis-ci.org/thlorenz/dev-null-writable.png)](http://travis-ci.org/thlorenz/dev-null-writable)
# dev-null [![build status](https://secure.travis-ci.org/thlorenz/dev-null.png)](http://travis-ci.org/thlorenz/dev-null)

/dev/null for node streams
`/dev/null` for node streams

Use it whenever you need to interrupt stream flow for instance if you want to log the state of a stream instead of its
output.

```js
// TODO
// without devnull
var numbers = require('../test/fixtures/number-readable')

numbers({ to: 2 })
.on('data', function (d) { console.log(d.toString()) });
// =>
// 0
// 1
// 2
```

## Status
```js
// piping into devnull
var devnull = require('dev-null');
var numbers = require('../test/fixtures/number-readable');

Nix, Nada, Nichevo, Nothing --> go away!
## Installation
numbers({ to: 2 })
.pipe(devnull())
.on('data', function (d) { console.log(d.toString()) });

npm install dev-null-writable
=>
```

## API
## Installation

npm install dev-null

## License

Expand Down
6 changes: 6 additions & 0 deletions examples/devnull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var devnull = require('../');
var numbers = require('../test/fixtures/number-readable');

numbers({ to: 3 })
.pipe(devnull())
.on('data', function (d) { console.log(d.toString()) });
4 changes: 4 additions & 0 deletions examples/no-devnull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var numbers = require('../test/fixtures/number-readable')

numbers({ to: 3 })
.on('data', function (d) { console.log(d.toString()) });
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

var util = require('util')
, stream = require('stream')
, Writable = stream.Writable
, setImmediate = setImmediate || function (fn) { setTimeout(fn, 0) }
;

module.exports = DevNull;

util.inherits(DevNull, Writable);

function DevNull (opts) {
if (!(this instanceof DevNull)) return new DevNull(opts);

opts = opts || {};
Writable.call(this, opts);
}

DevNull.prototype._write = function (chunk, encoding, cb) {
setImmediate(cb);
}
69 changes: 35 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
{
"name" : "dev-null-writable",
"version" : "0.0.0",
"description" : "/dev/null for node streams",
"main" : "index.js",
"scripts" : {
"test-main" : "tap test/*.js",
"test-0.8" : "nave use 0.8 npm run test-main",
"test-0.10" : "nave use 0.10 npm run test-main",
"test-all": "npm run test-main && npm run test-0.8 && npm run test-0.10",
"test" : "if [ -e $TRAVIS ]; then npm run test-all; else npm run test-main; fi"
},
"repository" : {
"type" : "git",
"url" : "git://github.com/thlorenz/dev-null-writable.git"
},
"homepage" : "https://github.com/thlorenz/dev-null-writable",
"dependencies" : {
},
"devDependencies" : {
"nave": "~0.4.3",
"tape": "~1.0.4",
"tap": "~0.4.3"
},
"keywords": [] ,
"author" : {
"name" : "Thorsten Lorenz",
"email" : "thlorenz@gmx.de",
"url" : "http://thlorenz.com"
},
"license" : {
"type": "MIT",
"url": "https://github.com/thlorenz/dev-null-writable/blob/master/LICENSE"
},
"engine" : { "node" : ">=0.6" }
"name": "dev-null",
"version": "0.0.0",
"description": "/dev/null for node streams",
"main": "index.js",
"scripts": {
"test-main": "tap test/*.js",
"test-0.8": "nave use 0.8 npm run test-main",
"test-0.10": "nave use 0.10 npm run test-main",
"test-all": "npm run test-main && npm run test-0.8 && npm run test-0.10",
"test": "if [ -e $TRAVIS ]; then npm run test-all; else npm run test-main; fi"
},
"repository": {
"type": "git",
"url": "git://github.com/thlorenz/dev-null.git"
},
"homepage": "https://github.com/thlorenz/dev-null",
"dependencies": {},
"devDependencies": {
"nave": "~0.4.3",
"tap": "~0.4.3",
"tap-stream": "~0.2.0"
},
"keywords": [],
"author": {
"name": "Thorsten Lorenz",
"email": "thlorenz@gmx.de",
"url": "http://thlorenz.com"
},
"license": {
"type": "MIT",
"url": "https://github.com/thlorenz/dev-null/blob/master/LICENSE"
},
"engine": {
"node": ">=0.6"
}
}
21 changes: 21 additions & 0 deletions test/fixtures/number-readable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var util = require('util')
, stream = require('stream')
, Readable = stream.Readable

module.exports = NumberReadable;

util.inherits(NumberReadable, Readable);

function NumberReadable (opts) {
if (!(this instanceof NumberReadable)) return new NumberReadable(opts);
Readable.call(this, opts);
this.idx = 0;
this.to = opts.to;
}

NumberReadable.prototype._read = function () {
if (this.idx > this.to) return this.push(null);
this.push('' + this.idx++);
}
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
/*jshint asi: true */

var test = require('tap').test
var devnull = require('../');
var tapstream = require('tap-stream')
var numbers = require('./fixtures/number-readable')

test('\npiping without devnull', function (t) {
var data = [];
numbers({ to: 2 })
.on('data', function (d) { data.push(d) })
.on('end', function () {
t.equal(data.length, 3, 'streams 3 numbers')
t.end()
})
})

test('\npiping through devnull', function (t) {
var data = [];
numbers({ to: 2 })
.on('end', function () {
t.equal(data.length, 0, 'streams 0 numbers')
t.end()
})
.pipe(devnull())
.on('data', function (d) { data.push(d) })
})

0 comments on commit 4582a7d

Please sign in to comment.