Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
function-overloader
Advanced tools
Introduce the mechanism for easy overloading functions and method.
npm install function-overloader
This library/helper is solution for lack of function overloading in javascript. It will not introduce overloading for functions. But it will help handling multiple different arguments.
It will return response from correct do
block.
import Overload from "function-overloader";
class Monster {
constructor() {
Overload.set(...arguments)
.when("string", "number")
.do((monsterName, level) => {
this.name = monsterName;
this.level = level;
})
.when("object")
.do(monsterData => {
this.name = monsterData.name;
this.level = monsterData.level;
})
.done();
console.log(`Monster ${this.name} level ${this.level} created`);
}
addAttribute() {
return Overload.set(...arguments)
.when(Attribute)
.do(this.addExisitingAttribute)
.when("string", "function")
.do(this.addNewAttribute)
.done();
}
addExisitingAttribute (attribute) {
this.attributes.push(attribute);
return attribute;
}
addNewAttribute (attributeName, attributeLogic) {
const attribute = new Attribute(attributeName, attributeLogic);
this.attributes.push(attribute);
return attribute;
}
}
Now it is possible:
const monster1 = new Monster ("hakuna", 3);
monster1.addAttribute("happy", () => {});
const attribute = new Attribute("sad", () => {});
monster1.addAttribute(attribute);
const monster2 = new Monster ({
name: "froggy",
level: 2
});
Decrease code complexity of overloaded functions and methods.
Old fashion:
function someMethod () {
if(arguments.length === 2 && typeof arguments[0] === "string" && typeof arguments[1] === "number") {
//do some stuff when get string and number
} else if (arguments.length === 1 && typeof arguments[0] === "object" && arguments[0] instanceof SomeCustomConstructor) {
//do something else if one argument which is instance of SomeCustomConstructor
} else {
//do something else
}
}
import/require this library
import Overload from "function-overloader";
then:
function someOverloadedFunction() {
return Overload.set(...arguments)
.when("sometypeA1", "sometypeA2")
.do(someFunctionHandlingFirstCase)
.when("sometypeB1", "sometypeB2")
.do(someFunctionHandlingSecondCase)
.done();
}
Overload.set(...arguments)
accept function arguments. It is possible by passing them one by one, but preffered why is to just pass spread ...arguments
.
It will return Condition Response
Another possibility is to :
new Overload(...arguments);
And it will also return Condition Response
It has methods:
.when()
It is for describe when to run related do
method.
Return Function Response
Accept multiple values that will descibe function. Possible values:
"boolean"
"number"
"string"
"symbol"
"function"
"undefined"
Constructor function / class to define that argument should be an instance of provided class / constructor.
if there is no arguments it means that it will resolve only when overloaded function doesn't get any arguments.
.done()
should be called at the end to mark that now we should get chosen function response
No arguments. Will return funtion response
Has one method
.do()
Accept function which should be called if previous .when
match arguments.
Will respond with Condition Response
MIT
FAQs
improve overloading functions and methods in js
The npm package function-overloader receives a total of 0 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.