Skip to content

Commit

Permalink
Merge pull request #520 from svozza/fromError-of
Browse files Browse the repository at this point in the history
add _.of and _.fromError. Resolves #519.
  • Loading branch information
vqvu committed Aug 6, 2016
2 parents 5ed1baf + 74e0adc commit 6405e6e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ This file does not aim to be comprehensive (you have git history for that),
rather it lists changes that might impact your own code as a consumer of
this library.

2.10.0
------
### New additions
* `of`: Creates a stream that sends a single value then ends.
[#520](https://github.com/caolan/highland/pull/520).
* `fromError`: Creates a stream that sends a single error then ends.
[#520](https://github.com/caolan/highland/pull/520).


2.9.0
-----
### New additions
Expand Down
35 changes: 35 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,41 @@ function Stream(/*optional*/xs, /*optional*/secondArg, /*optional*/mappingHint)
}
inherits(Stream, EventEmitter);

/**
* Creates a stream that sends a single value then ends.
*
* @id of
* @name _.of(x)
* @param x - the value to send
* @section Utils
*
* _.of(1).toArray(_.log); // => [1]
*/

_.of = function (x) {
return _([x]);
};

/**
* Creates a stream that sends a single error then ends.
*
* @id fromError
* @name _.fromError(err)
* @param error - the error to send
* @section Utils
*
* _.fromError(new Error('Single Error')).toCallback(function (err, result) {
* // err contains Error('Single Error') object
* }
*/

_.fromError = function (error) {
return _(function (push) {
push(error);
push(null, _.nil);
});
};

/**
* adds a top-level _.foo(mystream) style export for Stream methods
*/
Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,31 @@ exports['generator throws error if push called after nil'] = function (test) {
test.done();
};

exports.of = {
'creates stream of one item': function (test) {
test.expect(2);
_.of(1)
.toCallback(function (err, result) {
test.ifError(err);
test.same(result, 1);
test.done();
});
}
};

exports.fromError = {
'creates stream of one error': function (test) {
var error = new Error('This is an error');
test.expect(2);
_.fromError(error)
.toCallback(function (err, result) {
test.strictEqual(err, error);
test.strictEqual(result, void 0);
test.done();
});
}
};

exports['consume - throws error if push called after nil'] = function (test) {
test.expect(1);
var s = _([1, 2, 3]);
Expand Down

0 comments on commit 6405e6e

Please sign in to comment.