UNPKG

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