over - Function Overloading

0.0.5 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates defining multiple function overloads using `over` with various argument matchers, including optional parameters with defaults and callbacks, then calling the overloaded function with different argument sets to show matching behavior.

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

view raw JSON →