Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

function-overloader

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

function-overloader

improve overloading functions and methods in js

  • 2.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 27 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc