1.0.0 • Published 9 years ago
simple-overloading v1.0.0
simple-overloading
Simple function overloading for node.js
Install
npm install -g simple-overloading
Usage
Simple example
var SOverloading = require('../');
function myFn() {
//-- this is your variable scope with its default values
var scope = {
full_name : '',
send_sms : true
};
/**
* this is your first method variation:
* myFn( {String} full_name [ = '' ] );
*/
var m1 = {
'full_name' : 'string'
};
/**
* this is your second method variation:
* myFn( {String} full_name [ = '' ] , {Boolean} send_sms [ = true ] );
*/
var m2 = SOverloading.extendMethod(m1,{
'send_sms' : 'boolean'
});
if (!SOverloading.variation([m1,m2],scope,arguments)) {
throw new Error('Method not found...');
}
console.log(scope);
}
myFn('Martín Rafael González');
/* { full_name: 'Martín Rafael González', send_sms: true } */
myFn('Martín Rafael González',false);
/* { full_name: 'Martín Rafael González', send_sms: false } */
myFn( { full_name : 'Martín Rafael González' } );
/* { full_name: 'Martín Rafael González', send_sms: true } */
myFn('Martín Rafael González','hello');
// throws error
When we add our method variations, we can set the type and even the values we want to use.
var m1 = {
full_name : 'string', // only accept strings
phone_number : 'string,number', // accept strings or numbers
sex : 'string:male,female', // accept only strings with value 'male' or 'female',
additional : '^function' // accepts any value but a function
};
Allowed value types
- string: matches any string
- array: matches only arrays (not objects...)
- object: matches only objects (not arrays)
- number: matches 10, 10.1, '10', '10.4'
- integer: matches '786' or 786 but not 10.1 or '10.5'
- boolean: matches '1', '0', 1, 0, true or false
- function matches only functions
You can use the modifier ^
at the beginning of any type in order to match anything but that type. For example ^string
matches any type of value but strings.
You can also define some accepted values. For example, if you want to allow only female
or male
on a gender field, use: { gender : 'string:male,female' }
Advanced usage
var SOverloading = require('simple-overloading');
function myFn() {
//-- let's first define our scope with its default values
var scope = {
full_name : '',
sex : '',
phone_number : null,
send_sms : true,
callback : null
};
//-- later we define our method variations
//myFn({String} full_name, {String} sex[ = male | female ])
var m1 = {
full_name : 'string',
sex : 'string:male,female'
};
//myFn({String} full_name, {String} sex[ = male | female ], {Number} phone_number)
var m2 = SOverloading.extendMethod(m1,{
phone_number : 'integer'
});
//myFn({String} full_name, {String} sex[ = male | female ], {Function} callback)
var m3 = SOverloading.extendMethod(m1,{
callback : 'function'
});
//myFn({String} full_name, {String} sex[ = male | female ], {Number} phone_number, {Function} callback)
var m4 = SOverloading.extendMethod(m2,{
callback : 'function'
});
/**
* 0 => didn't match any method
* > 0 && <= SOverloading => method matched
* > SOverloading => scope provided by object
*/
var methodFound = SOverloading.variation([m1,m2,m3,m4],scope,arguments);
if (methodFound < 1) {
throw new Error('Method not found!');
}
console.log('Matched with method ',methodFound);
console.log(scope);
scope.callback && scope.callback(scope);
}
function mycb() {
console.log('do whatever!');
}
myFn('Martín Rafael González','male');
/**
{ full_name: 'Martín Rafael González',
sex: 'male',
phone_number: null,
send_sms: true,
callback: null }
*/
myFn('Ana Sosa','female',40404040);
/**
{ full_name: 'Ana Sosa',
sex: 'female',
phone_number: 40404040,
send_sms: true,
callback: null }
*/
myFn('Rafael Cotiz','male','4442555510',mycb);
/**
{ full_name: 'Rafael Cotiz',
sex: 'male',
phone_number: 4442555510,
send_sms: true,
callback: [Function: mycb] }
*/
myFn('Olivia Gonzalez','feminine'); //throws error: method not foud...
1.0.0
9 years ago