index.js 559 B

123456789101112131415161718192021222324252627282930
  1. /*!
  2. * array-last <https://github.com/jonschlinkert/array-last>
  3. *
  4. * Copyright (c) 2014-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. var isNumber = require('is-number');
  8. module.exports = function last(arr, n) {
  9. if (!Array.isArray(arr)) {
  10. throw new Error('expected the first argument to be an array');
  11. }
  12. var len = arr.length;
  13. if (len === 0) {
  14. return null;
  15. }
  16. n = isNumber(n) ? +n : 1;
  17. if (n === 1) {
  18. return arr[len - 1];
  19. }
  20. var res = new Array(n);
  21. while (n--) {
  22. res[n] = arr[--len];
  23. }
  24. return res;
  25. };