last.js 384 B

123456789101112
  1. var rest = require('./rest.js');
  2. // Get the last element of an array. Passing **n** will return the last N
  3. // values in the array.
  4. function last(array, n, guard) {
  5. if (array == null || array.length < 1) return n == null || guard ? void 0 : [];
  6. if (n == null || guard) return array[array.length - 1];
  7. return rest(array, Math.max(0, array.length - n));
  8. }
  9. module.exports = last;