Giter Site home page Giter Site logo

liamgoodacre / multifn.js Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 96 KB

Provides a method of combining multiple functions into one, and selecting one to execute based on the arguments passed to it.

License: Other

CoffeeScript 52.98% JavaScript 47.02%

multifn.js's Introduction

MultiFn.js

MultiFn is a JavaScript utility which allows multiple implementations of a function based on different arguments. i.e. if you want different code to run if a function is called with an Array as opposed to an Object.

MultiFn.js was authored by Liam Goodacre using Coffee-Script.

Example

Check out the "[src/out]/test/" folders for example(s).

Syntax

CoffeeScript:

myFn = new MultiFn {
"Number Number": (_n1, _n2) -> myFn(_n1) + myFn(_n2)
"Number":  (_num) -> _num
"String": (_str) -> parseInt(_str, 10)
}
JavaScript:
var myFn = new MultiFn({
"Number Number": function (_n1, _n2) { return myFn(_n1) + myFn(_n2); },
"Number": function (_num) { return _num; },
"String": function (_str) { return parseInt(_str, 10); }
});

Defaults

MultiFn supports having a default implementation function, should none of the argument patterns match. The syntax is as follows:

CoffeeScript:

someFn = new MultiFn {
#...other functions...
"default": ->
# some default implementation
#...other functions...
}

JavaScript:

var someFn = new MultiFn({
//...other functions...
"default": function () {
// some default implementation
},
//...other functions...
});

Types

The string of types identifying the arguments for an implementation, is a space delimited sequence of regular expressions.

This means that instead of writing "Number", you could write "^Num", or event just "^N". It also means you can do things like "Element$" which would match "HTMLElement", "HTMLBodyElement", etc...

Another adavantage to this, is that ".*" will match any type of value:

"Number .* Number": (_num1, _anything, _num2) -> #...

multifn.js's People

Contributors

liamgoodacre avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.