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