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

ooplus-js

v0.1.2

Published

Utilities to improve object-oriented programming in Javascript and Typescript.

Downloads

2

Readme

ooplus.js

Utilities to improve object-oriented programming in JavaScript and Typescript.

Getting Started

You can get ooplus.js on npm.

Use Cases

Add functionalities an existing class

import { enhance, property } from "ooplus-js"

class MyClass {
    baseValue = 40;
}

interface EnhancedClass extends MyClass {
    newValue: number;
    newProperty: number;
    newMethod();
}

enhance(MyClass, {
    newValue: 2,
    newProperty: property({
        writable: true
    }),
    newMethod: function(this: EnhancedClass) {
        this.newProperty = this.newValue + this.baseValue;
    }
});

This code enhances MyClass to add two properties newValue, newProperty and a method newMethod.

A property can be added in two ways: with the property function and a PropertyDescriptor, or a direct value. Method is actually a property too, with a type of function.

Calling super

import { enhance, $super } from "ooplus-js"

class BaseClass {
    protected /*virtual*/ action() { 
        console.info("base action")
    }
}

class DerivedClass {
    
}

interface EnhancedDerivedClass extends DerivedClass {
    action();
}

enhance(DerivedClass, {
    action(this: EnhancedDerivedClass) {
        $super(this).action();
        console.info("derived action")
    }
})

The $super function here mimics the ES6 super syntax, to call a method defined in the base/ancestor class.

Dynamic inheritance

class BaseClass {
    constructor(readonly value: number) {}
}

interface DerivedClass extends BaseClass {
    value1: number;
    value2: number;
}

const derivedClass = extend(BaseClass, {
    preConstructor(value1: number, value2: number) {
        return [ value1 + value2 ];
    },
    
    constructor(value1: number, value2: number) {
        this.value1 = value1;
        this.value2 = value2;
    }
});

const instance = new derivedClass(22, 20);

The extend function works like the enhance function, but rather than enhancing the existed class, it creates a new class inheriting from the specified base class. This example dynamically creates a class derivedClass.

While using the enhance function, the new class' constructor is split into two functions: a preConstructor and a constructor. Both functions will receive the same parameters. The pre-constructor is like code before the super call which calls the base class' constructor in a typical ES6 class constructor code, it can handle pre-processing of the arguments, and should return an array of parameters which will be used to call the base class' constructor. The constructor function is like the code after the super call, which can do all sorts of initializations. The code above is analog to the following ES6 code:

class BaseClass {
    constructor(readonly value: number) {}
}

class DerivedClass extends BaseClass {
    constructor(value1: number, value2: number) {
        super(value1 + value2);
        this.value1 = value1;
        this.value2 = value2;
    }
});

Note in the preConstructor, the this object in the preConstructor is called a courier object, anything stored in it will be assigned to the instance created later. However it does not represent the class instance to be created. which will not be available until the constructor function (just like you can't use this before the super call in an ES6 class constructor).