stream-assert
Advanced tools
Comparing version 1.1.1 to 1.2.0
156
index.js
@@ -1,1 +0,155 @@ | ||
module.exports = require('./lib/assert.js'); | ||
var through = require('through2'); | ||
var assert = {}; | ||
assert.defaults = { | ||
highWatermark: 16, | ||
objectMode: true | ||
}; | ||
function assertStream(options, transform, flush) { | ||
if (typeof options === 'function') { | ||
flush = transform; | ||
transform = options; | ||
options = {}; | ||
} | ||
options.highWatermark = options.highWatermark || assert.defaults.highWatermark; | ||
options.objectMode = options.objectMode || assert.defaults.objectMode; | ||
var stream = through(options, transform, flush); | ||
stream.on('pipe', function (source) { | ||
source._piped = true; | ||
source.on('error', function (err) { | ||
stream._parentError = err; | ||
}); | ||
}); | ||
stream.bubble = function (err) { | ||
this.emit(this._piped ? 'error' : 'end', err); | ||
}; | ||
return stream; | ||
} | ||
assert.nth = function (n, assertion) { | ||
var i = 0; | ||
return assertStream(function (obj, enc, cb) { | ||
if (i === n) { | ||
try { | ||
assertion(obj); | ||
} catch (err) { | ||
this.bubble(new Error(n + ' position is not passing assertion: ' + err.message)); | ||
this.emit('close'); | ||
} | ||
} | ||
i++; | ||
this.push(obj); | ||
cb(); | ||
}).on('finish', function () { | ||
this.bubble(this._parentError); | ||
}); | ||
}; | ||
assert.last = function (assertion) { | ||
var lastItem; | ||
return assertStream(function (obj, enc, cb) { | ||
lastItem = obj; | ||
this.push(obj); | ||
cb(); | ||
}).on('finish', function () { | ||
if (this._parentError) { return this.emit('end', this._parentError); } | ||
try { | ||
assertion(lastItem); | ||
this.emit('end'); | ||
} catch (err) { | ||
this.bubble(new Error('Last element is not passing assertion: ' + err.message)); | ||
this.emit('close'); | ||
} | ||
}); | ||
}; | ||
assert.first = function (assertion) { | ||
return assert.nth(0, assertion); | ||
}; | ||
assert.second = function (assertion) { | ||
return assert.nth(1, assertion); | ||
}; | ||
assert.all = function (assertion) { | ||
var i = 0; | ||
return assertStream(function (obj, enc, cb) { | ||
try { | ||
assertion(obj); | ||
} catch (err) { | ||
this.bubble(new Error('Element on ' + i + ' position is not passing assertion: ' + err.message)); | ||
this.emit('close'); | ||
} | ||
i++; | ||
this.push(obj); | ||
cb(); | ||
}).on('finish', function () { | ||
this.emit('end', this._parentError); | ||
}); | ||
}; | ||
assert.any = function (assertion) { | ||
var matched = false; | ||
return assertStream(function (obj, enc, cb) { | ||
try { | ||
assertion(obj); | ||
matched = true; | ||
} catch (err) { } | ||
this.push(obj); | ||
cb(); | ||
}).on('finish', function () { | ||
if (this._parentError) { return this.emit('end', this._parentError); } | ||
if (!matched) { | ||
return this.bubble(new Error('Nothing passing assertion')); | ||
} | ||
this.emit('end'); | ||
}); | ||
}; | ||
assert.length = function (expected) { | ||
var i = 0; | ||
return assertStream(function (obj, enc, cb) { | ||
i++; | ||
this.push(obj); | ||
cb(); | ||
}).on('finish', function () { | ||
if (this._parentError) { return this.emit('end', this._parentError); } | ||
var assertion = expected; | ||
if (typeof expected !== 'function') { | ||
assertion = function (data) { | ||
if (data !== expected) { | ||
throw new Error(expected + ' is not equal ' + data); | ||
} | ||
}; | ||
} | ||
try { | ||
assertion(i); | ||
this.emit('end'); | ||
} catch (err) { | ||
this.bubble(new Error('Expected length ' + err.message)); | ||
} | ||
}); | ||
}; | ||
assert.end = function (cb) { | ||
return assertStream(function (obj, enc, cb) { | ||
// Dump all the data! | ||
cb(); | ||
}) | ||
.on('finish', function () { | ||
if (cb) { cb(this._parentError); } | ||
this.emit('end', this._parentError); | ||
}); | ||
}; | ||
module.exports = assert; |
{ | ||
"name": "stream-assert", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Assertion library for streams", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "mocha -R spec", | ||
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" | ||
}, | ||
@@ -26,2 +27,5 @@ "repository": { | ||
"should": "^4.0.4", | ||
"coveralls": "^2.7.0", | ||
"istanbul": "^0.3.0", | ||
"mocha-lcov-reporter": "^0.0.1", | ||
"stream-array": "^0.1.3", | ||
@@ -28,0 +32,0 @@ "tobe": "0.0.2" |
@@ -1,2 +0,3 @@ | ||
# stream-assert [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url] | ||
# stream-assert | ||
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] | ||
@@ -17,3 +18,4 @@ Assert streams with ease. | ||
.pipe(assert.length(1)) | ||
.on('end', console.log); | ||
.pipe(assert.end(console.log)) // One way | ||
.on('end', console.log) // Or another | ||
``` | ||
@@ -53,2 +55,16 @@ | ||
#### end([cb]) | ||
Since streams has internal [buffer and highWatermark](http://nodejs.org/api/stream.html#stream_buffering), | ||
that stops data flow, when reached — test stream needs a dumping point, that will flush that buffer. | ||
`assert.end` will dump all data to `/dev/null` — so all pipes after this point will not get any data. | ||
### assert.defaults | ||
Type: `Object` | ||
Contains defaults, that will be passed to `through` constructor. | ||
* `highWatermark` — by default, will be equal `16`. If you don't want to use `assert.end`, then you can increase it. | ||
## License | ||
@@ -66,1 +82,4 @@ | ||
[depstat-image]: http://img.shields.io/david/floatdrop/stream-assert.svg?style=flat | ||
[coveralls-url]: https://coveralls.io/r/floatdrop/stream-assert | ||
[coveralls-image]: http://img.shields.io/coveralls/floatdrop/stream-assert.svg?style=flat |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6917
135
83
7
4
1