auto-delegate

0.1.3 • Public • Published

Auto Delegate

A simple auto delegation utility which attaches all the properties of some delegate to a single property under the owner

NPM

Build Status Coverage Status

What for?

This can be helpful if you want to mixin some object instance and its functionality without directly extending the target object.

Here is some random example from the tests (sort of)

import delegate from 'auto-delegate';
 
// There is some counter class (babel-ified)
class Counter {
  count = 0;
  
  increment = () => this.count++;
  decrement = () => this.count--;
}
 
// There is some point thingo
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  
  get location = () => `(${this.x}${this.y})`;
}
 
/* Well here you can delegate to these two additional classes. Its not an amazing example 
 * but shows the idea. Its a bit more useful with a bigger object that also has its state 
 * spoiled when extending but this is just a demo */
class TrackedLocation {
  constructor() {
    delegate(this, new Counter(), '_counter');
    delegate(this, new Point(0,2), '_point');
  }
}
 
// And use the methods
let tracked = new TrackedLocation();
tracked.increment();
console.log(tracked.count); // 1
console.log(tracked.location); // '(0, 2)'

Readme

Keywords

Package Sidebar

Install

npm i auto-delegate

Weekly Downloads

0

Version

0.1.3

License

MIT

Last publish

Collaborators

  • lessonteacher