5cefad3ed80e6128e40c7e5435c7617d5e1eb357
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / bindKey.js
1 var createWrapper = require('./_createWrapper'),
2     replaceHolders = require('./_replaceHolders'),
3     rest = require('./rest');
4
5 /** Used to compose bitmasks for wrapper metadata. */
6 var BIND_FLAG = 1,
7     BIND_KEY_FLAG = 2,
8     PARTIAL_FLAG = 32;
9
10 /**
11  * Creates a function that invokes the method at `object[key]` and prepends
12  * any additional `_.bindKey` arguments to those provided to the bound function.
13  *
14  * This method differs from `_.bind` by allowing bound functions to reference
15  * methods that may be redefined or don't yet exist.
16  * See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)
17  * for more details.
18  *
19  * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic
20  * builds, may be used as a placeholder for partially applied arguments.
21  *
22  * @static
23  * @memberOf _
24  * @category Function
25  * @param {Object} object The object to invoke the method on.
26  * @param {string} key The key of the method.
27  * @param {...*} [partials] The arguments to be partially applied.
28  * @returns {Function} Returns the new bound function.
29  * @example
30  *
31  * var object = {
32  *   'user': 'fred',
33  *   'greet': function(greeting, punctuation) {
34  *     return greeting + ' ' + this.user + punctuation;
35  *   }
36  * };
37  *
38  * var bound = _.bindKey(object, 'greet', 'hi');
39  * bound('!');
40  * // => 'hi fred!'
41  *
42  * object.greet = function(greeting, punctuation) {
43  *   return greeting + 'ya ' + this.user + punctuation;
44  * };
45  *
46  * bound('!');
47  * // => 'hiya fred!'
48  *
49  * // Bound with placeholders.
50  * var bound = _.bindKey(object, 'greet', _, '!');
51  * bound('hi');
52  * // => 'hiya fred!'
53  */
54 var bindKey = rest(function(object, key, partials) {
55   var bitmask = BIND_FLAG | BIND_KEY_FLAG;
56   if (partials.length) {
57     var placeholder = bindKey.placeholder,
58         holders = replaceHolders(partials, placeholder);
59
60     bitmask |= PARTIAL_FLAG;
61   }
62   return createWrapper(key, bitmask, object, partials, holders);
63 });
64
65 // Assign default placeholders.
66 bindKey.placeholder = {};
67
68 module.exports = bindKey;