function-overloader
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "function-overloader", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "improve overloading functions and methods in js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -7,1 +7,76 @@ # FUNCTION OVERLOADER | ||
Introduce the mechanism for easy overloading functions and method. | ||
## install | ||
``` | ||
npm install function-overloader | ||
``` | ||
## teaser | ||
``` | ||
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; | ||
}) | ||
.when() | ||
.do(() => { | ||
throw Error("Wrong attributes); | ||
}) | ||
.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) | ||
.when() | ||
.do(() => { | ||
throw Error("Wrong attributes); | ||
}) | ||
.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 | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51707
82