Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / function / bindKey.js
1 var createWrapper = require('../internal/createWrapper'),
2     replaceHolders = require('../internal/replaceHolders'),
3     restParam = require('./restParam');
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 the method belongs to.
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  * // using placeholders
50  * var bound = _.bindKey(object, 'greet', _, '!');
51  * bound('hi');
52  * // => 'hiya fred!'
53  */
54 var bindKey = restParam(function(object, key, partials) {
55   var bitmask = BIND_FLAG | BIND_KEY_FLAG;
56   if (partials.length) {
57     var holders = replaceHolders(partials, bindKey.placeholder);
58     bitmask |= PARTIAL_FLAG;
59   }
60   return createWrapper(key, bitmask, object, partials, holders);
61 });
62
63 // Assign default placeholders.
64 bindKey.placeholder = {};
65
66 module.exports = bindKey;