Skip to content

Commit

Permalink
"head" returns undefined if source is empty or not array
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Jan 2, 2019
1 parent 688487a commit cdf3226
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/head/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* head([1, 2, 3])
* // => 1
* head([])
* // => null
* // => undefined
*/
module.exports = source => (source.length === 0 ? null : source[0])
module.exports = source =>
Array.isArray(source) && source.length !== 0 ? source[0] : undefined
10 changes: 6 additions & 4 deletions src/head/head.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ const head = require("./head")
* head([1, 2, 3])
* // => 1
* head([])
* // => null
* // => undefined
*/
test("array::head", t => {
t.equal(head([1, 2, 3]), 1, "Get first element of number array")

t.equal(head([]), null, "Get first element of empty array")
t.equal(head([1, 2, 3]), 1, "From number array should return first element")
t.equal(head([]), undefined, "From empty array should return undefined")
t.equal(head(2), undefined, "From number should return undefined")
t.equal(head({}), undefined, "From object should return undefined")
t.equal(head(() => {}), undefined, "From function should return undefined")

t.end()
})

0 comments on commit cdf3226

Please sign in to comment.