Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseCallback = require('../internal/baseCallback'),
2
isIterateeCall = require('../internal/isIterateeCall'),
3
isObjectLike = require('../internal/isObjectLike'),
4
matches = require('./matches');
5
6
/**
7
* Creates a function that invokes `func` with the `this` binding of `thisArg`
8
* and arguments of the created function. If `func` is a property name the
9
* created callback returns the property value for a given element. If `func`
10
* is an object the created callback returns `true` for elements that contain
11
* the equivalent object properties, otherwise it returns `false`.
12
*
13
* @static
14
* @memberOf _
15
* @alias iteratee
16
* @category Utility
17
* @param {*} [func=_.identity] The value to convert to a callback.
18
* @param {*} [thisArg] The `this` binding of `func`.
19
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
20
* @returns {Function} Returns the callback.
21
* @example
22
*
23
* var users = [
24
* { 'user': 'barney', 'age': 36 },
25
* { 'user': 'fred', 'age': 40 }
26
* ];
27
*
28
* // wrap to create custom callback shorthands
29
* _.callback = _.wrap(_.callback, function(callback, func, thisArg) {
30
* var match = /^(.+?)__([gl]t)(.+)$/.exec(func);
31
* if (!match) {
32
* return callback(func, thisArg);
33
* }
34
* return function(object) {
35
* return match[2] == 'gt'
36
* ? object[match[1]] > match[3]
37
* : object[match[1]] < match[3];
38
* };
39
* });
40
*
41
* _.filter(users, 'age__gt36');
42
* // => [{ 'user': 'fred', 'age': 40 }]
43
*/
44
function callback(func, thisArg, guard) {
45
if (guard && isIterateeCall(func, thisArg, guard)) {
46
thisArg = null;
47
}
48
return isObjectLike(func)
49
? matches(func)
50
: baseCallback(func, thisArg);
51
}
52
53
module.exports = callback;
54
55