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

Proposal: instanceOf operator #3473

Closed
JohnYoungers opened this issue Mar 25, 2018 · 3 comments
Closed

Proposal: instanceOf operator #3473

JohnYoungers opened this issue Mar 25, 2018 · 3 comments

Comments

@JohnYoungers
Copy link

I've run into several situations where it would be nice to have an 'instanceOf' like operator (as have others it looks like: #2340, #3125).

I think it could be done relatively easily by adding something like this to src/internal/operators/filter.ts:

export function instanceOf<T, S extends T>(type: { new(...args: any[]): S }): OperatorFunction<T, S> {
  return function filterOperatorFunction(source: Observable<T>): Observable<S> {
    return source.lift(new FilterOperator(val => val instanceof type));
  };
}

If it belongs in a separate file I think you'd just need to expose the filter internals (or duplicate).

With it you can then do:

this.router.events.pipe(
  instanceOf(NavigationEnd),
  tap((e) => /* e is typed as NavigationEnd */)
);

It may make sense to allow for multiple types (instanceOf(NavigationStart, NavigationEnd)), but I'm not 100% sure what the signature would look like

@kwonoj
Copy link
Member

kwonoj commented Mar 26, 2018

As introduction of pipeable operator we do recommend custom operator in user level. If you think this should be included in core operator, fell freely reopen / create new issue includes examples can't be achieved via user level custom operator.

@kwonoj kwonoj closed this as completed Mar 26, 2018
@JohnYoungers
Copy link
Author

Fair enough. I've copied the code from filter.ts to create the full operator in a gist for anyone else who stumbles upon this:

https://gist.github.com/jayoungers/63bf0bce77d28e448a41f5bb4c531264

@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants