npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

function-overload

v0.0.2

Published

,-----.,--. ,--. ,---. ,--.,------. ,------. ' .--./| | ,---. ,--.,--. ,-| || o \ | || .-. \ | .---' | | | || .-. || || |' .-. |`..' | | || | \ :| `--, ' '--'\| |' '-' '' '' '\ `-' | .' / |

Downloads

4

Readme

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