How to use stampit - 10 common examples

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 / EventEmitter.js View on Github external
* Very simple event emitter implementation.
 *
 * @example
 * const ee = EventEmitter()
 *
 * ee.on('myEvent', (e) => {
 *   console.log(e.foo);
 * })
 *
 * ee.emit('myEvent', {
 *    foo: 'bar',
 * }) // prints `bar` in the console
 *
 * @class EventEmitter
 */
const EventEmitter = stampit({
  init() {
    /**
     * Object with registered event listeners. Keys are event names.
     *
     * @private
     * @memberof EventEmitter#
     * @name _events
     * @type Object
     */
    this._events = {};
    this._responding = true;
  },
  methods: {
    /**
     * Method used to register event listener.
     *
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 ericelliott / feature-toggle / feature-toggle-client.js View on Github external
activeFeatures = 
            activeFeatures.filter(function (feature) {
              return !contains(features, feature);
            });
          setFlags(activeFeatures);
          this.emit('deactivated', features);
          return this;
        }
      },

      // Creates the feature toggle object by
      // composing the methods above with an
      // event emitter using the Stampit
      // prototypal inheritance library.
      ft = stampit.compose(
        stampit.convertConstructor(EventEmitter),
        stampit(methods)
      ).create();

    // Kick things off by setting feature classes
    // for the currently active features.
    setFlags(activeFeatures);

    return ft;
  };
github ericelliott / feature-toggle / dist / feature-toggle-client.js View on Github external
activeFeatures = 
            activeFeatures.filter(function (feature) {
              return !contains(features, feature);
            });
          setFlags(activeFeatures);
          this.emit('deactivated', features);
          return this;
        }
      },

      // Creates the feature toggle object by
      // composing the methods above with an
      // event emitter using the Stampit
      // prototypal inheritance library.
      ft = stampit.compose(
        stampit.convertConstructor(EventEmitter),
        stampit(methods)
      ).create();

    // Kick things off by setting feature classes
    // for the currently active features.
    setFlags(activeFeatures);

    return ft;
  };
github sweet-js / sweet-core / src / stamps.js View on Github external
import stampit from "stampit";

export const Cloneable = stampit.init(({ instance, stamp }) => {
  // Avoid adding the same method to the prototype twice.
  if (!stamp.fixed.methods.clone) {
    stamp.fixed.methods.clone = function () {
      return stamp(this);
    };
  }
});

export const Frozen = stampit().init(({instance}) => {
  Object.freeze(instance);
});
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 char0n / page-objects / test / e2e / support / WebElementEnhanced.js View on Github external
'use strict';

const stampit = require('stampit');
const { pipe, split, includes } = require('ramda');
const { isFunction } = require('ramda-adjunct');


const WebElementEnhanced = stampit
  .props({
    webElement: null,
  })
  .init(function ({ webElement }) {
    this.webElement = webElement;
  })
  .methods({
    async containsClassName(className) {
      const classList = await this.webElement.getAttribute('class');

      return pipe(split(' '), includes(className))(classList);
    },
    async containsText(text) {
      const elementText = await this.webElement.getText();

      return includes(text, elementText);

stampit

Create objects from reusable, composable behaviors.

MIT
Latest version published 3 years ago

Package Health Score

62 / 100
Full package analysis