Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make first() and last() work for objects as well as lists
- Loading branch information
1 parent
87f4c86
commit c37766e
Showing
2 changed files
with
20 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))] | ||
} | ||
} |