@cursorsdottsx/o

Overloading was always a bit confusing (don't even get me started on TypeScript overloads), but never to fear, this library is here.

Usage no npm install needed!

<script type="module">
  import cursorsdottsxO from 'https://cdn.skypack.dev/@cursorsdottsx/o';
</script>

README

| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |

O is for Overload

@cursorsdottsx/o

Overloading was always a bit confusing (don't even get me started on TypeScript overloads), but never to fear, this library is here.

Though this library does not really go well with TypeScript, it does in fact work extremely well in JavaScript. No more headaches over what argument is what. It's just separate functions now that you can manage easily.

const overload = require("@cursorsdottsx/o");

const overloaded = overload({
    ["string number? object"](str, num) {
        console.log("callback 1 executed");
    },
    ["string boolean number?"](str, bool, num) {
        console.log("callback 2 executed");
    },
});

overloaded("hello world", 420, {}); // => "callback 1 executed"
overloaded("hello world", true, 69); // => "callback 2 executed"
overloaded("hello world", {}); // !! No overload matches this call.

Overloads follow a simple syntax, and there are currently on 4 supported types, all of which can be nullable:

  • string
  • number
  • boolean
  • object

To make a parameter nullable (and undefined-able), simply place a ? after the parameter:

  • string?
  • number?
  • boolean?
  • object?