forked from tjmehta/101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
last.js
29 lines (26 loc) · 758 Bytes
/
last.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @module 101/last
*/
var isObject = require('./is-object');
var exists = require('./exists');
var isFunction = require('./is-function');
/**
* Returns the last value of the item.
* @function module:101/last
* @param {array|string|object} item - item whose last value is returned
* @return {*} Last value of an array. Last char of a string. Last value of an object. Last char of item.toString() for everything else.
*/
module.exports = last;
function last (item) {
var val;
if (exists(item && item.length) && !isFunction(item)) {
val = item[item.length - 1];
}
else if (isObject(item)) {
val = item[Object.keys(item).pop()];
}
else {
val = (item && item.toString) ? last(item.toString()) : undefined;
}
return val;
}