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

autoclass

v0.4.3

Published

Define argument types for functions and automatically add methods to prototype.

Readme

AutoClass

Define argument types for functions and automatically add methods to prototype.

Installation

npm install autoclass

Example

import AutoClass from 'autoclass';

const Rider = AutoClass(
    'Rider',
    String,
    Number,
    function (nickname, number) {
        return {nickname, number};
    }
);

const ShowInfo = AutoClass(
    'ShowInfo',
    Rider,
    function ({nickname, number}) {
        console.log(`${nickname}, #${number}`);
    }
);

const dovizioso = Rider('Dovi', 4);
const iannone = Rider('The Maniac', 29);

dovizioso.ShowInfo(); // Logs: "Dovi, #4"
iannone.ShowInfo(); // Logs: "The Maniac, #29"

AutoClass(name[, ...type], func)

name: String or Any

A string to use as a method name. If an empty string or not a string, no methods are added.

type: Constructor or Type

Zero or more constructors. func arguments are validated using these corresponding types. Methods are added to prototypes of constructors. Methods are not added to array or variadic types.

func: Function

Function to execute, when instance as function or instance's method is invoked. func should have equal amount of named arguments as types are declared. func gets called with formatted and validated (see type) argument values. Its this value is set to object with argument names of func as keys and argument instances as values.

Returns: Instance
const MyClass = AutoClass(
    'MyClass',
    String,
    function (text) {
        return text;
    }
);

Type

Three kinds of types can be declared - basic, array and variadic.

const MyClass = AutoClass(
    'MyClass',
    String, // Basic types. Validated using `instanceof` operator.
    Object,
    [Number], // Array types. Each element is validated like basic type.
    [[String]], // Variadic type. Function may also have one variadic type. Validated like array type.
    Boolean, // More types...
    function (someText, obj, nums, texts, bool) {
        console.log('MyClass');
    }
);

MyClass('text', null, [1, 2, 3], 'a', 'b', 'c', true); // Logs: "MyClass"

Type conversions

If instanceof test fails, instance is passed to constructor.

const Rider = AutoClass(
    'Rider',
    String,
    Number,
    function (nickname, number) {
        console.log(typeof number); // Logs: "number"
        return {nickname, number};
    }
);

const dovizioso = Rider('Dovi', '04');

If class requires at least two types (variadic type is not considered), constructing from object literal is possible.

// `ShowInfo` requires `Rider`
ShowInfo({
    nickname: 'Petrux',
    number: 9
}); // Logs: "Petrux, #9"

Values and instances

Function arguments are values. Use this.argumentName to access instance.

const MyClass = AutoClass('MyClass', Number, function (number) {return number;});

const TestType = AutoClass(
    'TestType',
    MyClass,
    AutoClass,
    function (a, type) {
        console.log(a);
        console.log(this.a instanceof type);
    }
);

TestType(123, MyClass); // Logs: 123; true

Function and method calls return instance. Use .valueOf() to access return value.

const MyClass = AutoClass(
    'MyClass',
    Object,
    function (obj) {
        return obj;
    }
);

const ref = {};

console.log(MyClass(ref).valueOf() === ref); // Logs: true

Create classes

Validate inputs, return value.

const BikeNumber = AutoClass(
    'BikeNumber',
    Number,
    function (number) {
        if (isNaN(number)) {
            throw new Error('Bike number must be positive integer, got NaN.');
        }
        if (number < 1) {
            throw new Error(`Bike number must be positive integer, got ${number}.`);
        }
        return Math.floor(number);
    }
);

const Rider = AutoClass(
    'Rider',
    String,
    BikeNumber,
    function (nickname, bikeNumber) {
        return {nickname, bikeNumber};
    }
);

try {
    Rider('Kallio', -36); // Throws: "Error: Bike number must be positive integer, got -36."
} catch (e) {
    console.log(e);
}

try {
    Rider('The Doctor', 'VR46'); // Throws: "Error: Bike number must be positive integer, got NaN."
} catch (e) {
    console.log(e);
}

Method chaining

Functions that return nothing or undefined can be chained.

AutoClass(
    'SetNumber',
    Rider,
    BikeNumber,
    function (rider, bikeNumber) {
        rider.bikeNumber = bikeNumber;
    }
);

AutoClass(
    'ShowInfo',
    Rider,
    function ({nickname, bikeNumber}) {
        console.log(`${nickname}, #${bikeNumber}`);
    }
);

Rider('Stoner', 27)
    .ShowInfo() // Logs: "Stoner, #27"
    .SetNumber(1)
    .ShowInfo(); // Logs: "Stoner, #1"

Multiple methods from same type

Function may require same type multiple times. Primary method uses instance as first argument of its type. To use instance as another argument of its type, use instance.Method.argumentName(...other args).

AutoClass(
    'Append',
    String,
    String,
    function (to, text) {
        return to + text;
    }
);

// "Hello" as `to` argument
console.log('Hello'.Append(' world!').valueOf()); // Logs: "Hello world!"
// "Hello" as `text` argument
console.log('Hello'.Append.text(' world!').valueOf()); // Logs: " world!Hello"

License

ISC