function/after.jsjavascript
/**
* 创建一个在调用次数达到阈值后才执行的函数。
* @function after
* @memberof module:function
* @param {number} count 触发阈值,必须为正整数
* @param {Function} fn 目标函数
* @returns {Function} 包装函数
* @throws {TypeError|RangeError} 参数类型或 count 不合法时抛出
*/
function after(count, fn) {
if (!Number.isInteger(count) || count < 1) throw new RangeError('count must be a positive integer')
if (typeof fn !== 'function') throw new TypeError('fn must be a function')
let calls = 0
return function (...args) {
calls += 1
return calls >= count ? fn.apply(this, args) : undefined
}
}
export default after