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-overloader

v2.1.2

Published

improve overloading functions and methods in js

Downloads

59

Readme

FUNCTION OVERLOADER

Build Status Downloads Downloads NPM version

This library/helper is solution for lack of function overloading in javascript. You can define different behaviours depending on provided arguments to the function.

install

npm install function-overloader

usage

First import/require this library

import Overload from "function-overloader";

then for function or method define rules and callbacks

const rulesForSomeFunction = 
    Overload
       .when(<list of rules>)
       .do(<callback called when above rules pass>)
       .when(<list of other rules>)
       .do(<callback called when above rules pass>)
       // you can add as many rules as you need

Next we call rulesForSomeFunction.execute(...arguments) inside overloaded function or method

function someOverloadedFunction() {
    return rulesForSomeFunction
        .execute(...arguments);
}

Right when you call someOverloadedFunction with different arguments it will call correct callback

example

First example

Lets assume that we need function which can

  • accept object with properties name and age where name is string and age a number.
  • accept two arguments where former is a string and second a number.
  • accept two arguments where former is a number and second a string.

All variants should return string with format name_age


import Overload from "function-overloader";

function joinNameAndAge() {
    return Overload
       .when(Overload.Interface({
           name: Overload.STRING,
           age: Overload.NUMBER
       }))
       .do(objWithNameAndAge => objWithNameAndAge.name + objWithNameAndAge.age)
       .when(Overload.String, Overload.NUMBER)
       .do((name, age) => name + age)
       .when(Overlod.Number, Overload.String)
       .do((age, name) => name + age)
       .execute(...arguments);
}

joinNameAndAge({
    name: "Test",
    age: 1
}); // Test1
joinNameAndAge("Test", 2); // Test2
joinNameAndAge(3, "Test"); // Test3

API

.when()

.when()

It is for describe when to run related do method. Return object with do method

Accept multiple values that will descibe function.

There is lot of possible values, Some with additional params. All of them are available here https://github.com/uhlryk/check-complex-types

All of them are available in Overload object. e.g. Overload.ANY()

.do()

Is accessible only from object returned from .when method

.do()

Accept callback function which should be called if previous .when match arguments.

.else()

.else()

Accept callback function. Will invoke it when other criteria are not met.

.elseThrow()

.elseThrow()

Throws TypeError if not any above condition met

.execute()

.execute()

accept function arguments. It is possible by passing them one by one, but preferred why is to just pass spread ...arguments.

License

MIT