Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anywhichway/js-generics
A generic function definition and dispatch library for Javascript.
https://github.com/anywhichway/js-generics
Last synced: about 4 hours ago
JSON representation
A generic function definition and dispatch library for Javascript.
- Host: GitHub
- URL: https://github.com/anywhichway/js-generics
- Owner: anywhichway
- License: mit
- Created: 2015-11-30T11:50:20.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-27T16:07:26.000Z (almost 9 years ago)
- Last Synced: 2024-09-18T11:46:01.642Z (about 2 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# js-generics
A generic function definition and multi-dispatch library for JavaScript.Multi-dispatch generic functions support the selection and execution of a function based on the types and number of arguments.
[![Build Status](https://travis-ci.org/anywhichway/js-generics.svg)](https://travis-ci.org/anywhichway/js-generics)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/9fa47bc000d84db4b1336fc61e38fb58)](https://www.codacy.com/app/syblackwell/js-generics)
[![Code Climate](https://codeclimate.com/github/anywhichway/js-generics/badges/gpa.svg)](https://codeclimate.com/github/anywhichway/js-generics)
[![Test Coverage](https://codeclimate.com/github/anywhichway/js-generics/badges/coverage.svg)](https://codeclimate.com/github/anywhichway/js-generics/coverage)
[![Issue Count](https://codeclimate.com/github/anywhichway/js-generics/badges/issue_count.svg)](https://codeclimate.com/github/anywhichway/js-generics)[![NPM](https://nodei.co/npm/js-generics.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm//)
# Installation
npm install js-generics
The index.js and package.json files are compatible with [node-require](http://www.github.com/anywhichway/joqular) so that js-generics can be served directly to the browser from the node-modules/js-generics directory when using node Express.
# Usage
Multi-dispatch generic functions support the selection and execution of a function based on the types and number of arguments. A common use is supporting mathematical operations on different types of objects. For example:
```
var sum = generic(function() { throw new Error("can't sum " + JSON.stringify(arguments)); } )
.method("number","number",
function(arg1,arg2) { return arg1 + arg2; })
.method(function(arg) { return arg instanceof Array;},"number",
function(arg1, arg2) { arg1.forEach(function(item,i) { arg1[i] += arg2; }); return arg1; });
```In the example above the generic function *sum* is defined to throw an error if no matching dispatch can be found. Matching dispatches have two arguments of type number or a first argument of kind array and a second of type number.
The general form of usage is:
```
generic().method([,...],)[.method([,...],),...]
```*\* - This function is called if no matching dispatches are found. It can take any form and is called as *.apply(this,arguments)*. As a result generic functions can be included as methods on objects.
*\* - These should be primitive type names or functions. If a function, it's sole responsibility is to return *true* or *false* if the argument type is correct. The number of \ must match the number of arguments to \, or the last \ must be the special value *generic.VARGS*.
*\* - This function does the desired work. It is called as *\.apply(this,arguments)*.
.*method* calls can be chained and the last matching method is the one invoked by the dispatcher.
# Release History (reverse chronological order)
v0.0.6 2016-01-03 Corrected markdown issues is README.
v0.0.5 2016-01-03 Added unit tests and documentation.
v0.0.4 2015-12-13 Codacy improvements
v0.0.3 2015-12-13 Initial public commit.
# License
MIT License - see LICENSE file