1.1.4 • Published 4 years ago

@burntcoffee/overload v1.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Overload

A simple package that brings function overloading capabilities to javascript.

const { overload } = require('@burntcoffee/overload');

class Color {
    constructor(hexcode) {
        this.hexcode = hexcode;
    }
}

function componentToHex(c) {
    var hex = c.toString(16);
    return hex.length == 1 ? '0' + hex : hex;
}

function rgbToHex(r, g, b) {
    return '#' + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

function colorFromHex(hex) { return new Color(hex); }
function colorFromRGB(r, g, b) { return new Color(rgbToHex(r, g, b)); }

let color = overload({
    functions: [
        colorFromHex,
        colorFromRGB
    ],
});

color('#fa8b69'); // returns color
color(250, 139, 105); // returns same color as above by converting

Overload maps function calls to specific functions based on the arguments.

Functions can be added by passing them through the functions parameter.

Typing can be specified by passing an object as an argument:

const { overload, typing } = require('@burntcoffee/overload');

...

let color = overload({
    functions: [
        { func: colorFromHex, typing: [typing.Str] },
        { func: colorFromRGB, typing: [typing.Int, typing.Int, typing.Int] }
    ],
});

...

The 'func' key should contain a function and the 'typing' key should contain a list of the expected types of the arguments.

The following types are defined:

  • Int: Accepts anything with a type of "number"
  • Bool: Accepts anything with a type of "boolean"
  • Func: Accepts anything with a type of "function",
  • Str: Accepts anything with a type of "string"
  • Obj: Accepts anything with a type of "object"
  • Undef: Accepts anything with a type of "undefined"
  • Any: Accepts anything
1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago