jmorrell / backbone.obscura

A read-only proxy of a Backbone.Collection that can be filtered, sorted, and paginated.
http://jmorrell.github.io/backbone.obscura/
MIT License
107 stars 19 forks source link

add test for filterBy Array #4

Closed mulderp closed 11 years ago

mulderp commented 11 years ago

Add test for filter by Array, addresses: https://github.com/jmorrell/backbone.obscura/issues/3

jmorrell commented 11 years ago

Not sure I understand the need for this added test. The group of tests below it:

https://github.com/mulderp/backbone.obscura/blob/dd19e8191751f83202ae234a067fb5587d2921b8/test/filtered.js#L143

test the case where you have a filter function on a specific key's value.

Nothing against merging this in (more tests are almost never a bad thing) but can you help me understand the motivation?

mulderp commented 11 years ago

I was looking for documentation in the tests actually. The use case I was looking for that sometimes we need to filter "inclusive" not exclusive, e.g. Imagine buying a T-Shirt, we might like to filter for a size range "medium" to "large". So, we just check whether elements are part in a range. What might be interesting for documentation would be, filtering for a range is possible with:

 _.contains(['medium','large'], val)
mulderp commented 11 years ago

But I agree the location of the tests is not right, and maybe it would go best under documentation directly.

jmorrell commented 11 years ago

Is there something unclear about the existing documentation?

https://github.com/jmorrell/backbone.obscura#proxyfilterbyfiltername-filter

ex:

filtered.filterBy('a > 2', { a: function(val) { 
  return val > 2;
}});

shows that you can use arbitrary function predicates.

Would more examples make it clearer? Your case of filtering if it's included in a set is certainly a common case.

mulderp commented 11 years ago

My confusion was that I didn't realize that

    _.contains(range, val)

is a predicate. I jumped into fiddling with a function(range, val) { ... } first. And realized that this is a use case of {a: predicate(x) }.

jmorrell commented 11 years ago

Understandable. I'll add the _.contains example to the docs.