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

mix-classes

v1.1.10

Published

Seamlessly combine class inheritance with composition, guaranteed to work with any class

Downloads

4,003

Readme

mix-classes

Easily add typescript-safe mixins to JS classes, with support for generics, constructors, overloading and more. Correctly handles this for each class, so it'll work with anything.

  • Typescript generics
  • Pass arguments to mixins, by providing an array of arguments
  • Supports super calls in overloaded methods
  • Use instanceof to check for mixin classes
  • Handles the this inside classes, so that they always access their local scope first. No need to worry about name-collisions
import { Mix } from 'mix-classes'

class Contactable {
  constructor(public email: string, public phone?: string) {}
}
class Nameable {
  constructor(public name: string) {}
}
class Website {
  constructor(public websiteUrl: string) {}
}

class Developer extends Mix(Nameable, Contactable, Website) {
  constructor() {
    super(['Bob'], ['[email protected]'], ['https://example.com'])
  }
}

class Company extends Mix(Nameable, Contactable) {
  constructor() {
    super(['Apple'], ['[email protected]', '18-00'], ['https://apple.com'])
  }
}

const developer = new Developer()
developer.name
developer.email
developer.websiteUrl

const company = new Company()
company.name
company.email
company.phone
company.websiteUrl

Constructor arguments

You can pass custom constructor arguments to each mixin within an array inside the super call. The arguments order is dependant on the mix array order.

import { Mix } from 'mix-classes'

class Nameable {
  constructor(public name: string) {}
}

class Ageable {
  constructor(public age: number) {}
}

class Person extends Mix(Nameable, Ageable) {
  constructor() {
    super(['Bob'], [50])
    //     ^ name argument for Nameable
    //              ^ age argument for Ageable
  }
}

Overloading

All mixins are seperate classes with different this values, meaning you don't need to worry about name collisions.

import { Mix, getMixin } from 'mix-classes'

class A {
  variable = 'a'
  public a() {
    return this.variable
  }
}

class B {
  variable = 'b'
  public b() {
    return this.variable
  }
}

class Test extends Mix(A, B) {
  constructor() {
    super()

    // The default value is the last mixin specified
    console.log(this.variable) // 'b'

    // Use getMixin to get overloaded properties
    console.log(getMixin(this, A).variable) // 'a'

    // Mixins retain access to their local variables
    this.a() // 'a'
    this.b() // 'b'
  }
}

const test = new Test()

Typescript generics

Typescript generics are supported, but it requires using Typescript's declaration merging.

To use them, simply wrap the class that you want to pass generics to in Generic(), and then add an interface with the same name as the class you want it in.

Before:

import { Mix } from 'mix-classes'

class MyClass<T extends string = 'initial'> extends Mix(
  User,
  Nameable,
  Ageable
) {}

After:

import { Generic, Mix } from 'mix-classes'

// Move all generic type signatures to the interface
// including default values.
interface MyClass<T extends string = 'initial'> extends User<'bob'> {}

class MyClass<T> extends Mix(Generic(User), Nameable, Ageable) {}
import { Mix, Generic } from 'mix-classes'

class B {}

class Role<Type extends string> {
  constructor(public type: Type) {}
}

class User<Username extends string> {
  constructor(public username: Username) {}
}

interface Admin extends User<'bob'>, Role<'admin'> {}

class Admin extends Mix(Generic(User), Generic(Role), B) {
  constructor() {
    super(['bob'], ['admin'])
  }
}

const test = new Admin()
test.username // type 'bob'