Skip to content

Commit

Permalink
implement find (alias: filter) and findOne
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Keil committed Sep 22, 2015
1 parent 26d5077 commit 463779f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Function collection are also exported by their uppercased initial letter.
- reverse
- each
- map
- filter
- filter / find
- findOne
- shuffle
- every
- some
Expand Down
7 changes: 6 additions & 1 deletion src/array.ls
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ exports.map = curry (f, xs) ->
[f x, i for x, i in xs]

# filter :: function -> array -> array
exports.filter = curry (f, xs) ->
exports.find = exports.filter = curry (f, xs) ->
[x for x, i in xs when f x, i]

# find :: elem -> array -> elem | undefined
exports.findOne = curry (f, xs) !->
for x, i in xs when f x, i
return x

# shuffle :: array -> array
exports.shuffle = (xs) ->
result = new Array xs.length
Expand Down
51 changes: 42 additions & 9 deletions test/array.spec.ls
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{
empty, has, includes, clone, head, first, tail, last, initial, slice,
concat, remove, removeOne, flatten, each, map, filter, shuffle,
concat, remove, removeOne, flatten, each, map, find, findOne, shuffle,
every, some, reverse, zip, zipWith, partition, unique, uniqueBy,
difference, intersection, union, sortBy, countBy, groupBy, splitAt,
index, indices, findIndex, findIndices
Expand Down Expand Up @@ -180,32 +180,65 @@ suite 'map()' !->
map (-> it * 2), input
deepEqual input, [1 2 3]

suite 'filter()' !->
suite 'find()' !->
test 'curries' !->
isFunction filter (->)
isArray filter (->), [1 2 3]
isFunction find (->)
isArray find (->), [1 2 3]

test 'iterates over the complete array' !->
count = 0
filter (-> ++count), [1 2 3]
find (-> ++count), [1 2 3]
strictEqual count, 3

test 'iterator receives index and value' !->
['foo' 'bar' 'qaz'] |> filter (value, index) ->
['foo' 'bar' 'qaz'] |> find (value, index) ->
isString value
isNumber index
if index is 0 then strictEqual value, 'foo'
if index is 1 then strictEqual value, 'bar'
if index is 2 then strictEqual value, 'qaz'

test 'filters values' !->
test 'finds values' !->
deepEqual do
filter (-> typeof it isnt 'string'), [1 'foo' 2]
find (-> typeof it isnt 'string'), [1 'foo' 2]
[1 2]

test 'does not mutate input' !->
input = [1 2 3]
filter (-> it is 2), input
find (-> it is 2), input
deepEqual input, [1 2 3]

suite 'findOne()' !->
test 'curries' !->
isFunction findOne (->)
isNumber findOne (-> it is 2), [1 2 3]

test 'iterates over the complete array' !->
count = 0
findOne (!-> ++count), [1 2 3]
strictEqual count, 3

test 'iterator receives index and value' !->
['foo' 'bar' 'qaz'] |> findOne (value, index) ->
isString value
isNumber index
if index is 0 then strictEqual value, 'foo'
if index is 1 then strictEqual value, 'bar'
if index is 2 then strictEqual value, 'qaz'

test 'finds and returns the first value' !->
strictEqual do
findOne (-> typeof it is 'string'), [1 'foo' 2 'bar']
'foo'

test 'returns undefined when nothing is found' !->
strictEqual do
findOne (-> typeof it is 'string'), [1 2 3 4]
undefined

test 'does not mutate input' !->
input = [1 2 3]
findOne (-> it is 2), input
deepEqual input, [1 2 3]

suite 'shuffle()' !->
Expand Down

0 comments on commit 463779f

Please sign in to comment.