Skip to content

Instantly share code, notes, and snippets.

@cmstead
Created February 2, 2024 19:55
Show Gist options
  • Save cmstead/599a82ced3bad6cf67c8acb8209d8d8f to your computer and use it in GitHub Desktop.
Save cmstead/599a82ced3bad6cf67c8acb8209d8d8f to your computer and use it in GitHub Desktop.
Decorator to validate argument count
function validateAndCall(args, enforcedFunction, maxArgumentCount) {
if(args.length > maxCount) {
throw new Error(`Function '${fn.name}' cannot be called with more than ${maxCount} arguments, but was called with ${args.length}`);
}
return fn.apply(null, args);
}
function enforceCount({ enforcedFunction, maxArgumentCount }) {
return function () {
return validateAndCall(arguments, enforcedFunction, maxArgumentCount);
}
}
// export in whichever way you need
export default enforceCount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment