Permalink
Browse files

make first() and last() work for objects as well as lists

  • Loading branch information...
marcuswestin committed May 22, 2013
1 parent 87f4c86 commit c37766e08ad015a81d3a45a2a3786806d946f777
Showing with 20 additions and 7 deletions.
  1. +10 −2 first.js
  2. +10 −5 last.js
View
@@ -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]]
+ }
}
View
15 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.