search.js 861 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. /* eslint-disable eqeqeq */
  3. var _require = require('../helpers/is'),
  4. isArray = _require.isArray,
  5. isObject = _require.isObject,
  6. isFunction = _require.isFunction;
  7. module.exports = function search(valueOrFunction, strict) {
  8. var _this = this;
  9. var result;
  10. var find = function find(item, key) {
  11. if (isFunction(valueOrFunction)) {
  12. return valueOrFunction(_this.items[key], key);
  13. }
  14. if (strict) {
  15. return _this.items[key] === valueOrFunction;
  16. }
  17. return _this.items[key] == valueOrFunction;
  18. };
  19. if (isArray(this.items)) {
  20. result = this.items.findIndex(find);
  21. } else if (isObject(this.items)) {
  22. result = Object.keys(this.items).find(function (key) {
  23. return find(_this.items[key], key);
  24. });
  25. }
  26. if (result === undefined || result < 0) {
  27. return false;
  28. }
  29. return result;
  30. };