Skip to content

TeaEntityLab/js-function-overload

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Function overload

overload the function with flexible way

Usage

load this library

var overload = require("function-overload").overload;

create templete method

var newFunc = overload();

or init method with default handle

var newFunc = overload(function (...args) {
    // code goes here
});

set the default handle

var newFunc = overload();
newFunc["default"](function (...args) {
    // code goes here
});

overload with given signature

var newFunc = overload();

// method 1
newFunc.overload([Number], function (arr) {
    // handle the array of number
});

// method 2
newFunc.overload([Boolean], function (arr) {
    // handle the array of boolean
});

// method 3
newFunc.overload(Number, function (num) {
    // handle the number
});

newFunc([1]); // trigger method 1
newFunc([true]); // trigger method 2
newFunc(1); // trigger method 3
newFunc(); // throws, because no one can handle it and default handler does no exist

nested signature

var newFunc = overload();

// method 1
newFunc.overload([{id: Number, name: String}], function (arrOfUser) {
    // handle these users
});

newFunc([{id: 0, name: 'root'}]); // trigger method 1
newFunc([{id: '0', name: 'root'}]]); //throws, because no one can handle it

special types

requires these types first

var types = require("function-overload").types;

equal
virtual type matches only when the content is exactly equal to target.

var newFunc = overload();

newFunc.overload(types.equal(0), function (zero) {
    // handle it
});

newFunc(0); // pass
newFunc(1); // throws
newFunc("0"); // throws

biggerThan
virtual type matches only when the content is a number bigger than passed number.

var newFunc = overload();

newFunc.overload(types.biggerThan(0), function (positiveNumber) {
    // handle it
});

newFunc(1); // pass
newFunc(0); // throws
newFunc(-1); // throws
newFunc("1"); // throws

smallerThan
virtual type matches only when the content is a number smaller than passed number.

var newFunc = overload();

newFunc.overload(types.smallerThan(0), function (negtiveNumber) {
    // handle it
});

newFunc(1); // throws
newFunc(0); // throws
newFunc(-1); // pass
newFunc("1"); // throws

biggerEqualThan
virtual type matches only when the content is a number bigger than or equal to passed number.

var newFunc = overload();

newFunc.overload(types.biggerEqualThan(0), function (positiveNumber) {
    // handle it
});

newFunc(1); // pass
newFunc(0); // pass
newFunc(-1); // throws
newFunc("1"); // throws

smallerEqualThan
virtual type matches only when the content is a number smaller than or equal to passed number.

var newFunc = overload();

newFunc.overload(types.smallerEqualThan(0), function (negtiveNumber) {
    // handle it
});

newFunc(1); // throws
newFunc(0); // pass
newFunc(-1); // pass
newFunc("1"); // throws

or
virtual type matches only when one of the two methods matched

var newFunc = overload();

newFunc.overload(
types.or(String, Number), function (stringOrNumber) {
    // handle it
});

newFunc(1); // pass
newFunc("1"); // pass
newFunc(true); // throws

and
virtual type matches only when both methods matched

var newFunc = overload();

newFunc.overload(types.and(
    types.biggerEqualThan(0),
    types.smallerThan(10)
), function (numberBwetweenOneAndTen) {
    // handle it
});

newFunc(-1); // throws
newFunc(0); // pass
newFunc(9); // pass
newFunc(10); // throws
newFunc(true); // throws

custom types

t.unshift("odd", function (makeValidator) {
    return {
        $typeCheck: function (val) {
            return "number" === typeof val && ((val % 2) + 2) % 2 === 1;
        },
        $typeDescription: "odd number",
        $typeSignature: "{odd number}"
    }
})

var newFunc2 = overload();

newFunc.overload(types.odd(), function (oddNumber) {
    // handle it
});

newFunc(0); // throws
newFunc(0.5); // throws
newFunc(1); // pass

custom type 2

t.unshift("not", function (makeValidator, input) {
    // create the validator from input
    var originalValidator = makeValidator(input);
    
    return {
        $typeCheck: function (val) {
             return !originalValidator.$typeCheck(val)
        },
        $typeDescription: "not " + originalValidator.$typeDescription,
        $typeSignature: "{not" + originalValidator.$typeSignature + "}"
    }
})

var newFunc2 = overload();

newFunc2.overload(types.not(Number), function (notNumber) {
    // handle it
});

newFunc(0); // throws
newFunc("0"); // pass
newFunc(true); // pass

cleaned method

var newFunc = overload(function () {
    return 1;
});
// create cleaned method with the original one
var newFunc2 = newFunc.clean();

newFunc(); // -> 1
newFunc2(); // -> 1

newFunc2.overload(someFunc) // throws

About

function overload in javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%