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.

27 lines
562 B

4 years ago
  1. 'use strict';
  2. var _require = require('../helpers/is'),
  3. isFunction = _require.isFunction;
  4. module.exports = function last(fn, defaultValue) {
  5. var items = this.items;
  6. if (isFunction(fn)) {
  7. items = this.filter(fn).all();
  8. }
  9. if (Array.isArray(items) && !items.length || !Object.keys(items).length) {
  10. if (isFunction(defaultValue)) {
  11. return defaultValue();
  12. }
  13. return defaultValue;
  14. }
  15. if (Array.isArray(items)) {
  16. return items[items.length - 1];
  17. }
  18. var keys = Object.keys(items);
  19. return items[keys[keys.length - 1]];
  20. };