
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
function-overloader
Advanced tools
This library/helper is solution for lack of function overloading in javascript. You can define different behaviours depending on provided arguments to the function.
npm install function-overloader
First import/require this library
import Overload from "function-overloader";
then for function or method define rules and callbacks
const rulesForSomeFunction =
Overload
.when(<list of rules>)
.do(<callback called when above rules pass>)
.when(<list of other rules>)
.do(<callback called when above rules pass>)
// you can add as many rules as you need
Next we call rulesForSomeFunction.execute(...arguments)
inside overloaded function or method
function someOverloadedFunction() {
return rulesForSomeFunction
.execute(...arguments);
}
Right when you call someOverloadedFunction
with different arguments it will call correct callback
Lets assume that we need function which can
name
and age
where name
is string and age
a number.All variants should return string with format name_age
import Overload from "function-overloader";
function joinNameAndAge() {
return Overload
.when(Overload.Interface({
name: Overload.STRING,
age: Overload.NUMBER
}))
.do(objWithNameAndAge => objWithNameAndAge.name + objWithNameAndAge.age)
.when(Overload.String, Overload.NUMBER)
.do((name, age) => name + age)
.when(Overlod.Number, Overload.String)
.do((age, name) => name + age)
.execute(...arguments);
}
joinNameAndAge({
name: "Test",
age: 1
}); // Test1
joinNameAndAge("Test", 2); // Test2
joinNameAndAge(3, "Test"); // Test3
.when()
It is for describe when to run related do
method.
Return object with do
method
Accept multiple values that will descibe function.
There is lot of possible values, Some with additional params. All of them are available here https://github.com/uhlryk/check-complex-types
All of them are available in Overload
object. e.g. Overload.ANY()
Is accessible only from object returned from .when
method
.do()
Accept callback function which should be called if previous .when
match arguments.
.else()
Accept callback function. Will invoke it when other criteria are not met.
.elseThrow()
Throws TypeError if not any above condition met
.execute()
accept function arguments. It is possible by passing them one by one, but preferred why is to just pass spread ...arguments
.
MIT
FAQs
improve overloading functions and methods in js
We found that function-overloader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.