MCPcopy
hub / github.com/stampit-org/stampit

github.com/stampit-org/stampit @v5.1.0 sqlite

repository ↗ · DeepWiki ↗ · release v5.1.0 ↗
155 symbols 297 edges 20 files 41 documented · 26%
README

stampit

Stampit npm

Create objects from reusable, composable behaviors

Stampit is a 1.3KB gzipped (or 4.8K minified) JavaScript module which supports three different kinds of prototypal inheritance (delegation, concatenation, and functional) to let you inherit behavior in a way that is much more powerful and flexible than any other Object Oriented Programming model.

Stamps are standardised composable factory functions. Stampit is a handy implementation of the specification featuring friendly API.

Find many more examples in this series of mini blog posts or on the official website.

Example

import stampit from "stampit";

const Character = stampit({
  props: {
    name: null,
    health: 100,
  },
  init({ name = this.name }) {
    this.name = name;
  },
});

const Fighter = Character.compose({
  // inheriting
  props: {
    stamina: 100,
  },
  init({ stamina = this.stamina }) {
    this.stamina = stamina;
  },
  methods: {
    fight() {
      console.log(`${this.name} takes a mighty swing!`);
      this.stamina--;
    },
  },
});

const Mage = Character.compose({
  // inheriting
  props: {
    mana: 100,
  },
  init({ mana = this.mana }) {
    this.mana = mana;
  },
  methods: {
    cast() {
      console.log(`${this.name} casts a fireball!`);
      this.mana--;
    },
  },
});

const Paladin = stampit(Mage, Fighter); // as simple as that!

const fighter = Fighter({ name: "Thumper" });
fighter.fight();
const mage = Mage({ name: "Zapper" });
mage.cast();
const paladin = Paladin({ name: "Roland", stamina: 50, mana: 50 });
paladin.fight();
paladin.cast();

console.log(Paladin.compose.properties); // { name: null, health: 100, stamina: 100, mana: 100 }
console.log(Paladin.compose.methods); // { fight: [Function: fight], cast: [Function: cast] }

Status

Install

NPM

API

See https://stampit.js.org

What's the Point?

Prototypal OO is great, and JavaScript's capabilities give us some really powerful tools to explore it, but it could be easier to use.

Basic questions like "how do I inherit privileged methods and private data?" and "what are some good alternatives to inheritance hierarchies?" are stumpers for many JavaScript users.

Let's answer both of these questions at the same time.

// Some privileged methods with some private data.
const Availability = stampit({
  init() {
    let isOpen = false; // private

    this.open = function open() {
      isOpen = true;
      return this;
    };
    this.close = function close() {
      isOpen = false;
      return this;
    };
    this.isOpen = function isOpenMethod() {
      return isOpen;
    };
  },
});

// Here's a stamp with public methods, and some state:
const Membership = stampit({
  props: {
    members: {},
  },
  methods: {
    add(member) {
      this.members[member.name] = member;
      return this;
    },
    getMember(name) {
      return this.members[name];
    },
  },
});

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

// Classical inheritance has nothing on this.
// No parent/child coupling. No deep inheritance hierarchies.
// Just good, clean code reusability.
const Bar = stampit(Defaults, Availability, Membership);

// Create an object instance
const myBar = Bar({ name: "Moe's" });

// Silly, but proves that everything is as it should be.
myBar.add({ name: "Homer" }).open().getMember("Homer");

For more examples see the API or the Fun With Stamps mini-blog series.

Development

Unit tests

npm t

Benchmark tests

npm run test:benchmark

Extension points exported contracts — how you extend this code

FactoryFunction (Interface)
A factory function: call it (optionally with options) to get an instance.
stampit.ts
ServiceDescriptor (Interface)
* The shape of a service descriptor: just an `ExtendedDescriptor` plus a * `dependencies` factory. It stays a plain p
test/types.test-d.ts
InitializerContext (Interface)
The context object passed as the 2nd argument to every initializer.
stampit.ts
Logger (Interface)
(no doc)
test/types.test-d.ts
Initializer (Interface)
* A function used as an `.init()` / `.initializers()` argument. * @template I The instance type (`this` and the produce
stampit.ts
DescriptorInitializer (Interface)
* An initializer as it is written *inside a descriptor literal* * (`stampit({ init(options) { … } })`). * * Unlike {@
stampit.ts
ComposerParameters (Interface)
The parameters received by a `.composers()` function.
stampit.ts

Core symbols most depended-on inside this repo

stampit
called by 120
stampit.ts
expectType
called by 49
test/types.test-d.ts
init
called by 23
stampit.ts
props
called by 22
stampit.ts
create
called by 18
stampit.ts
methods
called by 17
stampit.ts
statics
called by 13
stampit.ts
isObject
called by 11
stampit.ts

Shape

Function 89
Method 45
Interface 15
Class 6

Languages

TypeScript100%

Modules by API surface

stampit.ts73 symbols
test/types.test-d.ts31 symbols
test/benchmark/object-create.test.js26 symbols
test/basics-methods.test.js5 symbols
test/stampit-api.test.js3 symbols
test/basics-static.test.js3 symbols
test/infected-statics.test.js2 symbols
test/immutability.test.js2 symbols
test/compose.test.js2 symbols
test/benchmark/property-access.test.js2 symbols
test/basics-props.test.js2 symbols
test/init.test.js1 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

benchmark2.1.4 · 1×
c810.1.3 · 1×
check-compose5.1.0 · 1×
gzip-size-cli5.1.0 · 1×
oxlint1.19.0 · 1×
prettier3.8.3 · 1×
typescript6.0.3 · 1×
uglify-js3.13.3 · 1×

For agents

$ claude mcp add stampit \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact