| 1 |
var baseClone = require('./_baseClone'), |
| 2 |
baseIteratee = require('./_baseIteratee'); |
| 3 |
|
| 4 |
/** Used to compose bitmasks for cloning. */ |
| 5 |
var CLONE_DEEP_FLAG = 1; |
| 6 |
|
| 7 |
/** |
| 8 |
* Creates a function that invokes `func` with the arguments of the created |
| 9 |
* function. If `func` is a property name, the created function returns the |
| 10 |
* property value for a given element. If `func` is an array or object, the |
| 11 |
* created function returns `true` for elements that contain the equivalent |
| 12 |
* source properties, otherwise it returns `false`. |
| 13 |
* |
| 14 |
* @static |
| 15 |
* @since 4.0.0 |
| 16 |
* @memberOf _ |
| 17 |
* @category Util |
| 18 |
* @param {*} [func=_.identity] The value to convert to a callback. |
| 19 |
* @returns {Function} Returns the callback. |
| 20 |
* @example |
| 21 |
* |
| 22 |
* var users = [ |
| 23 |
* { 'user': 'barney', 'age': 36, 'active': true }, |
| 24 |
* { 'user': 'fred', 'age': 40, 'active': false } |
| 25 |
* ]; |
| 26 |
* |
| 27 |
* // The `_.matches` iteratee shorthand. |
| 28 |
* _.filter(users, _.iteratee({ 'user': 'barney', 'active': true })); |
| 29 |
* // => [{ 'user': 'barney', 'age': 36, 'active': true }] |
| 30 |
* |
| 31 |
* // The `_.matchesProperty` iteratee shorthand. |
| 32 |
* _.filter(users, _.iteratee(['user', 'fred'])); |
| 33 |
* // => [{ 'user': 'fred', 'age': 40 }] |
| 34 |
* |
| 35 |
* // The `_.property` iteratee shorthand. |
| 36 |
* _.map(users, _.iteratee('user')); |
| 37 |
* // => ['barney', 'fred'] |
| 38 |
* |
| 39 |
* // Create custom iteratee shorthands. |
| 40 |
* _.iteratee = _.wrap(_.iteratee, function(iteratee, func) { |
| 41 |
* return !_.isRegExp(func) ? iteratee(func) : function(string) { |
| 42 |
* return func.test(string); |
| 43 |
* }; |
| 44 |
* }); |
| 45 |
* |
| 46 |
* _.filter(['abc', 'def'], /ef/); |
| 47 |
* // => ['def'] |
| 48 |
*/ |
| 49 |
function iteratee(func) { |
| 50 |
return baseIteratee(typeof func == 'function' ? func : baseClone(func, CLONE_DEEP_FLAG)); |
| 51 |
} |
| 52 |
|
| 53 |
module.exports = iteratee; |