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 🙏

© 2026 – Pkg Stats / Ryan Hefner

generic-methods

v0.0.2

Published

Create composable generic methods or lift them from existing classes with minimum overhead

Readme

generic-methods

generic-methods is a generic methods library for JS. It's designed to help create composable polymorphic functions either from scratch or by lifting from existing classes.

Other existing generic methods implementations tend to try to emulate CLOS generic functions semantics which I don't really want (nor like), so I wrote this.

Usage

$ npm install --save generic-methods

In your .js file:

const Package = require('generic-methods');

Rationale

Imagine you're developing a polymorphic map() function. Suppose you want it to be able to map over arrays and objects and maybe even strings. You could approach implementing such a function like this in a vanilla JS:

function map(collection, func) {
    if (collection instanceof Array) {
        return collection.map(func);
    } else if (typeof(collection) === 'string') {
        return collection.split('').map(func).join('');
    } else {
        let result = {};
        Object.keys(collection).forEach(key => {
            result[key] = func(collection[key]);
        });
        return result;
    }
}

That's pretty dense, but it works. And yet it's somewhat difficult to extend. What if next time you implement a LinkedList class and want to map over it, too? You'd have to introduce yet another else if clause in your code. After several iterations the code will simply become unreadable.

Ideally, you could easily solve this OOP way by making every class whose instances you want to map over implement a common interface, say, Mappable. Obviously, you can't do that since Array and String and Object are standard library classes. If you feel hacky you might monkey patch them, but that'd be gross and could break third party libraries.

Generic methods provide an elegant solution to this problem by decoupling objects from actions they can perform. It's still OOP, but methods aren't properties anymore, they exist somewhere outside of the object and yet are dispatched as needed when needed.

This is how you'd approach the problem with generic-methods:

const Package = require('generic-methods');

const package = new Package();

package.define('map', Array, (self, func) => {
    return self.map(func);
});

package.define('map', 'string', (self, func) => {
    return self.split('').map(func).join('');
});

package.define('map', Object, (self, func) => {
    let result = {};
    Object.keys(self).forEach(key => {
        result[key] = func(self[key]);
    });
    return result;
});

And this is how you'd use it:

const { map } = package.bindings;

const twice = x => x + x;

console.log(map([1, 2, 3], twice)); // [2, 4, 6]

console.log(map('sausage', twice)); // 'ssaauussaaggee'

console.log(map({ foo: 1, bar: 2 }, twice)); // { foo: 2, bar: 4 }

Extending your new map() to support more classes is trivial, and you can even scope implementation of map() for you class within its module, just make sure package is visible from there.

generic-methods also provides a facility for lifting methods from existing classes, that is extracting them in a package as stand-alone functions that accept class instance as a first argument. It's ridiculously simple:

const liftedArray = new Package('LiftedArray');

liftedArray.lift(Array);

// Now Array.prototype methods are available from liftedArray.bindings

const { reverse } = liftedArray.binings;

console.log(reverse([1, 2, 3])); // [3, 2, 1]