Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mixinable

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mixinable

[![travis](https://img.shields.io/travis/dmbch/mixinable.svg)](https://travis-ci.org/dmbch/mixinable) [![npm](https://img.shields.io/npm/v/mixinable.svg)](https://www.npmjs.com/package/mixinable)

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
177
decreased by-9.23%
Maintainers
1
Weekly downloads
 
Created
Source

Mixinable

travis npm

mixinable is a small utility library allowing you to use mixins in your code. Apart from enabling you to add and override new methods to your prototypes, it helps you apply different strategies to those additional methods.

mixinable keeps your protoype chain intact (instanceof works as expected), allows you to provide custom constructor functions and supports asynchronous methods returning Promises.

Installation

Using NPM:

npm install -S mixinable

Using Yarn:

yarn add mixinable

API

mixin([...definition])

The main export of mixinable is a variadic function accepting any number of mixin definitions. It returns a constructor/factory function that creates instances containing the mixin methods you defined.

mixin.replace([...implementation])

replace is the default mixin method application strategy. It mimics the behavior of, for example, Backbone's extend implementation. replace accepts any number of functions, i.e. implementations.

mixin.parallel([...implementation])

parallel executes all defined implementations in parallel. This is obviously most useful if there are asynchronous implementations involved - otherwise, it behaves identically to sequence.

mixin.pipe([...implementation])

pipe passes the each implementation's output to the next, using the first argument as the initial value. All other arguments are being passed to all implementations as-is.

mixin.sequence([...implementation])

sequence executes all implementation sequentially, passing all arguments unchanged. Use it if your implementations might rely on others changing the instance they are run on.

Examples

Basic Example
import mixin from 'mixinable';

// const Foo = mixin({
const createFoo = mixin({
  bar() {
    // ...
  }
});

// const foo = new Foo();
const foo = createFoo();

// console.log(foo instanceof Foo);
console.log(foo instanceof createFoo);
Multiple Mixins Example
import mixin from 'mixinable';

// multiple mixin arguments are being merged
const createFoo = mixin(
  {
    bar() {
      // ...
    }
  },
  {
    // you can pass multiple implementations at once
    baz: [
      function () { /* ... */ },
      function () { /* ... */ }
    ]
  }
);

// constructors/factories created with mixin can be extended
const createQux = createFoo.mixin({
  bar() {
    // ...
  }
});
parallel Example
import mixin, { parallel } from 'mixinable';

// const Foo = mixin({
const createFoo = mixin({
  bar: parallel(
    function () {
      return Promise.resolve(1);
    },
    function () {
      return Promise.resolve(2);
    }
  )
});

const foo = createFoo();

foo.bar().then(res => console.log(res));
// [1, 2]
pipe Example
import mixin, { pipe } from 'mixinable';

// const Foo = mixin({
const createFoo = mixin({
  bar: pipe(
    function (val, inc) {
      return Promise.resolve(val + inc);
    },
    function (val, inc) {
      return (val + inc);
    }
  )
});

const foo = createFoo();

foo.bar(0, 1).then(res => console.log(res));
// 2
sequence Example
import mixin, { sequence } from 'mixinable';

// const Foo = mixin({
const createFoo = mixin({
  bar: sequence(
    function (options) {
      this.baz = options.baz;
    },
    function (options) {
      this.qux = this.bar * 42;
    }
  )
});

const foo = createFoo();

foo.bar({ baz: 23 });
console.log(foo.qux);
// '966'

Contributing

If you want to contribute to this project, create a fork of its repository using the GitHub UI. Check out your new fork to your computer:

mkdir mixinable && cd $_
git clone git@github.com:user/mixinable.git

Afterwards, you can yarn install the required dev dependencies and start hacking away. When you are finished, please do go ahead and create a pull request.

mixinable is entirely written in ECMAScript 5 and adheres to semistandard code style. Please make sure your contribution does, too.

FAQs

Package last updated on 13 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc