some.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var arraySome = require('../internal/arraySome'),
  2. baseCallback = require('../internal/baseCallback'),
  3. baseSome = require('../internal/baseSome'),
  4. isArray = require('../lang/isArray'),
  5. isIterateeCall = require('../internal/isIterateeCall');
  6. /**
  7. * Checks if `predicate` returns truthy for **any** element of `collection`.
  8. * The function returns as soon as it finds a passing value and does not iterate
  9. * over the entire collection. The predicate is bound to `thisArg` and invoked
  10. * with three arguments: (value, index|key, collection).
  11. *
  12. * If a property name is provided for `predicate` the created `_.property`
  13. * style callback returns the property value of the given element.
  14. *
  15. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  16. * style callback returns `true` for elements that have a matching property
  17. * value, else `false`.
  18. *
  19. * If an object is provided for `predicate` the created `_.matches` style
  20. * callback returns `true` for elements that have the properties of the given
  21. * object, else `false`.
  22. *
  23. * @static
  24. * @memberOf _
  25. * @alias any
  26. * @category Collection
  27. * @param {Array|Object|string} collection The collection to iterate over.
  28. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  29. * per iteration.
  30. * @param {*} [thisArg] The `this` binding of `predicate`.
  31. * @returns {boolean} Returns `true` if any element passes the predicate check,
  32. * else `false`.
  33. * @example
  34. *
  35. * _.some([null, 0, 'yes', false], Boolean);
  36. * // => true
  37. *
  38. * var users = [
  39. * { 'user': 'barney', 'active': true },
  40. * { 'user': 'fred', 'active': false }
  41. * ];
  42. *
  43. * // using the `_.matches` callback shorthand
  44. * _.some(users, { 'user': 'barney', 'active': false });
  45. * // => false
  46. *
  47. * // using the `_.matchesProperty` callback shorthand
  48. * _.some(users, 'active', false);
  49. * // => true
  50. *
  51. * // using the `_.property` callback shorthand
  52. * _.some(users, 'active');
  53. * // => true
  54. */
  55. function some(collection, predicate, thisArg) {
  56. var func = isArray(collection) ? arraySome : baseSome;
  57. if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
  58. predicate = undefined;
  59. }
  60. if (typeof predicate != 'function' || thisArg !== undefined) {
  61. predicate = baseCallback(predicate, thisArg, 3);
  62. }
  63. return func(collection, predicate);
  64. }
  65. module.exports = some;