Fun with Stamps. Episode 12. New @stamp home

Vasyl Boroviak
3 min readJan 30, 2017

Hello. I’m developer Vasyl Boroviak and welcome to the twelfth episode of Vasyl Boroviak presents Fun with Stamps.

Some history

Stamps were born in 2013 in Eric Elliott’s head. First released in github.com/dilvie/stampit repo. Then moved to github.com/ericelliott/stampit. Then eventually we created the GitHub organization account to host all stamp related work there — https://github.com/stampit-org. Stampit was moved to github.com/stampit-org/stampit.

Some future plans

To showcase all the powers of stamps and also to make stamps easier to use, it was decided to create few commonly used stamps various people ask from time to time. Like “How to make my method private?”, or “How to avoid method conflicts?”, or “How to check which stamp my object was created with?”, etc.

New place for stamps

There is new NPM scoped package namespace — @stamp. https://www.npmjs.com/~stamp

Every part of stamp ecosystem is gonna live there. The repository for that new ecosystem is https://github.com/stampit-org/stamp

It’s a monorepo controlled with the Lerna tool.

Let’s list what we have to date.

The compose() implementation

The standard implementation of the compose() function is here:

@stamp/compose

URL: https://github.com/stampit-org/stamp/tree/master/packages/compose

The “is” utility functions

For example isStamp, isComposable, isDescriptor.

@stamp/is

URL: https://github.com/stampit-org/stamp/tree/master/packages/is

The check-compose executable module

The module to check if your compose() implementation comply to the latest specification:

@stamp/check-compose

URL: https://github.com/stampit-org/stamp/tree/master/packages/check-compose

The stamps “core”

The only “core” thing stamps have is the standardized deep merging algorithm.

@stamp/core

URL: https://github.com/stampit-org/stamp/tree/master/packages/core

The commonly asked stamps

As you know, pretty much any behavior of your object instances and/or stamps can be implemented as a … drum roll … stamp! The new mono repo will implement a bunch of behaviors (aka stamps) many of you asked throughout the years.

Here is what we have so far.

@stamp/privatize

URL: https://github.com/stampit-org/stamp/tree/master/packages/privatize

This behavior will hide all properties from your object. Optionally it can hide selected methods.

import Privatize from '@stamp/privatize';
import compose from '@stamp/compose';
const Password = compose({
properties: {
password: '12345qwert'
},
methods: {
getPassword() {
return this.password;
},
setPassword(pwd) {
this.password = pwd;
}
}
})
.compose(Privatize); // Privatizing all the properties by default
const obj = Password();console.log(obj.password); // undefined
console.log(obj.getPassword()); // 12345qwert
obj.setPassword('1234567890');
console.log(obj.getPassword()); // 1234567890

As you can see all the properties are hidden by default. But methods are still public. Let’s hide the setPassword() method too.

const ReadonlyPassword = Password.privatizeMethods('setPassword');const obj = ReadonlyPassword();console.log(obj.password); // undefined
console.log(obj.getPassword()); // 12345qwert
obj.setPassword(); // Error: no method setPassword of undefined

@stamp/collision

URL: https://github.com/stampit-org/stamp/tree/master/packages/collision

You can forbid method override (aka override, aka collision). Also, you can collect collided methods to a single method.

See Fun With Stamps. Episode 13.

Your stamps in the @stamp namespace

Often, people come to our public Gitter chat room and ask how to do this or that… Typically, the solutions can be implemented as a behavior (aka stamp).

If you want your objects or stamps to behave in a certain way — feel free to open a GitHub issue or PR, or quickly ask in the Gitter chat.

Have fun with stamps!

--

--