Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isNil for type checking #645

Merged
merged 2 commits into from
Mar 14, 2018
Merged

feat: add isNil for type checking #645

merged 2 commits into from
Mar 14, 2018

Conversation

alvis
Copy link
Contributor

@alvis alvis commented Mar 13, 2018

This PR provides a very simple, and yet essential, function for type checking, as
_.nil is not a primitive variable but an object. This function is particularly
useful in a consume handler which has a signature of consume<U>(f: (err: Error, x: R | Highland.Nil, push: (err: Error | null, value?: U | Highland.Nil) => void, next: () => void) => void): Stream<U>. Only with this function and an
updated typescript or flow definition, x can be distinguished between R and Highland.Nil.

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();
  }
});

Note: This PR also comes with a simple test.

@vqvu
Copy link
Collaborator

vqvu commented Mar 14, 2018

Do you need this in the 2.x branch or the 3.x branch?

If you're going off the currently published documentation, that's the 2.x branch, so you'll want to rebase this onto caolan:2.x instead of caolan:master.

@alvis alvis changed the base branch from master to 2.x March 14, 2018 07:39
This provides a very simple, and yet essential, function for type checking, as
_.nil is not a primitive variable but an object. This function is particularly
useful in a consume handler which has a signature of `consume<U>(f: (err: Error,
x: R | Highland.Nil, push: (err: Error | null, value?: U | Highland.Nil) =>
void, next: () => void) => void): Stream<U>`. Only with this function and an
updated typescript or flow definition, x can be distinguished between R and
Highland.Nil.

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();
  }
});
```
@alvis
Copy link
Contributor Author

alvis commented Mar 14, 2018

Thanks for pointing out. I'm using 2.x, so it should merge with the 2.x branch.

I've since changed the target branch.

@vqvu vqvu merged commit 2e5cd30 into caolan:2.x Mar 14, 2018
@vqvu
Copy link
Collaborator

vqvu commented Mar 21, 2018

I've released this in 2.13.0

@alvis
Copy link
Contributor Author

alvis commented Mar 22, 2018

Thank you.

alvis added a commit to alvis/DefinitelyTyped that referenced this pull request Mar 22, 2018
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 caolan/highland#645
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants