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

multi-extender

v1.0.1

Published

Simply extend one class with a multiple others

Downloads

7

Readme

About

This module is based on this Stack Overflow Answer. It however includes a few changes to help extend classes with the ability to add prefixes or suffixes to the property names.

The purpose is to provide for a straight forward way to extend any class with a multiple other classes.


const extender=require("multi-extender);

// This base class can have some methods and properties accessible in all Extension Classes
class BaseClass {}

// the multiple classes we want to extend BaseClass with
class ExtensionOne {}
class ExtensionTwo {}

const MyBaseClass = extender(ExtensionOne, ExtensionTwo)(BaseClass);

Prefixes & Suffixes

Sometimes you want to extend multiple classes, say ( ExtClassA & ExtClassB ), but ExtClassA & ExtClassB have properties with similar names which means the properties of ExtClassA get overwritten by ExtClassB, which is definitely not what you wish.

This is where you use prefixes and suffixes.

To declare a prefix/suffix, we use the private class features notation so that the prefix/suffix declaration is localized and private to the class.

  • To prefix we add the property #__prefix="your_prefix".
  • To suffix we add the property #__suffix="your_prefix".

Below is an example:

const extender = require('multi-extender');

// This is our base class
class Base {
	one() {
        return 'Base Method one'
    }
}

// Below are the classes we want to extend Base with
// ExtendOne uses a prefix
// ExtendTwo uses a suffix
// Both have methods named "one" and "two"

class ExtendOne {
	#__prefix = 'prefix_';

	one() {
		return 'Extend one Method one';
	}
	two() {
		return 'Extend one Method two';
	}
}

class ExtendTwo {
	#__suffix = '_suffix';

	one() {
		return 'Extend two Method one';
	}

	two() {
		return 'Extend two Method two';
	}

	three() {
		return 'Extend two Method three';
	}
}

// Tet us apply the extension
let Extended = extender(ExtendOne, ExtendTwo)(Base);

// Now let us test and see how the extensions have been done

// 1. Initialize the class
let Initialized = new Extended();

// 2. Pick all property names
var propertyNames = Object.getOwnPropertyNames(Extended.prototype);
// 3 Loop through each property with exception of 'constructor'
let propReturns = propertyNames
	.filter((n) => n !== 'constructor')
	.map((name) => {
		//4. call each method and get return value
		return { prop: name, return: Initialized[name]() };
	});

// log results
console.log(propReturns);

This code will log the following

[
  { prop: 'one', return: 'Base Method one' },
  { prop: 'prefix_one', return: 'Extend one Method one' },
  { prop: 'prefix_two', return: 'Extend one Method two' },
  { prop: 'one_suffix', return: 'Extend two Method one' },
  { prop: 'two_suffix', return: 'Extend two Method two' },
  { prop: 'three_suffix', return: 'Extend two Method three' }
]

This shows that all six methods exist and have been appropriately renamed.

You can check out these examples in the test directory.

Important to Note:

  • When you prefix or suffix classes, the methods will be renamed to "prefix" + "property" or "property" + "suffix". Therefore, if the prefix is declared as #__prefix="cls1_, then the method prop_one becomes cls1_prop_one and prop_one no longer exists!
  • Therefore, you must select proper prefix/suffix names, cognizant of the fact that you will need to easily access the renamed properties. So name your prefixes and suffixes as you would name variables i.e.
    • Use snake or camel case
    • Avoid symbols or exclamations.