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

classer

v1.3.0

Published

A dependent-less module utility to create `new`-less class instances that double as operator functions

Downloads

1,612

Readme

classer

A light-weight (dependent-less) module utility that provides new-less and asynchronous class instantiation for module exports that practice an Object-Oriented-Programming paradigm. Also includes a 7-channel colored console logger. Works in both node.js and browser.

Contents

Install

$ npm i --save classer

Use

The following module demonstrates an effective use members in an OOP fashion:

my-class.js:

// import classer
const classer = require('classer');

// symbol to access private members (except for methods that use public members)
let _private = Symbol();

// private method that can use public members
let $add_name = Symbol();  // less convenient than latter since each method requires own symbol


/**
* private static:
**/

// private static constant field
const S_NAME_DEFAULT = 'no-name';

// private static (mutatable) field
let s_class_message = 'none';


/**
* class:
**/
class MyClass {

    constructor(h_config={}) {

        // destruct config
        let {
            name: s_name,
        } = h_config;

        /**
        * private members:
        **/
        Object.assign(this, {
            [_private]: {

                // private field
                name: s_name || S_NAME_DEFAULT,

                // private method that mutates private fields (cannot call public methods!)
                reset() {
                    // `this` will be the [_private] object
                    this.name = S_NAME_DEFAULT;
                },
            },

            // private method that can access/mutate private fields AND call public methods
            [$add_name](s_name) {
                // mutate private field; `this` will be the instance object
                this[_private].name += ' '+s_name;

                // use public getter
                return this.name;
            },
        });
    }

    /**
    * public members:
    **/

    // public method to call public operator
    greet() {
        return this();
    }

    // public method to access private field
    getName() {
        return this[_private].name;
    }

    // public getter to access private field
    get name() {
        return this[_private].name;
    }

    // public setter to mutate private field
    set name(s_name) {
        this[_private].name = s_name;
    }

    // public method to call private method
    reset() {
        // call private method with `this[_private]` so it can access private members
        this[_private].reset();
    }

    // public method to mutate private field
    add(s_name) {
        // call private method with `this` so it can access public members
        this[$add_name](s_name);

        // return mutated private field
        return this[_private].name;
    }

    /**
    * public static members (getters/setters and methods only):
    **/

    // public static method to access private static field
    static getMessage() {
        return s_class_message;
    }

    // public static getter to access private static field
    static get message() {
        return s_class_message;
    }

    // public static setter to mutate private static field
    static set message(s_message) {
        s_class_message = s_message;
    }

    // public static method to access public static field
    static getSpecies() {
        return MyClass.species;
    }
}


// lets MyClass be called without `new` keyword
module.exports = classer.export(MyClass, function() {
    // optional operator() function that acts as handle to instance of this class
    return `My name is ${this[_private].name}`;
}, {
    /**
    * additional public static members including fields (overrides members defined by class on conflict):
    **/

    // public static field value (otherwise would be forced to use getter function in class-syntax)
    species: 'classic',

    // public static method
    help() {
        return `help yourself, i'm an ${MyClass.species} species!`;
    },
});

By exporting the class with classer.export(class, operator, staticMembers), and by using the coding convention above, you get the following API features:

index.js:

const MyClass = require('./my-class.js');

// instantiate class without `new`
let instance = MyClass({name: 'frank'});

// call instance handle as an operator
instance(); // 'My name is frank'

// proof that public method can access operator
instance.greet(); // 'My name is frank'

// access private field using a method
instance.getName(); // 'frank'

// access private field using a getter
instance.name; // 'frank'

// mutate private field using a method
instance.reset();
instance.name; // 'no-name'

// mutate private field using a setter
instance.name = 'frankie';
instance.name; // 'frankie'

// access a private static field using a public static method
MyClass.getMessage(); // 'none'

// access a private static field using a public static getter
MyClass.message; // 'none'

// mutate a private static field using a public static setter
MyClass.message = 'All your base are belong to us!';
MyClass.message; // 'All your base are belong to us!'

// access a public static field
MyClass.species; // 'none'

// mutate a public static field
MyClass.species = 'unknown';
MyClass.species; // 'unknown'

// proof that changes to a static field are reflected when using a public static method
MyClass.help(); // 'help yourself, i'm an unknown species!'

API


classer.export(class: class[, operator: {function|array|other}[, staticMembers: plainObject]])

Creates a function that instantiates class when invoked (with or without new operator). If operator is supplied, every instance of the class returned by the constructor will be that object, with its prototype chain set and any necessary properties overriden. If staticMembers is provided, all of its' members will be ammended to class, and setters/getters will be defined on the returned object to allow mutations without losing pointer references. See example above for more detail.


classer.exportAsync(class: class[, operator: {function|array|other}[, staticMembers: plainObject]])

Same as classer.export, except that instead of returning the instance, a callback (provided as the last argument to the constructor call) receives the instance. To support this, class must accept a callback function as the last parameter in its constructor. The constructor may override operator by passing an object to the callback. For example:

async-class.js

const classer = require('classer');
const _private = Symbol();

class AsyncClass {
    constructor(s_name, f_okay_async) {
        // go async
        setTimeout(() => {
            // finishing setting private fields...
            this[_private].name = s_name;
            
            // class instance is ready

            // fred! override the default operator
            if('fred' === s_name) {
                f_okay_async(function() {
                    return `${this[_private].name} rules!`;
                });
            }
            // use default operator
            else {
                f_okay_async();
            }
        }, 200);
    }
}

// export
module.exports = classer.exportAsync(AsyncClass, function operator() {
    return `My name is ${this[_private].name}`;
});

Then, from another script:

const AsyncClass = require('./async-class.js');

AsyncClass({name: 'george'}, (k_async) => {
    k_async();  // 'My name is george'
});

AsyncClass({name: 'fred'}, (k_async) => {
    k_async();  // 'fred rules!'
});

classer.logger(class_name: string)

classer.logger(class: class)

Creates a logger instance that includes 7 channels of colored output (works in both node.js and browser)

class MyClass {}
const local = classer.logger(MyClass);
/** OR **/
const local = classer.logger('MyClass');

/** THEN **/
local.log('pizza'); // prints: '[MyClass]+0.012s/ pizza'

The 7 log channels are as follows:

  • .log - calls console.log using default console color
  • .out - calls console.log using light cyan color
  • .good - calls console.log using green color
  • .info - calls console.info using blue color
  • .warn - calls console.warn using orange color
  • .error - calls console.error using red color
  • .fail - calls console.error using vibrant blood color, then throws an error