1 /** Used as the `TypeError` message for "Functions" methods. */
2 var FUNC_ERROR_TEXT = 'Expected a function';
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.
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.
17 * jQuery('#add').on('click', _.before(5, addContactToList));
18 * // => allows adding up to 4 contacts to the list
20 function before(n, func) {
22 if (typeof func != 'function') {
23 if (typeof n == 'function') {
28 throw new TypeError(FUNC_ERROR_TEXT);
33 result = func.apply(this, arguments);
42 module.exports = before;