function-overloader
Advanced tools
Comparing version 1.9.0 to 1.10.0
@@ -228,22 +228,8 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
this._debug("when", Array.from(arguments)); | ||
var checkCondition = false; | ||
if (arguments.length === 0 && this._args.length === 0) { | ||
checkCondition = true; | ||
} else if (arguments.length === this._args.length) { | ||
checkCondition = Array.from(arguments).every(function (typeFunction, index) { | ||
switch (typeof typeFunction === "undefined" ? "undefined" : _typeof(typeFunction)) { | ||
case "function": | ||
return typeFunction().execute(_this._args[index]); | ||
case "object": | ||
return typeFunction.execute(_this._args[index]); | ||
default: | ||
throw TypeError("Wrong arguments", typeFunction); | ||
} | ||
}); | ||
} | ||
this._debug("result", checkCondition); | ||
var conditionResult = checkCondition(arguments, this._args); | ||
this._debug("result", conditionResult); | ||
return { | ||
do: function _do(callback) { | ||
_this._debug("do"); | ||
if (checkCondition && _this._enabled) { | ||
if (conditionResult && _this._enabled) { | ||
_this._debug("execute function"); | ||
@@ -258,2 +244,3 @@ _this._enabled = false; | ||
else: _this.else, | ||
elseThrow: _this.elseThrow, | ||
done: _this.done | ||
@@ -279,2 +266,14 @@ }; | ||
} | ||
}, { | ||
key: "elseThrow", | ||
value: function elseThrow() { | ||
this._debug("elseThrow"); | ||
if (this._enabled) { | ||
this._enabled = false; | ||
throw TypeError(); | ||
} | ||
return { | ||
done: this.done | ||
}; | ||
} | ||
@@ -309,2 +308,24 @@ /** | ||
function checkCondition(conditionArguments, testedArguments) { | ||
if (conditionArguments.length === testedArguments.length) { | ||
return Array.from(conditionArguments).every(function (typeFunction, index) { | ||
var testedArgument = testedArguments[index]; | ||
return checkTypeCondition(typeFunction, testedArgument); | ||
}); | ||
} | ||
return false; | ||
} | ||
function checkTypeCondition(typeFunction, testedArgument) { | ||
switch (typeof typeFunction === "undefined" ? "undefined" : _typeof(typeFunction)) { | ||
case "function": | ||
return typeFunction().execute(testedArgument); | ||
case "object": | ||
return typeFunction.execute(testedArgument); | ||
default: | ||
throw TypeError("Wrong arguments", typeFunction); | ||
} | ||
} | ||
/***/ }), | ||
@@ -311,0 +332,0 @@ /* 4 */ |
{ | ||
"name": "function-overloader", | ||
"version": "1.9.0", | ||
"version": "1.10.0", | ||
"description": "improve overloading functions and methods in js", | ||
@@ -10,3 +10,3 @@ "main": "dist/index.js", | ||
"test": "mocha --require source-map-support --require babel-register --require babel-polyfill --require init.mocha.js --check-leaks --timeout 3000 'src/**/*.spec.js'", | ||
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- --require source-map-support --require babel-register --require babel-polyfill --require init.mocha.js --check-leaks --timeout 3000 'src/**/*.spec.js' && open coverage/lcov-report/index.html", | ||
"coverage": "istanbul cover -x 'src/**/*.spec.js' ./node_modules/.bin/_mocha -- --require source-map-support --require babel-register --require babel-polyfill --require init.mocha.js --check-leaks --timeout 3000 'src/**/*.spec.js' && open coverage/lcov-report/index.html", | ||
"complexity": "plato -r --eslint .eslintrc -d report src && open report/index.html", | ||
@@ -13,0 +13,0 @@ "linter": "eslint src", |
@@ -170,2 +170,11 @@ # FUNCTION OVERLOADER | ||
### .elseThrow() | ||
```javascript | ||
.elseThrow() | ||
``` | ||
Throws TypeError if not any above condition met | ||
Return object with `done` method. | ||
### .done() | ||
@@ -172,0 +181,0 @@ |
@@ -59,22 +59,8 @@ import debug from "debug"; | ||
this._debug("when", Array.from(arguments)); | ||
let checkCondition = false; | ||
if (arguments.length === 0 && this._args.length === 0) { | ||
checkCondition = true; | ||
} else if (arguments.length === this._args.length) { | ||
checkCondition = Array.from(arguments).every((typeFunction, index) => { | ||
switch (typeof typeFunction) { | ||
case "function": | ||
return typeFunction().execute(this._args[index]); | ||
case "object": | ||
return typeFunction.execute(this._args[index]); | ||
default: | ||
throw TypeError("Wrong arguments", typeFunction); | ||
} | ||
}); | ||
} | ||
this._debug("result", checkCondition); | ||
let conditionResult = checkCondition(arguments, this._args); | ||
this._debug("result", conditionResult); | ||
return { | ||
do: callback => { | ||
this._debug("do"); | ||
if (checkCondition && this._enabled) { | ||
if (conditionResult && this._enabled) { | ||
this._debug("execute function"); | ||
@@ -89,2 +75,3 @@ this._enabled = false; | ||
else: this.else, | ||
elseThrow: this.elseThrow, | ||
done: this.done | ||
@@ -110,2 +97,13 @@ }; | ||
elseThrow() { | ||
this._debug("elseThrow"); | ||
if (this._enabled) { | ||
this._enabled = false; | ||
throw TypeError(); | ||
} | ||
return { | ||
done: this.done | ||
}; | ||
} | ||
/** | ||
@@ -119,1 +117,22 @@ * Should be called at the end. It will return result from called use callback | ||
} | ||
function checkCondition(conditionArguments, testedArguments) { | ||
if (conditionArguments.length === testedArguments.length) { | ||
return Array.from(conditionArguments).every((typeFunction, index) => { | ||
const testedArgument = testedArguments[index]; | ||
return checkTypeCondition(typeFunction, testedArgument); | ||
}); | ||
} | ||
return false; | ||
} | ||
function checkTypeCondition(typeFunction, testedArgument) { | ||
switch (typeof typeFunction) { | ||
case "function": | ||
return typeFunction().execute(testedArgument); | ||
case "object": | ||
return typeFunction.execute(testedArgument); | ||
default: | ||
throw TypeError("Wrong arguments", typeFunction); | ||
} | ||
} |
@@ -10,6 +10,7 @@ /* eslint-disable no-new-wrappers */ | ||
expect(whenMethodResult).to.not.have.property("else"); | ||
expect(whenMethodResult).to.not.have.property("elseThrow"); | ||
expect(whenMethodResult).to.not.have.property("done"); | ||
}); | ||
it("ensure that 'do' method return object with 'when' & 'else' & 'done' methods", () => { | ||
it("ensure that 'do' method return object with 'when' & 'else' & 'elseThrow' & 'done' methods", () => { | ||
let doMethodResult = Overload.set() | ||
@@ -20,2 +21,3 @@ .when() | ||
expect(doMethodResult).to.have.property("else"); | ||
expect(doMethodResult).to.have.property("elseThrow"); | ||
expect(doMethodResult).to.have.property("done"); | ||
@@ -30,5 +32,15 @@ expect(doMethodResult).to.not.have.property("do"); | ||
expect(elseMethodResult).to.not.have.property("else"); | ||
expect(elseMethodResult).to.not.have.property("elseThrow"); | ||
expect(elseMethodResult).to.not.have.property("do"); | ||
}); | ||
it("ensure that 'elseThrow' method return only object with 'done' method", () => { | ||
let elseMethodResult = Overload.set().else(() => {}); | ||
expect(elseMethodResult).to.have.property("done"); | ||
expect(elseMethodResult).to.not.have.property("when"); | ||
expect(elseMethodResult).to.not.have.property("else"); | ||
expect(elseMethodResult).to.not.have.property("elseThrow"); | ||
expect(elseMethodResult).to.not.have.property("do"); | ||
}); | ||
it("ensure that 'done' method return response", () => { | ||
@@ -160,2 +172,16 @@ let doneMethodResult = Overload.set().done(); | ||
it("invoke elseThrow when no condition met", () => { | ||
expect(() => { | ||
Overload.set(10, 10) | ||
.when(Overload.NUMBER, Overload.STRING) | ||
.do(() => "wrong result") | ||
.when() | ||
.do(() => "wrong result") | ||
.when(Overload.NUMBER, Overload.OBJECT) | ||
.do(() => "wrong result") | ||
.elseThrow() | ||
.done(); | ||
}).to.throw(TypeError); | ||
}); | ||
it("return sync result for classes", () => { | ||
@@ -162,0 +188,0 @@ class Test1 {} |
Sorry, the diff of this file is not supported yet
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
919
210
112766
43