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.

36 lines
772 B

4 years ago
  1. 'use strict';
  2. var _require = require('../helpers/is'),
  3. isFunction = _require.isFunction;
  4. module.exports = function first(fn, defaultValue) {
  5. if (isFunction(fn)) {
  6. for (var i = 0, length = this.items.length; i < length; i += 1) {
  7. var item = this.items[i];
  8. if (fn(item)) {
  9. return item;
  10. }
  11. }
  12. if (isFunction(defaultValue)) {
  13. return defaultValue();
  14. }
  15. return defaultValue;
  16. }
  17. if (Array.isArray(this.items) && this.items.length || Object.keys(this.items).length) {
  18. if (Array.isArray(this.items)) {
  19. return this.items[0];
  20. }
  21. var firstKey = Object.keys(this.items)[0];
  22. return this.items[firstKey];
  23. }
  24. if (isFunction(defaultValue)) {
  25. return defaultValue();
  26. }
  27. return defaultValue;
  28. };