function-overloader
Advanced tools
Comparing version 1.3.1 to 1.4.0
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(require("babel-polyfill")); | ||
module.exports = factory(require("babel-polyfill"), require("debug")); | ||
else if(typeof define === 'function' && define.amd) | ||
define(["babel-polyfill"], factory); | ||
define(["babel-polyfill", "debug"], factory); | ||
else { | ||
var a = typeof exports === 'object' ? factory(require("babel-polyfill")) : factory(root["babel-polyfill"]); | ||
var a = typeof exports === 'object' ? factory(require("babel-polyfill"), require("debug")) : factory(root["babel-polyfill"], root["debug"]); | ||
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; | ||
} | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) { | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_4__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
@@ -120,2 +120,3 @@ /******/ // The module cache | ||
}); | ||
exports.UNDEFINED = exports.SYMBOL = exports.BOOLEAN = exports.FUNCTION = exports.OBJECT = exports.NUMBER = exports.STRING = undefined; | ||
@@ -126,2 +127,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _debug = __webpack_require__(4); | ||
var _debug2 = _interopRequireDefault(_debug); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
@@ -131,5 +138,10 @@ | ||
/** | ||
* Class representing helper for methods for simplify overloading | ||
*/ | ||
var STRING = exports.STRING = "string"; | ||
var NUMBER = exports.NUMBER = "number"; | ||
var OBJECT = exports.OBJECT = "object"; | ||
var FUNCTION = exports.FUNCTION = "function"; | ||
var BOOLEAN = exports.BOOLEAN = "boolean"; | ||
var SYMBOL = exports.SYMBOL = "symbol"; | ||
var UNDEFINED = exports.UNDEFINED = "undefined"; | ||
var Overload = function () { | ||
@@ -150,3 +162,5 @@ _createClass(Overload, null, [{ | ||
this._debug = (0, _debug2.default)("overloader"); | ||
this._args = Array.from(arguments); | ||
this._debug("constructor get arguments ", this._args); | ||
this._enabled = true; | ||
@@ -170,19 +184,30 @@ this._result = null; | ||
var checkCondition = Array.from(arguments).every(function (arg, index) { | ||
switch (typeof arg === "undefined" ? "undefined" : _typeof(arg)) { | ||
// means that this is expected typeof argument | ||
case "string": | ||
return _typeof(_this._args[index]) === arg; | ||
// means that this is function which positive result means that this is expected argument | ||
case "function": | ||
return _this._args[index] instanceof arg; | ||
default: | ||
throw TypeError("Wrong arguments", arg); | ||
} | ||
}); | ||
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 (arg, index) { | ||
switch (typeof arg === "undefined" ? "undefined" : _typeof(arg)) { | ||
// means that this is expected typeof argument | ||
case "string": | ||
return _typeof(_this._args[index]) === arg; | ||
// means that this is function which positive result means that this is expected argument | ||
case "function": | ||
return _this._args[index] instanceof arg; | ||
default: | ||
throw TypeError("Wrong arguments", arg); | ||
} | ||
}); | ||
} | ||
this._debug("result", checkCondition); | ||
return { | ||
do: function _do(callback) { | ||
_this._debug("do"); | ||
if (checkCondition && _this._enabled) { | ||
_this._debug("execute function"); | ||
_this._enabled = false; | ||
_this._result = callback.apply(undefined, _toConsumableArray(_this._args)); | ||
var result = callback.apply(undefined, _toConsumableArray(_this._args)); | ||
_this._debug("function sync result", result); | ||
_this._result = result; | ||
} | ||
@@ -193,2 +218,15 @@ return _this; | ||
} | ||
}, { | ||
key: "else", | ||
value: function _else(callback) { | ||
this._debug("else"); | ||
if (this._enabled) { | ||
this._debug("execute function"); | ||
this._enabled = false; | ||
var result = callback.apply(undefined, _toConsumableArray(this._args)); | ||
this._debug("function sync result", result); | ||
this._result = result; | ||
} | ||
return this; | ||
} | ||
@@ -212,2 +250,8 @@ /** | ||
/***/ }), | ||
/* 4 */ | ||
/***/ (function(module, exports) { | ||
module.exports = require("debug"); | ||
/***/ }) | ||
@@ -214,0 +258,0 @@ /******/ ]); |
{ | ||
"name": "function-overloader", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "improve overloading functions and methods in js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import debug from "debug"; | ||
export const STRING = "string"; | ||
export const NUMBER = "number"; | ||
export const OBJECT = "object"; | ||
export const FUNCTION = "function"; | ||
export const BOOLEAN = "boolean"; | ||
export const SYMBOL = "symbol"; | ||
export const UNDEFINED = "undefined"; | ||
export default class Overload { | ||
@@ -4,0 +12,0 @@ static set() { |
@@ -1,2 +0,2 @@ | ||
import Overload from "./Overload"; | ||
import Overload, * as TYPES from "./Overload"; | ||
@@ -16,2 +16,46 @@ describe("Helper overload", () => { | ||
it("resolved to correct types", () => { | ||
let result = Overload.set("someString") | ||
.when(TYPES.STRING) | ||
.do(() => "correct string") | ||
.done(); | ||
expect(result).to.be.equal("correct string"); | ||
result = Overload.set(1234) | ||
.when(TYPES.NUMBER) | ||
.do(() => "correct number") | ||
.done(); | ||
expect(result).to.be.equal("correct number"); | ||
result = Overload.set(true) | ||
.when(TYPES.BOOLEAN) | ||
.do(() => "correct bool") | ||
.done(); | ||
expect(result).to.be.equal("correct bool"); | ||
result = Overload.set(() => {}) | ||
.when(TYPES.FUNCTION) | ||
.do(() => "correct function") | ||
.done(); | ||
expect(result).to.be.equal("correct function"); | ||
result = Overload.set({}) | ||
.when(TYPES.OBJECT) | ||
.do(() => "correct object") | ||
.done(); | ||
expect(result).to.be.equal("correct object"); | ||
result = Overload.set(Symbol("test")) | ||
.when(TYPES.SYMBOL) | ||
.do(() => "correct symbol") | ||
.done(); | ||
expect(result).to.be.equal("correct symbol"); | ||
result = Overload.set(undefined) | ||
.when(TYPES.UNDEFINED) | ||
.do(() => "correct undefined") | ||
.done(); | ||
expect(result).to.be.equal("correct undefined"); | ||
}); | ||
it("return correct response when expected undefined as argument", () => { | ||
@@ -18,0 +62,0 @@ let result = Overload.set(undefined, 12345) |
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
81074
457