Skip to content

Commit

Permalink
make first() and last() work for objects as well as lists
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed May 22, 2013
1 parent 87f4c86 commit c37766e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 10 additions & 2 deletions first.js
@@ -1,3 +1,11 @@
module.exports = function first(arr) {
return arr ? arr[0] : null
var isList = require('./isList')
var keys = require('./keys')

module.exports = function first(obj) {
if (!obj) { return null }
if (isList(obj)) {
return obj[0]
} else {
return obj[keys(obj)[0]]
}
}
15 changes: 10 additions & 5 deletions last.js
@@ -1,6 +1,11 @@
var isArray = require('./isArray')
var isList = require('./isList')
var keys = require('./keys')

module.exports = function(arr) {
if (!isArray(arr)) { return null }
return arr[arr.length - 1]
}
module.exports = function(obj) {
if (!obj) { return null }
if (isList(obj)) {
return obj[obj.length - 1]
} else {
return obj[last(keys(obj))]
}
}

0 comments on commit c37766e

Please sign in to comment.