Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added Collection#detect. #33

Closed
wants to merge 2 commits into from

1 participant

@gma

I just found myself needing this, and it was so easy to write I thought I'd just do it…

@gma

Okay, cancel that. I just spotted Collection#find with exactly the same implementation…

@gma gma closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2012
  1. @gma

    Added test for Collection#select.

    gma authored
  2. @gma

    Added Collection#detect.

    gma authored
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +10 −0 spec/collection.spec.coffee
  2. +2 −0  src/collection.coffee
View
10 spec/collection.spec.coffee
@@ -101,3 +101,13 @@ describe 'Serenade.Collection', ->
expect(@collection.get(0)).toEqual('a')
expect(@collection.get(1)).toEqual('c')
expect(@collection.get(2)).toEqual(undefined)
+
+ describe '#select', ->
+ it 'returns items that match user specified criteria', ->
+ selected = @collection.select (item) -> item < 'c'
+ expect(selected).toEqual(['a', 'b'])
+
+ describe '#detect', ->
+ it 'returns the first item that matches user specified criteria', ->
+ detected = @collection.detect (item) -> item > 'a'
+ expect(detected).toEqual('b')
View
2  src/collection.coffee
@@ -48,6 +48,8 @@ class exports.Collection
serializeObject(@list)
select: (fun) ->
item for item in @list when fun(item)
+ detect: (fun) ->
+ return item for item in @list when fun(item)
_in: (item) ->
if item?._triggerChangesTo
Something went wrong with that request. Please try again.