Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
The stampit package is a powerful library for creating composable, reusable, and flexible object factories in JavaScript. It leverages the concept of stamps, which are composable factory functions that produce objects. This approach allows for a more modular and maintainable codebase, especially in large applications.
Creating Stamps
This feature allows you to create a stamp with default properties. The resulting stamp can be used to create new objects with those properties.
const stampit = require('stampit');
const MyStamp = stampit().props({
name: 'defaultName',
age: 0
});
const instance = MyStamp();
console.log(instance); // { name: 'defaultName', age: 0 }
Composing Stamps
This feature allows you to compose multiple stamps into a single stamp. The resulting stamp inherits properties from all composed stamps.
const stampit = require('stampit');
const StampA = stampit().props({
name: 'StampA'
});
const StampB = stampit().props({
age: 30
});
const ComposedStamp = stampit.compose(StampA, StampB);
const instance = ComposedStamp();
console.log(instance); // { name: 'StampA', age: 30 }
Adding Methods
This feature allows you to add methods to the stamp. The methods will be available on all objects created by the stamp.
const stampit = require('stampit');
const MyStamp = stampit().methods({
sayHello() {
console.log(`Hello, my name is ${this.name}`);
}
}).props({
name: 'John'
});
const instance = MyStamp();
instance.sayHello(); // Hello, my name is John
Initializers
This feature allows you to add initializers to the stamp. Initializers are functions that run when a new object is created by the stamp.
const stampit = require('stampit');
const MyStamp = stampit().init(function() {
this.initialized = true;
});
const instance = MyStamp();
console.log(instance.initialized); // true
Lodash is a utility library that provides a wide range of functions for common programming tasks. While it is not specifically focused on object composition like stampit, it offers some similar functionalities such as object merging and cloning.
Ramda is a functional programming library for JavaScript. It provides utilities for working with objects and functions in a functional style. Ramda's approach to immutability and function composition can be seen as an alternative to stampit's object composition.
Mixwith is a library for creating mixins in JavaScript. It allows you to compose classes from multiple mixins, similar to how stampit composes objects from multiple stamps. Mixwith is more focused on ES6 class-based composition.
Create objects from reusable, composable behaviors
Stampit is a 1.4KB gzipped (or 3K 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.
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] }
npm i stampit@1
npm i stampit@2
Breaking changesnpm i stampit@3
Breaking changes. Compatible with the stamp specification <= 1.4npm i stampit
Breaking changes. Compatible with the stamp specification v1.5npm i @stamp/it
The new ecosystem of useful stamps like collision control, etc.Stampit should run fine in any ES5 browser or any node.js.
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.
npm t
env CI=1 npm t
To run unit tests in a default browser:
npm run browsertest
To run tests in a different browser:
./test/index.html
in your browser, andnpx cut-release
It will run the cut-release
utility which would ask you if you're publishing patch, minor, or major version. Then it will execute npm version
, git push
and npm publish
with proper arguments for you.
FAQs
Create objects from reusable, composable behaviors.
The npm package stampit receives a total of 224,462 weekly downloads. As such, stampit popularity was classified as popular.
We found that stampit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.