Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
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
The npm package function-overloader receives a total of 10 weekly downloads. As such, function-overloader popularity was classified as not popular.
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
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.