Created
February 2, 2024 19:55
-
-
Save cmstead/599a82ced3bad6cf67c8acb8209d8d8f to your computer and use it in GitHub Desktop.
Decorator to validate argument count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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