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

@customcommander/multifun

v1.0.1

Published

Clojure-inspired multimethods for JavaScript

Downloads

7

Readme

release release

multifun

Clojure-inspired multimethods for JavaScript.

Why?

Problem statement: JavaScript does not offer native constructs to facilitate the orchestration of functions.

Say we need to generate code that greets someone in a given programming language:

greetings('clojure', 'John');
//=> '(print "Hi John!")'

greetings('shell', 'John');
//=> 'echo "Hi John!"'

Here's one classic implementation:

const greetings = (lang, name) => {
  switch (lang) {
    case CLOJURE:
      return greetings_clojure(name);
    case SHELL:
      return greetings_shell(name);
    default:
      throw new Error(`unknown: ${lang}`);
  }
};

Let's take a moment to identify the important bits:

  1. We need some parameters (pink)
  2. We need a dispatch value to make a decision (yellow)
  3. We need a "decision tree" (green)
  4. We need a fallback (orange)

Everything else is just implementation details....

It is these details that multifun intends to manage so that you can focus on what matters the most:

How does it work?

const multifun = require('@customcommander/multifun');

const xyz =
  multifun
    ( dispatching_fn
    , 'xxx', handle_xxx_fn
    , 'yyy', handle_yyy_fn
    , 'zzz', handle_zzz_fn
    // ...
    , fallback_fn
    );

xyz(...);

Here are the parameters to multifun:

  1. The 1st parameter is the dispatching function. It takes all the parameters passed to xyz and returns a value.

  2. Then you have a serie of value & handler pairs. If a value is strictly equal to the dispatched value, its handler is applied to the parameters passed to xyz. (No other pairs will be evaluated.)

  3. And finally a fallback function if no values matched. It is applied to the parameters passed to xyz.