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

mixin-util

v1.0.10

Published

A mixin utility for ES2015 classes

Downloads

15

Readme

mixin-util

A mixin utility for ES2015 Classes

Basic usage

The function accepts the names of mixins or mixin fnctions

const mixinUtil = require('mixin-util');

class MyCoolClass extends mixinUtil('settings', 'middleware') {
  constructor() {
    // nothing will work if super is not called
    super();
  }
}

let mcc = new MyCoolClass();

mcc.set('test', 'works');

// log -> works
console.log(mcc.get('test'));

Mixins

Middleware

this mixin adds middleware functionality similar to that of express.

.use

This method will add functions to the middleware stack. Any arrays passed will be flattened. For eaxmple,

.use(function () {}, [function () {}, function () {}]);

... is the same as...

.use(function () {}, function () {}, function () {});

Error handling middleware ware should accept and error, the expected arguments, and then next. To forward and error to the error handler simply pass it to the next function or throw the error.

.handle

This method will exacute the middleware stack. It accepts the arguments to be passed to the middle functions.

Example:

const mixinUtil = require('mixinUtil');

class MyMiddleware extends mixinUtil('middleware') {
  constructor() {
    super();
  }
}

let middle = new MyMiddleware();

middle.use(function (err, icecream, next) {
  // called because of bacon
  // log -> eww bacon icecream
  console.log(err);
});

middle.use(function (icecream, next) {
  icecream.add('cherry');
  next();
});

middle.use(function (icecream, next) {
  icecream.add('bacon');
  next();
});

middle.use(function (icecream, next) {
  if(icecream.is('bacon'))
  next(new Error("eww bacon icecream"));

  // next can only be called once so this is ok
  next();
});

// call stack
middle.handle(icecream);

Settings

This mixin adds setting functionality. For Each of these methods the instance methods applies to the instance and the static method applies to the prototype.

.set

This method accepts the name of the setting and the new value

.set('test', 'works')

.get

This method accepts the name of the setting and returns the value of that setting

.get('test')
// returns 'works'

.Setter

This method will assign a setter to a setting. The setter function should accept a value and return the transformed value.

.Setter('name', function uppercase(name) {
  return name.toUpperCase();
});

.Getter

This method will assign a getter to a setting. You can think of a getter as a imaginary setting. The getter function should accept a object containing all of the settings.

// Settings -> {
//   name: {
//     first: 'john',
//     last: 'doe',
//   }
// }

.Getter('name.full', function fullname(settings) {
  return `${settings.name.first} ${settings.name.last}`;
});

Mixin Pattern

function myMixin(parentClass) {
  // private variables
  let _private = Symbol("private");

  class MyMixin extends parentClass{
    constructor() {
      super();
      this[_private] = "hi";
    }
  }

  return MyMixin
}

License

MIT