kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

Cannot find a documented way to test if an object is a Kefir property or stream. #9

Closed beckyconning closed 10 years ago

beckyconning commented 10 years ago

Is there a way to do something like the following?

var x = Kefir.constant(1);
var y = Kefir.never();

Kefir.isProperty(x); // returns: true
Kefir.isStream(y);  // returns true

Kefir.isProperty(y); // returns: false
Kefir.isStream(x); // returns: false

Kefir.isProperty({}); // returns: false
Kefir.isStream({}); // returns: false

If this worked regardless of which instance of Kefir the stream of property comes from I think that would be really useful for testing.

What I am doing at the moment is testing the .__proto__._name of the stream and or property.

rpominov commented 10 years ago

Sure, you can do this using the instanceof operator. All three base classes are exposed as Kefir.Observable, Kefir.Stream, and Kefir.Property.

Here is an example:

var x = Kefir.constant(1);
var y = Kefir.never();

x instanceof Kefir.Stream // false
y instanceof Kefir.Stream // true

x instanceof Kefir.Property // true
y instanceof Kefir.Property // false

x instanceof Kefir.Observable // true
y instanceof Kefir.Observable // true
beckyconning commented 10 years ago

Thanks, I changed the way tests are loaded so our app and tests both share the same instance of Kefir so that I could use this approach.

rpominov commented 10 years ago

Ah, I didn't realize you need it to work with different instances of Kefir. Yeah, using the same instance is preferable solution here. I am glad it worked out :)