Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 488f9f1

Browse files
hmch17Kent C. Dodds
authored andcommitted
feat(find): a function that finds the first element that matches the predicate in an array (#134)
This method will iterate over elements of an array, returning the first element that the given predicate returns truthy for. Closes #133
1 parent 9fca9c7 commit 488f9f1

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/find.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default find
2+
3+
/**
4+
* This method will iterate over elements of an array, returning the first element
5+
* that the given predicate returns truthy for.
6+
* Similar to _.find in LoDash https://lodash.com/docs/4.17.4#find
7+
*
8+
* @param {Array} array - Input array of objects
9+
* @param {Function} predicate - A function that iterates over the elements in `array`
10+
* @return {Object} - First element that matches the predicate
11+
*/
12+
13+
function find(array, predicate) {
14+
if (!(array instanceof Array)) {
15+
return undefined
16+
}
17+
18+
let foundElement
19+
20+
for (let i = 0; i < array.length; i++) {
21+
if (predicate(array[i])) {
22+
foundElement = array[i]
23+
return foundElement
24+
}
25+
}
26+
27+
return undefined
28+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ import range from './range'
4747
import contains from './contains'
4848
import getOrdinalSuffix from './get-ordinal-suffix'
4949
import arrayAverage from './array-average'
50+
import find from './find'
5051

5152
export {
5253
isOdd,
5354
isEven,
5455
revstring,
5556
initArray,
5657
reduce,
58+
find,
5759
flatten,
5860
snakeToCamel,
5961
getQueryStringParam,

test/find.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava'
2+
import { find } from '../src'
3+
4+
test('find returns undefined if the input is not an instance of Array', t => {
5+
const original = { prop: 'test' }
6+
const predicate = element => 'does nothing'
7+
const expected = undefined
8+
const actual = find(original, element => predicate(element))
9+
t.deepEqual(actual, expected)
10+
})
11+
12+
test('find returns the first element that matches the given predicate', t => {
13+
const original = [{ a: 1, b: 2 }, { c: 3 }, { a: 1, c: 4 }]
14+
const predicate = element => element.a === 1
15+
const expected = { a: 1, b: 2 }
16+
const actual = find(original, element => predicate(element))
17+
t.deepEqual(actual, expected)
18+
})
19+
20+
test('find returns undefined if nothing matches the given predicate', t => {
21+
const original = [{ a: 1, b: 2 }, { c: 3 }, { a: 1, c: 4 }]
22+
const predicate = element => element.d === 1
23+
const expected = undefined
24+
const actual = find(original, element => predicate(element))
25+
t.deepEqual(actual, expected)
26+
})

0 commit comments

Comments
 (0)