From e1806c890eadd0e83c4af8512feee44621b13ac1 Mon Sep 17 00:00:00 2001 From: Alvis HT Tang Date: Thu, 22 Mar 2018 23:48:26 +0000 Subject: [PATCH] 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 --- types/highland/index.d.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types/highland/index.d.ts b/types/highland/index.d.ts index eff8fb2769..473e93880a 100644 --- a/types/highland/index.d.ts +++ b/types/highland/index.d.ts @@ -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(x: R | Highland.Nil): x is Highland.Nil; + /** * Returns true if `x` is a Highland Stream. *