UNPKG

1.91 kBJavaScriptView Raw
1/**
2 * lodash 3.0.1 (Custom Build) <https://lodash.com/>
3 * Build: `lodash modern modularize exports="npm" -o ./`
4 * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <https://lodash.com/license>
8 */
9
10/**
11 * A specialized version of `baseCallback` which only supports `this` binding
12 * and specifying the number of arguments to provide to `func`.
13 *
14 * @private
15 * @param {Function} func The function to bind.
16 * @param {*} thisArg The `this` binding of `func`.
17 * @param {number} [argCount] The number of arguments to provide to `func`.
18 * @returns {Function} Returns the callback.
19 */
20function bindCallback(func, thisArg, argCount) {
21 if (typeof func != 'function') {
22 return identity;
23 }
24 if (thisArg === undefined) {
25 return func;
26 }
27 switch (argCount) {
28 case 1: return function(value) {
29 return func.call(thisArg, value);
30 };
31 case 3: return function(value, index, collection) {
32 return func.call(thisArg, value, index, collection);
33 };
34 case 4: return function(accumulator, value, index, collection) {
35 return func.call(thisArg, accumulator, value, index, collection);
36 };
37 case 5: return function(value, other, key, object, source) {
38 return func.call(thisArg, value, other, key, object, source);
39 };
40 }
41 return function() {
42 return func.apply(thisArg, arguments);
43 };
44}
45
46/**
47 * This method returns the first argument provided to it.
48 *
49 * @static
50 * @memberOf _
51 * @category Utility
52 * @param {*} value Any value.
53 * @returns {*} Returns `value`.
54 * @example
55 *
56 * var object = { 'user': 'fred' };
57 *
58 * _.identity(object) === object;
59 * // => true
60 */
61function identity(value) {
62 return value;
63}
64
65module.exports = bindCallback;