How to use the stampit.compose function in stampit

To help you get started, we’ve selected a few stampit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stampit-org / stampit / test / types / tests.ts View on Github external
}
  },
  props: {
      members: {}
  }
});

// Let's set some defaults:
const defaults = stampit().props({
  name: 'The Saloon',
  specials: 'Whisky, Gin, Tequila'
});

// Classical inheritance has nothing on this. No parent/child coupling. No deep inheritance hierarchies.
// Just good, clean code reusability.
const bar = stampit.compose(defaults, membership);
// Note that you can override props on instantiation:
const myBar = bar({ name: 'Moe\'s' });
// Silly, but proves that everything is as it should be.
myBar.add({ name: 'Homer' }).open().getMember('Homer');

const myStamp = stampit().methods({
  foo() {
      return 'foo';
  },
  methodOverride() {
      return false;
  }
}).methods({
  bar() {
      return 'bar';
  },
github stampit-org / stampit / test / types / tests.ts View on Github external
const a = options.args[0];
  this.getA = () => {
      return a;
  };
});
a(); // Object -- so far so good.
a().getA(); // "a"

const b = stampit().init(function() {
  const a = 'b';
  this.getB = () => {
      return a;
  };
});

const c = stampit.compose(a, b);
const foo = c(); // we won't throw this one away...
foo.getA(); // "a"
foo.getB(); // "b"

// Here's a mixin with public methods, and some props:
const membership = stampit({
  methods: {
      // members: {},
      add(member: any) {
          this.members[member.name] = member;
          return this;
      },
      getMember(name: any) {
          return this.members[name];
      }
  },
github tdzienniak / entropy / packages / entropy / src / Engine.js View on Github external
import { compose } from 'stampit';
import isStamp from 'stampit/isStamp';
import FastArray from 'fast-array';

import EventEmitter from './EventEmitter';
import Pool from './Pool';
import { isNonEmptyString } from './helpers';

/**
 * This module manages the state of entities, components and systems. The heart of Entropy.
 *
 * @class Engine
 * @extends EventEmitter
 */
const Engine = compose({
  init(opts) {
    // entity ids start from 1, 0 means uninitailized or disabled entity
    let greatestEntityID = 1;

    /**
     * When entity is removed, it's ID can be reused by new entities. This pool stores old IDs ready to reuse.
     *
     * @private
     * @name _entitiesIdsPool
     * @memberof Engine#
     * @type Pool
     */
    this._entitiesIdsPool = Pool({
      _new() {
        return greatestEntityID++;
      },
github tdzienniak / entropy / packages / entropy / src / ComponentStore.js View on Github external
register(descriptor) {
      this._factories[descriptor.type] = compose(Component, {
        methods: pickBy(descriptor, value => isFunction(value)),
      });

      // generating `id` for class of components
      this._componentsIdsMap[descriptor.type] = this._greatestComponentId;
      this._greatestComponentId += 1;

      this._pools[descriptor.type] = Pool({
        _new: (...args) => {
          const component = this._factories[descriptor.type]({
            type: descriptor.type,
            id: this._componentsIdsMap[descriptor.type],
          });

          component.onCreate(...args);
github ericelliott / irecord / test / stampit.js View on Github external
assert.plan(1);

  const record = irecord({
    a: 'a',
    b: 'b'
  });

  const stamp = stampit({
    methods: {
      countProps () {
        return Object.keys(this.toJS()).length;
      }
    }
  });

  const composedRecord = stampit.compose(
    record.stamp,
    stamp
  ).create();

  assert.equal(composedRecord.countProps(), 2,
    'should expose irecord methods');
});
github PacktPublishing / Node.js_Design_Patterns_Second_Edition_Code / Chapter06 / 05_factory_composable / game.js View on Github external
const shooter = stampit()
  .props({
      bullets: 6
  })
  .methods({
    shoot(direction) {
      if (this.bullets > 0) {
        --this.bullets;
        console.log(`${this.name} shoot to the ${direction}`);
      }
    }
  })
;

const runner = stampit.compose(character, mover);
const samurai = stampit.compose(character, mover, slasher);
const sniper = stampit.compose(character, shooter);
const gunslinger = stampit.compose(character, mover, shooter);
const westernSamurai = stampit.compose(gunslinger, samurai);

const gojiro = westernSamurai();
gojiro.name = 'Gojiro Kiryu';
gojiro.move(1,0);
gojiro.slash('left');
gojiro.shoot('right');
github PacktPublishing / Node.js_Design_Patterns_Second_Edition_Code / Chapter06 / 05_factory_composable / game.js View on Github external
const shooter = stampit()
  .props({
      bullets: 6
  })
  .methods({
    shoot(direction) {
      if (this.bullets > 0) {
        --this.bullets;
        console.log(`${this.name} shoot to the ${direction}`);
      }
    }
  })
;

const runner = stampit.compose(character, mover);
const samurai = stampit.compose(character, mover, slasher);
const sniper = stampit.compose(character, shooter);
const gunslinger = stampit.compose(character, mover, shooter);
const westernSamurai = stampit.compose(gunslinger, samurai);

const gojiro = westernSamurai();
gojiro.name = 'Gojiro Kiryu';
gojiro.move(1,0);
gojiro.slash('left');
gojiro.shoot('right');
github PacktPublishing / Node.js_Design_Patterns_Second_Edition_Code / ch06 / 05_factory_composable / game.js View on Github external
const shooter = stampit().
    props({
        bullets: 6
    }).
    methods({
        shoot(direction) {
            if (this.bullets > 0) {
                --this.bullets;
                console.log(`${this.name} shoot to the ${direction}`);
            }
        }
    })
;

const runner = stampit.compose(character, mover);
const samurai = stampit.compose(character, mover, slasher);
const sniper = stampit.compose(character, shooter);
const gunslinger = stampit.compose(character, mover, shooter);
const westernSamurai = stampit.compose(gunslinger, samurai);

let gojiro = westernSamurai();
gojiro.name = 'Gojiro Kiryu';
gojiro.move(1,0);
gojiro.slash('left');
gojiro.shoot('right');

stampit

Create objects from reusable, composable behaviors.

MIT
Latest version published 4 years ago

Package Health Score

62 / 100
Full package analysis