function/ary.jsjavascript
/**
* 创建一个最多接收指定数量参数的函数。
* @function ary
* @memberof module:function
* @param {Function} fn 目标函数
* @param {number} [count=fn.length] 最大参数数量
* @returns {Function} 参数受限的包装函数
* @throws {TypeError|RangeError} 参数类型或 count 不合法时抛出
*/
function ary(fn, count = fn?.length ?? 0) {
if (typeof fn !== 'function') throw new TypeError('fn must be a function')
if (!Number.isInteger(count) || count < 0) throw new RangeError('count must be a non-negative integer')
return function (...args) {
return fn.apply(this, args.slice(0, count))
}
}
export default ary