• Stars
    star
    151
  • Rank 246,057 (Top 5 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple way to overload JavaScript functions

overload.js

A simple way to overload a function depending on the type of the parameters passed to it.

Usage

Create a mathod taking different types and number of arguments by calling overload. The returned method can be called just like any other function.

var createPerson = overload([String], function(name){
	return "name: "+name;
}).when([String, Number], function(name, age){
	return createPerson(name)+", age: "+age;
}).when([String, Number, Function], function(name, age, callback){
	return callback(name, age);
});



createPerson("JavaScript") //returns 'name: JavaScript'
createPerson("JavaScript", 18) //returns 'name: JavaScript, age: 18'
createPerson("JavaScript", 18, function(name, age){
	return "age: "+age+", name: "+name;
}) //returns 'age: 18, name: JavaScript'

Fallback

If nothing matches, a fallback function will be called:

var myMethod = overload([String], function(value){
	return value.toUpperCase();
}).fallback(function(){
	throw Error("what happened here?" + arguments);
});


myMethod() //throws error

Constructors

You can create your own instances and use the constructor to match

function Person(name, age){
	this.name = name;
	this.age = age;
}

var getAge = overload([Person], function(person){
	return person.age
}).when([Number], function(age){
	return age;
});


getAge(new Person("JavaScript", 18)) //returns 18
getAge(18) //returns 18

MIT Licence

Copyright ©2013 Marius Gundersen All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

sparkline

JavaScript
89
star
2

module-pusher

Demo of SW + HTTP2 + ES6 modules
JavaScript
72
star
3

gulp-flatMap

process all the files in a gulp-stream individually
JavaScript
62
star
4

Samsara

Simple docker web interface
JavaScript
26
star
5

knockout-es6

es6 features in knockout
JavaScript
24
star
6

gulp-amd-optimizer

Emits the entire dependency tree of one or more AMD modules, from leaves to root, and names anonymous modules
JavaScript
22
star
7

descartes

Mock async JavaScript libraries
JavaScript
22
star
8

Origami3D

A simple 3D engine using the 2D canvas written in JavaScript
JavaScript
14
star
9

OnGameStart-2011

My presentation at OnGameStart 2011
JavaScript
10
star
10

Moquire

project for mocking AMD modules during testing
JavaScript
6
star
11

postfix-forward

docker container for forwarding emails to another server
Shell
5
star
12

website

My website
HTML
5
star
13

amd-optimizer

Optimize one or more AMD modules
JavaScript
4
star
14

Ekkiog

NAND logic
TypeScript
4
star
15

untab

JavaScript
4
star
16

EspruinoVSCode

TypeScript
4
star
17

WebSocketServer

A simple WebSocket server written in Java
Java
3
star
18

ajax-login

testing ajax login
JavaScript
2
star
19

jazzmine

Jazzed up Jasmine testing!
JavaScript
2
star
20

personnummer

JavaScript
2
star
21

PIGS

Progress Indicator via Gif Sockets
Java
2
star
22

MultitouchPresentation

Multitouch presentation at StandardsNext 2012
JavaScript
2
star
23

semver-dns

proof of concept semantic versioning dns system for Docker
JavaScript
2
star
24

BitcoinOTP

A One Time Pad encoder/decoder for bitcoin private keys
JavaScript
1
star
25

samsara-lib

JavaScript
1
star
26

vhost-delegator

node module for routing http requests to other node applications based on hostname
JavaScript
1
star
27

OrdnungJS

A framework for creating maintainable frontend applications
JavaScript
1
star
28

namecoin-transaction

Create the three namecoin transactions
JavaScript
1
star
29

OmegaVISP

Omega Verksted ISP programmer
C
1
star
30

continue-with-proposal

1
star
31

gulp-sourcemaps-test

JavaScript
1
star