1.1.2 • Published 10 years ago
overlord-js v1.1.2
Overloading javascript functions like a overlord
Overlord-js is a module which allows you to overload functions in a pseudo-functional style
Installation
NPM
npm install overlord-js --saveUsage
Usage on server-side
var overlord = require('overlord-js');
var say = overlord([
{
args: [String],
func: function(name) {
return "Hi, " + name;
}
}, {
args: [String, Number],
func: function(name, meters) {
return name + " ran " + meters + " meters";
}
}, {
args: [
String,
Number,
Array
],
check: [
/^S/,
7,
function(arr) {
if (arr.indexOf('commandos') !== -1) {
return true;
} else {
return false;
}
}
],
func: function(name, age, games) {
var game, gameStr, i, j, len;
gameStr = '';
for (i = j = 0, len = games.length; j < len; i = ++j) {
game = games[i];
if (i > 0 && i < games.length - 1) {
gameStr += ', ';
} else if (i === games.length - 1) {
gameStr += ' and ';
}
gameStr += game;
}
return "In " + age + " years, " + name + " was playing: " + gameStr;
}
}, {
args: ['any', 'any'],
func: function(var_1, var_2) {
if (var_1 === var_2) {
return "Equivalent";
} else {
return "Not equivalent";
}
}
}, {
args: [String, String, '...'],
func: function() {
return "We got " + arguments.length + " arguments";
}
}, {
args: [],
func: function() {
return "No one variable";
}
}
], function() {
return "Oops, custom call";
});
console.log( say('Sasha') ); // Hi, Sasha
console.log( say('Sasha', 10) ); // Sasha ran 10 meters
console.log( say('Sasha', 7, ['commandos', 'nfs', 'warcraft']) ); // In 7 years, Sasha was playing: commandos, nfs and warcraft
console.log( say('Sasha', 5, ['commandos', 'nfs', 'warcraft']) ); // Oops, custom call
console.log( say('Artem', 7, ['commandos', 'nfs', 'warcraft']) ); // Oops, custom call
console.log( say('Sasha', 7, ['hugo', 'nfs', 'warcraft']) ); // Oops, custom call
console.log( say(5, 'five') ); // Not equivalent
console.log( say(10, 10) ); // Equivalent
console.log( say('one', 'two', 'three', 'four') ); // We got 4 arguments
console.log( say('one', 'two', 'three', 'four', 'five') ); // We got 5 arguments
console.log( say() ); // No one variable
console.log( say(1, [], false) ); // Oops, custom callUsage on client-side
<html lang="ru-RU">
<head>
<meta charset="UTF-8" />
<title>Overlord.js - bow down before the Lord</title>
<script type="text/javascript" src="./overlord-v1.1.0-min.js"></script>
</head>
<body>
<script type="text/javascript">
var say = overlord([
{
args: [String],
check: [/^(?:S|A|L)/],
function: function(name) {
return "Hi, " + name;
}
}
], function() {
return "Oops, custom call";
});
console.log(say('Sasha')); // Hi, Sasha
console.log(say('Lesha')); // Hi, Lesha
console.log(say('Artem')); // Hi, Artem
console.log(say('Motja')); // Oops, custom call
console.log(say(1, [], false)); // Oops, custom call
</script>
</body>API
overlord(overloads, default, error)
arrayoverloads - array of functions.object- information about single functionarrayargs - array of prospective types of arguments. example:String, NumberdString,Number,Object,Array,Function,RegExp,Undefined- for the type of a variable'any'- for any type'...'- for zero or more any type. It is available from theargumentslikearguments[3]null- For zero variables
functionfunction or func - The called function, if types of arguments matcharraycheck - optional - array of expected value, regular expression or checking function of verifiable argumentString,Number,Object,Array- expected value of the argument. Validate by==operator. example:'foo'RegExp- a regular expression to validate the argument. example:/^www/Function- custom checking function. function should returntrueorfalsefor validate argument. example:function( arg ){ if(arg == 'foo') return true; else return false }
functiondefault - function() If none of the options didn't approach then this function will be called. default:undefinedbooleanerror - If none of the option didn't approach, default function not defited and it set to true, will be emit eventCannot read property 'apply' of undefined. default:false
Test
Run the mocha test
Current test runs for 8-10 ms
npm testOr for old
mocha test/test.v< version >.jsBuild form coffee source
Build project
gulp buildBuild only browser files
gulp browserifyClear all compiled files
gulp clearInnovations in version
1.1.0
custom arguments validation of overload
var say = overlord([
{
args: [String, Number, Array],
check: [
/^S/, // Using regular expressions
7, // expected value
function(arr) { // or custom function
if (arr.indexOf('commandos') !== -1) return true; // Valid
else return false;
}
],
func: function(name, age, games) {
// ...
}
])Changelog
1.1.2 Stable
Add- property of overloadcheck. It may be a expected value, regular expression or checking function of verifiable argument. ExampleAdd-funcalias forfunctionproperty of overload.Add- throw error when overload propertyfunc|functionorargsnot set
1.0.0 Stable
Add- first realise