highland: add the definition for the new isNil() for type guarding

This commit add the definition for the a very simple, and yet essential, isNil
function for type checking. This function is particularly useful in a consume
handler.

Example:
```
source.consume(function (err, x, push, next) {
  if (err) {
    // pass errors along the stream and consume next value
    push(err);
    next();
  } else if (_.isNil(x)) { // type guard is only possible with this function
    // pass nil (end event) along the stream
    push(null, x);
  } else {
    // pass on the value only if the value passes the predicate
    if (f(x)) {
      push(null, x);
    }
    next();
  }
});
```

See https://github.com/caolan/highland/pull/645
This commit is contained in:
Alvis HT Tang
2018-03-22 23:48:26 +00:00
parent 81db85e6ee
commit e1806c890e

View File

@@ -89,6 +89,17 @@ interface HighlandStatic {
// UTILS
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Returns true if `x` is the end of stream marker.
*
* @id isNil
* @section Streams
* @name _.isNil(x)
* @param x - the object to test
* @api public
*/
isNil<R>(x: R | Highland.Nil): x is Highland.Nil;
/**
* Returns true if `x` is a Highland Stream.
*