You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
559 B
30 lines
559 B
/*!
|
|
* array-last <https://github.com/jonschlinkert/array-last>
|
|
*
|
|
* Copyright (c) 2014-2017, Jon Schlinkert.
|
|
* Released under the MIT License.
|
|
*/
|
|
|
|
var isNumber = require('is-number');
|
|
|
|
module.exports = function last(arr, n) {
|
|
if (!Array.isArray(arr)) {
|
|
throw new Error('expected the first argument to be an array');
|
|
}
|
|
|
|
var len = arr.length;
|
|
if (len === 0) {
|
|
return null;
|
|
}
|
|
|
|
n = isNumber(n) ? +n : 1;
|
|
if (n === 1) {
|
|
return arr[len - 1];
|
|
}
|
|
|
|
var res = new Array(n);
|
|
while (n--) {
|
|
res[n] = arr[--len];
|
|
}
|
|
return res;
|
|
};
|
|
|