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-noop
or
yarn add wrap-with-noop
Usage
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 silent
In 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 argumentsfn
should receive. -
{ silent }
: An options object that includes asilent
property (default isfalse
). Ifsilent
isfalse
, the function throws an error when the argument check fails. Ifsilent
istrue
, it returnsundefined
(no-op) when the argument check fails.
License
This package is open-source and available under the MIT License.