over - Function Overloading
over is a JavaScript library designed to facilitate function overloading, allowing developers to define multiple implementations for a single function based on the arguments provided during invocation. It operates by accepting an array of 'test functions' (such as `over.string`, `over.numberOptionalWithDefault`, `over.callbackOptional`), which are used to match the types and presence of input arguments against predefined patterns. This enables complex argument signature management, default values for optional parameters, and callback handling in a structured way. The package is currently at version `0.0.5`, with its last significant activity in 2013, and the copyright dates back to 2012. This strongly suggests the project is abandoned, with no active development or maintenance. Its core differentiator was providing a declarative approach to function dispatch in a pre-ESM and pre-TypeScript era, often mitigating verbose `if`/`else` or switch statements for argument validation. It primarily targets Node.js environments.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module context (e.g., in a `.mjs` file or a `.js` file where `"type": "module"` is set in `package.json`).fixChange your module to CommonJS by ensuring it's a `.js` file and not declared as an ES Module in `package.json`, or use a dynamic `import()` if absolutely necessary: `const over = await import('over');`. -
TypeError: over is not a function
cause Incorrectly attempting to use named imports or default ES Module imports (`import over from 'over';`) in an environment where `over` only provides a CommonJS export.fixEnsure you are using the correct CommonJS `require` syntax for importing the package: `const over = require('over');`.
Warnings
- breaking This package appears to be abandoned since 2013 and is no longer maintained. Using an unmaintained library introduces significant security risks, as it will not receive patches for vulnerabilities or updates for compatibility with modern JavaScript runtimes or security standards.
- gotcha The package was developed for CommonJS environments and does not natively support ES Modules (ESM). Attempting to `import over from 'over';` in an ESM context will likely result in errors like `ERR_REQUIRE_ESM` or `over is not defined`.
- gotcha Due to its age, `over` may exhibit compatibility issues with newer Node.js versions, particularly those released after 2013. Behavior might be undefined or lead to runtime errors, especially with changes in core APIs or internal JavaScript engine optimizations.
Install
-
npm install over -
yarn add over -
pnpm add over
Imports
- over
import over from 'over';
const over = require('over'); - over.string
import { string } from 'over';const over = require('over'); const myFn = over([over.string, function (str) { /* ... */ }]);
Quickstart
const over = require('over');
const myfn = over([
[over.string, function (str) { console.log('Called with a single string:', str); }],
[over.string, over.numberOptionalWithDefault(5), over.callbackOptional, function (str, number, callback) {
console.log('Called with string, optional number (default 5), and optional callback.');
console.log('String:', str, 'Number:', number);
if (callback) {
console.log('Executing callback...');
callback(str, number);
}
}],
function() {
console.log('No specific overload matched. Falling back to default.');
}
]);
// Test cases
myfn("hello");
myfn("world", 10, (s, n) => console.log(`Callback invoked: ${s}, ${n}`));
myfn("test", null, (s, n) => console.log(`Callback invoked with null number: ${s}, ${n}`)); // numberOptionalWithDefault should handle null/undefined
myfn("another test", undefined, (s, n) => console.log(`Callback invoked with undefined number: ${s}, ${n}`));
myfn(); // Should hit default