How to filter a stream using short syntax, when the values are primitives

95 Views Asked by At

When I have a stream of objects, e.g.

 values = Bacon.fromArray([ {'status':"active"},  {'status':"inactive"} ]);

I can apply a short form filter, like this:

 activeStates = values.filter( ".status", "active" );

But when the original stream contains only primitives:

 states = Bacon.fromArray( ["active", "inactive" ] );

Then I can no longer use that form, and I have to write a function literal. I have tried using ".", "", null, for the property name argument but that doesn't work.

I can define a function like this:

    function equals( val ){
        return function( obj ){
            return obj === val;
        }
    }

And then write:

activeStates = values.filter( equals( "active" ) );

Is this the best I can do? Or is there either a Bacon function that I'm missing, or a common lodash function that saves me from writing my own equals function?

1

There are 1 best solutions below

0
On BEST ANSWER

You can use the Bacon.matchers library to implement such filters.

Bacon.fromArray( ["active", "inactive" ] )
  .where().equalTo("active")
  .onValue(function(val) {
      document.body.innerHTML += val
  })

JsFiddle