Giter Club home page Giter Club logo

talentcomposer's Introduction

Build StatusCoverage StatusDependency status devDependency Status

NPM

talentcomposer

The talentcomposer is a tool which can compose pieces of capabilities (talents) with each other and also talents with an instance. This tool can help you to do role oriented programming. The implementation is based on the publication of the Software Composition Group part of the Institute of Computer Science (INF) at the University of Bern

##Supported engines

node v8 and v10

Notes

  1. The package mainliner utilizes this package.
  2. Conflict resolution between talents is explicit. The programmer has to resolve conflicts using either the alias or the exclude method. See below in the examples
  3. Conflict resolution between an instance and talents is implicit. The member will delegated on to the instance blindly. If there is already a member on the instance with same name it's gonna be shadowed or overridden.
  4. Although the publication allows stateful talents I would NOT not recommend to use them with this tool. Like DON'T do this (It's leaking anyway)
const Composer = require("talentcomposer");
let x = 0;

module.export = Composer.createTalent({
  method() {

    x += 1;

    return x;
  }
});

Examples

Composing an instance with talents

const assert = require("assert");
const Composer = require("talentcomposer");

class Person {
  constructor(name) {
    this.name = name;
  }
  eating() {}
  sleeping() {}
}

const studentTalent = Composer.createTalent({
  studying() {}
});

const teacherTalent = Composer.createTalent({
  teaching() {}
});

const sportTalent = Composer.createTalent({
  swimming() {}
});

let travis = new Person("Travis");
let mavis = new Person("Mavis");

travis = Composer.composeWithTalents(travis, studentTalent, sportTalent);
mavis = Composer.composeWithTalents(mavis, teacherTalent, sportTalent);

assert.ok(travis.studying);
assert.ok(travis.swimming);
assert.ok(mavis.teaching);
assert.ok(mavis.swimming);

Composing talents

const assert = require("assert");
const Composer = require("talentcomposer");

const circusTalent = Composer.createTalent({
  juggling() {},
  clowning() {}
});

const sportTalent = Composer.createTalent({
  running() {},
  swimming() {}
});

combinedTalents = Composer.composeTalents(circusTalent, sportTalent);

assert.ok(combinedTalents.juggling);
assert.deepEqual(combinedTalents.juggling, circusTalent.juggling);
assert.ok(combinedTalents.clowning);
assert.deepEqual(combinedTalents.clowning, circusTalent.clowning);
assert.ok(combinedTalents.running);
assert.deepEqual(combinedTalents.running, sportTalent.running);
assert.ok(combinedTalents.swimming);
assert.deepEqual(combinedTalents.swimming, sportTalent.swimming);

Require an unimplemented capability

const assert = require("assert");
const Composer = require("talentcomposer");

class Person {
  constructor(name) {
    this.name = name;
  }
  eating() {}
  sleeping() {}
}

const courierTalent = Composer.createTalent({
  "cycling": Composer.required,
  delivering() {
    this.cycling();
  }
});

const sportTalent = Composer.createTalent({
  cycling() {}
});

let travis = new Person("Travis");

travis = Composer.composeWithTalents(travis, courierTalent, sportTalent);

assert.ok(travis.cycling);
assert.deepEqual(travis.cycling, sportTalent.cycling);
assert.ok(travis.delivering);
assert.deepEqual(travis.delivering, courierTalent.delivering);

Require an unimplemented capability on the prototype of a class

const assert = require("assert");
const Composer = require("talentcomposer");

class Person {
  constructor(name) {
    this.name = name;
  }
  get drawing() {
    return Composer.required;
  }
  eating() {}
  sleeping() {}
}

const artTalent = Composer.createTalent({
  drawing() {}
});

let travis = new Person("Travis");

travis = Composer.composeWithTalents(travis, artTalent);

assert.ok(travis.drawing);
assert.deepEqual(travis.drawing, artTalent.drawing);

Require an unimplemented capability in the constructor of a class

const assert = require("assert");
const Composer = require("talentcomposer");

class Person {
  constructor(name) {
    this.name = name;
    this.painting = Composer.required;
  }
  eating() {}
  sleeping() {}
}

const artTalent = Composer.createTalent({
  painting() {}
});

let travis = new Person("Travis");

travis = Composer.composeWithTalents(travis, artTalent);

assert.ok(travis.painting);
assert.deepEqual(travis.painting, artTalent.painting);

Explicit conflict resolution (aliasing)

const assert = require("assert");
const Composer = require("talentcomposer");

const courierTalent = Composer.createTalent({
  cycling() {console.log("like a courier");}
});

const sportTalent = Composer.createTalent({
  cycling() {console.log("like an athlete");}
});

combinedTalents = Composer.composeTalents(
    courierTalent,
    Composer.alias(sportTalent, "cycling", "riding")
);

assert.ok(combinedTalents.cycling);
assert.deepEqual(combinedTalents.cycling, courierTalent.cycling);
assert.ok(combinedTalents.riding);
assert.deepEqual(combinedTalents.riding, sportTalent.cycling);

Explicit conflict resolution (excluding)

const assert = require("assert");
const Composer = require("talentcomposer");

const courierTalent = Composer.createTalent({
  cycling() {console.log("like a courier");},
  delivering() {}
});

const sportTalent = Composer.createTalent({
  cycling() {console.log("like a athlete");},
  swimming() {}
});

combinedTalents = Composer.composeTalents(
    courierTalent,
    Composer.exclude(sportTalent, "cycling")
);

assert.ok(combinedTalents.cycling);
assert.deepEqual(combinedTalents.cycling, courierTalent.cycling);
assert.ok(combinedTalents.cycling);
assert.deepEqual(combinedTalents.cycling, courierTalent.cycling);
assert.ok(combinedTalents.delivering);
assert.deepEqual(combinedTalents.delivering, courierTalent.delivering);

API

createTalent(record: Object): Talent

Creates a new talent

composeWithTalents(instance: Object, ...talents: Talent[]): Object

Composes talents with an instantiated object

composeTalents(...talents: Talent[]): Talent

Composes talents into one

alias(talent: Talent, methodName: string, alias: string): Talent

Aliases a method of the talent (It doesn't mutate the talent but returns a new one)

exclude(talent: Talent, methodName: string): Talent

Excludes a method of the talent (It doesn't mutate the talent but returns a new one)

required: Symbol

Property to mark required members for talents

talentcomposer's People

Contributors

dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.