# @yadah/mixin

> class mixin utilities

Latest version **0.2.0** (published 2023-04-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install @yadah/mixin
pnpm add @yadah/mixin
yarn add @yadah/mixin
bun add @yadah/mixin
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2023-04-08 |
| First published | 2023-04-08 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM |
| Node | >=18.3.0 |
| Dependencies | 0 |
| Unpacked size | 3.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ttoohey |
| Maintainers | ttoohey |
| Keywords | mixins |

## Links

- npm: https://www.npmjs.com/package/@yadah/mixin
- Repository: https://github.com/ttoohey/yadah
- Homepage: https://github.com/ttoohey/yadah/tree/master/packages/mixin#readme
- Issues: https://github.com/ttoohey/yadah/issues
- npm.io page: https://npm.io/package/@yadah/mixin

## Recent versions

- 0.2.0 (latest) — 2023-04-08

## README

# @yadah/mixin

Utilities for using class mixins

## `pipe()`

Execute a list of unary functions passing the output of one to the next.

```js
import { pipe } from "@yadah/mixin";

class superclass {}

function AMixin(superclass) {
  return class A extends superclass {
    aFn() {
      return "a";
    }
  };
}
function BMixin(superclass) {
  return class B extends superclass {
    bFn() {
      return "b";
    }
  };
}
function CMixin(superclass) {
  return class C extends superclass {
    cFn() {
      return "c";
    }
  };
}

const mixed = pipe(superclass, AMixin, BMixin, CMixin);

class ABC extends mixed {}

const abc = new ABC();
abc.aFn(); // 'a'
abc.bFn(); // 'b'
abc.cFn(); // 'c'
```

## `dedupe()`

A wrapper for mixins to allow a mixin to be used multiple times without
duplicating behaviour.

The typical usage is like:

```js
// CustomMixin.js
import { dedupe } from "@yadah/mixin";

function CustomMixin(superclass) {
  return class Custom extends superclass {
    fn() {
      super.fn();
      console.log("inside Custom.fn");
    }
  };
}
export default dedupe(CustomMixin);
```

If this mixin is then used multiple times in the some mixin pipe, it will
only appear once in the class prototype chain

```js
import { pipe } from "@yadah/mixin";
import CustomMixin from "./CustomMixin.js";

class superclass {
  fn() {}
}

class Example extends pipe(superclass, CustomMixin, CustomMixin) {}

new Example().fn();
// logs "inside Custom.fn" (once only)
```

`dedupe()` also adds a method to the wrapped mixin function that allows
checking if a class includes the mixin:

```js
CustomMixin.extends(Example);
// true
```

---
_Source: https://npm.io/package/@yadah/mixin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
