npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

def.js

v0.1.6

Published

A multiple-dispatch function overloading library.

Downloads

35

Readme

def.js

A multiple-dispatch function overloading library for JavaScript.

Examples

Function overloading:

var def = require('def.js');

var fibonacci = def(Number       , function(n) { return fibonacci(n-1) + fibonacci(n-2); })
               .def(Number.max(1), function(n) { return 1; });

fibonacci(0); // 1
fibonacci(1); // 1
fibonacci(2); // 2
fibonacci(3); // 3

Simple router for node.js HTTP servers:

var http = require('http'),
    def = require('def.js'),
    handler = def();

handler.def({url : '/hello'}, function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello\n');
});

handler.def({url : '/world'}, function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('World\n');
});

http.createServer(handler).listen(1337, '127.0.0.1');

Usage

Overloaded functions can be created using the def function. It returns a dispatcher function, which will later check the function arguments, and call the appropriate implementation functions. Implementation functions are always associated with a function signature, and can be defined with the def method of the dispatcher. The first implementation and signature may be defined when initally calling the standalone def function.

The first parameters of the def function are called the function signature or the filters. Filters are described as JS schema. The first filter applies to the first argument, the second applies to the second, etc. A typical function overloading with signature definitions looks like this:

f = def(Number, String        , function(n, s) { console.log(n, s); })
   .def(42    , /The Answer.*/, function(n, s) { console.log('The answer is: ' + s); });

Function call order

The most generic implementation must come first, then the special cases. It is explained by the way def.js executes the implementation functions. It goes like this:

  1. Select the last defined unchecked function implementation and signature
  2. Check the function signature against the function call's arguments and the value of this
  3. If the signature matches the arguments and the value of this:
  • Execute the implementation function with the given arguments and this
  • If the return value ret is not undefined then stop executing and return ret
  1. Go to step 1 if there are unchecked implementations left

Filter for this

The filter for this is also part of the function signature. To define a filter for the value of this, use def.call or def.apply.

print = def.call(Number, function(comment) { console.log('Number: ' + this + ' (' + comment + ')'); })
       .def.call(String, function(comment) { console.log('String: ' + this + ' (' + comment + ')'); });

print.call(1, 'an important number'); // Number: 1 (an important number)
print.call('a', 'the first letter');  // String: ` (the first letter)

License

The MIT License

Copyright (C) 2012 Gábor Molnár [email protected]