es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
21 lines (18 loc) • 692 B
JavaScript
import { identity } from '../../function/identity.mjs';
import { iteratee } from '../util/iteratee.mjs';
function overArgs(func, ..._transforms) {
if (typeof func !== 'function') {
throw new TypeError('Expected a function');
}
const transforms = _transforms.flat();
return function (...args) {
const length = Math.min(args.length, transforms.length);
const transformedArgs = [...args];
for (let i = 0; i < length; i++) {
const transform = iteratee(transforms[i] ?? identity);
transformedArgs[i] = transform.call(this, args[i]);
}
return func.apply(this, transformedArgs);
};
}
export { overArgs };