0.1.0 • Published 7 months ago

@bytegenius/wrap-with-noop v0.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

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 arguments fn should receive.
  • { silent }: An options object that includes a silent property (default is false). If silent is false, the function throws an error when the argument check fails. If silent is true, it returns undefined (no-op) when the argument check fails.

License

This package is open-source and available under the MIT License.

0.1.0

7 months ago