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.

43 lines
997 B

4 years ago
  1. 'use strict';
  2. var _require = require('../helpers/is'),
  3. isFunction = _require.isFunction;
  4. module.exports = function search(valueOrFunction, strict) {
  5. var _this = this;
  6. var valueFn = valueOrFunction;
  7. if (isFunction(valueOrFunction)) {
  8. valueFn = this.items.find(function (value, key) {
  9. return valueOrFunction(value, key);
  10. });
  11. }
  12. var index = false;
  13. if (Array.isArray(this.items)) {
  14. var itemKey = this.items.filter(function (item) {
  15. if (strict === true) {
  16. return item === valueFn;
  17. }
  18. return item === Number(valueFn) || item === String(valueFn);
  19. })[0];
  20. index = this.items.indexOf(itemKey);
  21. } else {
  22. return Object.keys(this.items).filter(function (prop) {
  23. if (strict === true) {
  24. return _this.items[prop] === valueFn;
  25. }
  26. return _this.items[prop] === Number(valueFn) || _this.items[prop] === valueFn.toString();
  27. })[0] || false;
  28. }
  29. if (index === -1) {
  30. return false;
  31. }
  32. return index;
  33. };