@bytegenius/wrap-with-noop v0.1.0
wrap-with-noop
wrap-with-noop is a simple utility for wrapping a function and adding argument count validation. This package allows you to either perform a no-op (return a default value) or throw an error when the function is called with an incorrect number of arguments.
Installation
You can install this package using npm or yarn:
npm install wrap-with-noopor
yarn add wrap-with-noopUsage
To use wrap-with-noop, require the package in your Node.js application:
const wrapWithNoOp = require('wrap-with-noop');Example
Here's an example of how to use wrapWithNoOp:
const wrappedFunction = wrapWithNoOp(
(a, b) => a + b,
2,
{ silent: true }
);
const result = wrappedFunction(3);
console.log(result); // This will print "undefined" because it's silentIn this example, we are wrapping a function that expects at least two arguments. When the wrapped function is called with only one argument, it returns undefined because the silent option is set to true. If the silent option were set to false, it would throw an error.
API
wrapWithNoOp(fn, requiredArgs, { silent })
fn: The function you want to wrap with argument checking.requiredArgs: An integer representing the minimum number of argumentsfnshould receive.{ silent }: An options object that includes asilentproperty (default isfalse). Ifsilentisfalse, the function throws an error when the argument check fails. Ifsilentistrue, it returnsundefined(no-op) when the argument check fails.
License
This package is open-source and available under the MIT License.
2 years ago