Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jimmyhmiller/multiple-methods
A micro library that implements multi methods in javascript.
https://github.com/jimmyhmiller/multiple-methods
Last synced: about 1 month ago
JSON representation
A micro library that implements multi methods in javascript.
- Host: GitHub
- URL: https://github.com/jimmyhmiller/multiple-methods
- Owner: jimmyhmiller
- License: mit
- Created: 2015-09-23T05:30:20.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-31T00:49:07.000Z (almost 6 years ago)
- Last Synced: 2024-10-10T15:11:31.278Z (about 1 month ago)
- Language: JavaScript
- Size: 57.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Multiple-methods
Multiple-methods is a micro library that implements multi methods in javascript. This allows you to do things like the following:
```javascript
var multi = require('multiple-methods');
var area = multi(_.property('shape'));
area.defaultMethod(() => 0);
area.method('circle', (s) => Math.PI * Math.pow(s.radius,2));
area.method('square', (s) => Math.pow(s.side, 2));area({shape:'circle', radius: 1});
// 3.141592653589793
area({shape:'square', side: 4});
// 16
area({shape:'nothing'});
// 0
```This library is incredibly small, so you should feel free to read the source. In fact you have seen everything that the library can do above. It only has one entry point "multi". Which has two properties, method and defaultMethod. It doesn't implement clojure style instance checking, although it would be easy to do yourself with it.
## Install
npm install --save multiple-methodsIf you find any bugs feel free to file and issue or put in a PR.