Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / function / before.js
1 /** Used as the `TypeError` message for "Functions" methods. */
2 var FUNC_ERROR_TEXT = 'Expected a function';
3
4 /**
5  * Creates a function that invokes `func`, with the `this` binding and arguments
6  * of the created function, while it's called less than `n` times. Subsequent
7  * calls to the created function return the result of the last `func` invocation.
8  *
9  * @static
10  * @memberOf _
11  * @category Function
12  * @param {number} n The number of calls at which `func` is no longer invoked.
13  * @param {Function} func The function to restrict.
14  * @returns {Function} Returns the new restricted function.
15  * @example
16  *
17  * jQuery('#add').on('click', _.before(5, addContactToList));
18  * // => allows adding up to 4 contacts to the list
19  */
20 function before(n, func) {
21   var result;
22   if (typeof func != 'function') {
23     if (typeof n == 'function') {
24       var temp = n;
25       n = func;
26       func = temp;
27     } else {
28       throw new TypeError(FUNC_ERROR_TEXT);
29     }
30   }
31   return function() {
32     if (--n > 0) {
33       result = func.apply(this, arguments);
34     }
35     if (n <= 1) {
36       func = undefined;
37     }
38     return result;
39   };
40 }
41
42 module.exports = before;