Socket
Socket
Sign inDemoInstall

stream-assert

Package Overview
Dependencies
7
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.0 to 1.0.0

77

lib/assert.js
var through = require('through2');
var deepEqual = require('deep-equal');
var inspect = require('util').inspect;
var assert = {};
assert.nth = function (n, expected) {
assert.nth = function (n, assertion) {
var i = 0;
return through.obj(function (obj, enc, cb) {
if (i === n && !deepEqual(obj, expected)) {
this.emit('end',
'Expected to see ' + inspect(expected) +
' on ' + n + ' position, but got ' + inspect(obj));
this.emit('close');
if (i === n) {
try {
assertion(obj);
} catch (err) {
this.emit('end', 'Element on ' + n + ' position is not passing assertion:\n' + err.message);
this.emit('close');
}
}

@@ -23,30 +23,42 @@ i++;

assert.first = function (expected) {
return assert.nth(0, expected);
assert.first = function (assertion) {
return assert.nth(0, assertion);
};
assert.second = function (expected) {
return assert.nth(1, expected);
assert.second = function (assertion) {
return assert.nth(1, assertion);
};
assert.contains = function (expected) {
var found = false;
assert.all = function (assertion) {
var i = 0;
return through.obj(function (obj, enc, cb) {
if (deepEqual(obj, expected)) {
found = true;
try {
assertion(obj);
} catch (err) {
this.emit('end', 'Element on ' + i + ' position is not passing assertion:\n' + err.message);
this.emit('close');
}
i++;
cb(null, obj);
}).on('finish', function () {
if (!found) {
this.emit('end', new Error(
'Object ' + inspect(expected) +
' was not found in stream'
));
} else {
this.emit('end');
this.emit('end');
});
};
assert.any = function (assertion) {
var matched = false;
return through.obj(function (obj, enc, cb) {
try {
assertion(obj);
matched = true;
} catch (err) { }
cb(null, obj);
}).on('finish', function () {
if (!matched) {
return this.emit('end', 'Not found any element in stream, that pass assertion');
}
this.emit('end');
});
};
assert.length = function (expected) {

@@ -58,9 +70,14 @@ var i = 0;

}).on('finish', function () {
if (i !== expected) {
this.emit('end', new Error(
'Expected stream length is ' + inspect(expected) +
', but got ' + i
));
} else {
var assertion = expected;
if (typeof expected !== 'function') {
assertion = function (data) {
if (data !== expected) throw new Error('not ' + data + ' equal ' + expected);
}
}
try {
assertion(i);
this.emit('end');
} catch (err) {
this.emit('end', new Error( 'Stream length ' + err.message ));
}

@@ -67,0 +84,0 @@ });

{
"name": "stream-assert",
"version": "0.2.0",
"version": "1.0.0",
"description": "Assertion library for streams",

@@ -29,5 +29,4 @@ "main": "index.js",

"dependencies": {
"deep-equal": "^0.2.1",
"through2": "^0.6.1"
}
}

@@ -5,2 +5,6 @@ # stream-assert [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]

### Assertion library wanted
`assert.first(function(data) { should.eql(1); })` could be `assert.first(should.eql(1))` if only `should` return assert function.
## Usage

@@ -11,7 +15,8 @@

var assert = require('stream-assert');
var should = require('should');
array([1, 2, 3])
.pipe(assert.first(1))
.pipe(assert.second(2))
.pipe(assert.nth(2, 3))
.pipe(assert.first(function(data) { should.eql(1); }))
.pipe(assert.second(function(data) { should.eql(2); }))
.pipe(assert.nth(2, function(data) { should.eql(3); }))
.pipe(assert.length(1))

@@ -25,12 +30,12 @@ .on('end', console.log);

Builder for asserting stream. Constructed stream will emit `error` and `end` on wrong assertion about stream with assertion error as first argument.
Builder for asserting stream. Constructed stream will emit `end` on wrong assertion about stream with assertion error as first argument.
#### nth(n, obj)
#### nth(n, assertion)
Verify, that `n` object in stream is deepEqual to `obj`.
Calls `assertion` function on `nth` element in stream.
#### first(obj)
#### first(Function)
> alias to nth(0, obj)
#### second(obj)
#### second(Function)
> alias to nth(1, obj)

@@ -42,6 +47,10 @@

#### contains(obj)
#### all(assertion)
Searches object in stream that deepEqual to `obj`.
Checking that all elements in stream pass assertion function.
#### any(assertion)
Checking that at least one of elements in stream pass assertion function.
## License

@@ -48,0 +57,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc