Uber
Uber is a function overloading tool for Node.js.
Installation
Install Uber with npm by running
npm install uber.js
Basic Usage
First of all, you need to require
Uber:
var uber = ;
Now you just have to replace your long function implementations with lots of arguments validations with Uber calls, so your code that looks like this:
var { if !array instanceof array throw 'Must provide a valid array'; if typeof index === 'number' && arrayindex === value; else array; };
Turns into this:
var addToArray = ;
Or this:
var addToArray = ;
And then Uber does the magic for you:
var myArray = ;;;;
Class instances types
You can also tell Uber to match arguments by their prototype:
var { thisname = name;}; var logUser = ; ;;;
Custom argument types
You can define your own type definitions by simply passing a function instead of a string in your implementation:
var repeatTimes = ;;
Uber will call your function with 3 arguments:
arg
: The current argumentindex
: The current argument's indexargs
: The actual array of arguments
And your function must either return a boolean or a positive integer, which represents the number of arguments it matches.
If you want to create reusable type definitions you can register them using uber.registerType
:
uber; var repeatTimes = ;;
Custom error handling
Notice that if Uber can't find a suitable implementation for the given arguments, it will throw an error:
var dummy = ; ; // Will throw 'Could not resolve function with given arguments'
If you want to handle these errors in your own way, you can add a function to the last index of your implementations array and it will get all the actual arguments:
var dummy = ; ; // Logs an error message
Class instance methods
Let's say you have a Person
class:
var { thisname = name;};
And you want to add a new method to its prototype. Do this:
Personprototypewalk = ;
What if you want to add it to an instance? Then Uber lets you tell what's the function's context:
var walter = 'Walter White';waltersayMyName = ;
See? It's the second argument (walter
). This way your implementation will be .apply
'ed with walter
:
walter;walter;
Map vs Array of implementations
As seen before, you can also call Uber with a map of implementations instead of an array. Even though it has a cleaner syntax, it lacks some features, since the keys must be strings. That's why you can't define error handler implementations by using maps. You may, however, declare an implementation that takes no arguments:
;
Also notice that Uber splits those keys and commas (,
) are delimiters and they are not .trim
'med,
so all whitespaces are kept. Thus, 'bool, int'
is NOT the same as 'bool,int'
since they are
respectively parsed into ['bool', 'int']
and ['bool', ' int']
.
uber.def
You can also can uber.def
instead of uber
. This way you should pass each argument as a function
signature, which means that this code:
;
Is the same as this one:
uber;
Note: This method does not support the context
argument.
List of supported argument types
Type | Matches | Number of matches |
---|---|---|
* | Anything | 1 |
... | Anything | Everything starting from the current argument until the end |
null | null |
1 |
undefined | undefined |
1 |
bool | Booleans | 1 |
int | Integers | 1 |
real | Real | 1 |
number | JavaScript numbers | 1 |
string | Strings | 1 |
array | Arrays | 1 |
plain | Plain objects ({} ) |
1 |
date | Date instances |
1 |
regex | Regexes | 1 |
function | Functions | 1 |
License
Please refer to file LICENSE.