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

@narvin/mix

v1.1.1

Published

Creates a class composed of mixin classes.

Downloads

3

Readme

Mix

Create a class by composing mixin classes to form a prototype chain. You may also inherit from a single regular class or object that will go at the base of your prototype chain.

By using this pattern, you gain some of the benefits of composition while also retaining the efficiency of inheritance. Methods are shared by all instances of the class and super works, just like in normal inheritance.

Installation

> npm i @narvin/mix

Usage

Write classes that you want to use for composition as mixin class factories. The factory should take a Superclass parameter and return a class that extends that superclass. If the class has an explicit constructor, it should pass along the arguments for the superclass.

// This is a class factory
function createSpeaker(Superclass = Object) {
  return class Speaker extends Superclass {
    constructor(sound, ...superArgs) {
      super(...superArgs);
      this.sound = sound;
    }

    speak() {
      console.log(`I say ${this.sound}.`);
    }

    eat(amount) {
      super.eat(amount);
      console.log('That was yummy!');
    }
  };
}

// This is another class factory
function createEater(Superclass = Object) {
  return class Eater extends Superclass {
    constructor(energy, ...superArgs) {
      super(...superArgs);
      this.energy = energy;
    }

    eat(amount) {
      this.energy += amount;
    }
  };
}

Now you can create a new class composed of mixin classes.

// Prototype chain: Speaker -> Eater -> Object
const Cat = createSpeaker(createEater());

const mimi = new Cat('brawwr', 10);
mimi.speak(); // 'I say brawwr.'
mimi.eat(5); // 'That was yummy!'
console.log(mimi.energy); // 15

mix(...classFactories)

Streamline the composition of these classes.

import { mix } from '@narvin/mix'

// Prototype chain: Speaker -> Eater -> Object
const Cat = mix(createSpeaker, createEater);

const mimi = new Cat('brawwr', 10);
mimi.speak(); // 'I say brawwr.'
mimi.eat(5); // 'That was yummy!'
console.log(mimi.energy); // 15

mixClass(...classFactories, Baseclass)

Extend a single regular class then mix it with mixin classes. The regular class will come after the mixin classes in the prototype chain.

import { mixClass } from '@narvin/mix'

class Jumper {
  constructor(howHigh) {
    this.howHigh = howHigh;
  }

  jump() {
    console.log(`Uh, no. ${this.howHigh} is too high.`);
  }
}

// Prototype chain: Speaker -> Eater -> Jumper -> Object
const Cat = mixClass(createSpeaker, createEater, Jumper);

const mimi = new Cat('brawwr', 10, 8);
mimi.jump(); // 'Uh, no. 8 is too high.'

mixObject(...classFactories, baseObject)

Inherit from an object then mix it with mixin classes. The object will come after the mixin classes in the prototype chain.

import { mixObject } from '@narvin/mix'

const state = { favoriteToy: 'mouse' };

// Prototype chain: Speaker -> Eater -> state -> Object
const Cat = mixObject(createSpeaker, createEater, state);

const mimi = new Cat('brawwr', 10);
console.log(mimi.favoriteToy); // 'mouse'

mixSuperclass(Baseclass, ...requirements)

Use in mixin class factories when mixin classes depend on certain methods being in the prototype chain. The function checks if Baseclass contains all of the methods that the mixin class requires, then returns Baseclass or a mixin class composed of Baseclass and the mixin classes that would provide the missing methods.

You could create a class Eater that requires a digest method that is present in a Digester class like this:

function createDigester(Superclass = Object) {
  return class Digester extends Superclass {
    constructor(energy, ...superArgs) {
      super(...superArgs);
      this.energy = energy;
    }

    digest(amount) {
      this.energy -= 0.1 * amount;
    }
  };
}

function createEater(Baseclass = Object) {
  const Superclass = typeof Baseclass.prototype.digest === 'function'
    ? Baseclass : createDigester(Baseclass);
  return class Eater extends Superclass {
    constructor(energy, ...superArgs) {
      super(...superArgs);
      this.energy = energy;
    }

    eat(amount) {
      this.energy += amount;
      this.digest(amount);
    }
  };
}

But if Eater requires a digest method that is present in a Digester class, as well as chew and swallow methods that are present in a Masticater class, you could use mixSuperclass in createEater. If Baseclass contains digest, chew and swallow methods, then mixSuperclass will return Baseclass. Otherwise mixSuperclass will return a new Superclass that is composed of Baseclass and only the mixin classes that contain the methods that Baseclass does not have.

function createEater(Baseclass = Object) {
  const Superclass = mixSuperclass(
    Baseclass,
    { factory: createDigester, methods: ['digest'] },
    { factory: createMasticater, methods: ['chew', 'swallow'] },
  );
  return class Eater extends Superclass {
    constructor(energy, ...superArgs) {
      super(...superArgs);
      this.energy = energy;
    }

    eat(amount) {
      this.chew(amount);
      this.swallow(amount);
      this.digest(amount);
      this.energy += amount;
    }
  };
}