Fun with Stamps. Episode 24. New “name” feature
Hello. I’m developer Vasyl Boroviak and welcome to the twenty fourth episode of Vasyl Boroviak presents Fun with Stamps.
TL;DR: Now you can easily set MyStamp.name
property like this:
const MyGateway = stampit({
name: "SmsGateway"
})MyGateway.name === "SmsGateway" // this used be hard to set
Available now in stampit@4.2.0
and @stamp/it@1.1.0
. See docs at https://stampit.js.org
I find myself setting name of my stamps very often. Usually I use this package: https://www.npmjs.com/package/@stamp/named
import stampit from "@stamp/it" // or "stampit"
import {setName} from "@stamp/named" // we will get rid of this line
import bunyan from "bunyan"const HasLog = stampit({
init(_, {stamp}) {
// this will set logger name to "SmsGateway"
this.log = bunyan.createLogger({name: stamp.name})
}
})const SmsGateway = stampit(HasLog, setName("SmsGateway"), {
props: ...
methods: ...
})
The code above adds
HasLog
behaviour which simply initialises a bunyan logger instance for each gateway instance. However, bunyan requires you to give it aname
. So, I’m taking the logger name from the stamp’s name. In this case “SmsGateway”. I find this trick neat and concise. (Take that classic classes! You can’t do anything like that!)
Starting today no extra @stamp/named
package needed! Just pass name: "StampName"
to stampit.
import stampit from "@stamp/it" // or "stampit"
import bunyan from "bunyan"const HasLog = stampit({
init(_, {stamp}) {
// this will set logger name to "SmsGateway"
this.log = bunyan.createLogger({name: stamp.name});
}
})const SmsGateway = stampit(HasLog, {
name: "SmsGateway", props: ...
methods: ...
})
Gotchas
- Won’t work in ES5 environments (like IE11). The name will always be “Stamp”. Name of a function can be set only in ≥ES6 environments.
- This code doesn’t work in JavaScript in general:
SmsGateway.name = “bla”
. BecauseFunction.name
is a special protected property. - Composing with
SmsGateway
will inherit its name:
const TwillioGateway = SmsGateway.compose( ... )TwillioGateway.name === "SmsGateway" // Oops!
or
const TwillioGateway = stampit({
props: ...
method: ...
...
}, SmsGateway // composing with a stamp which has a non-default name
)TwillioGateway.name === "SmsGateway" // Oops!
You just need to overwrite it:
const TwillioGateway = SmsGateway.compose({
name: "TwillioGateway", ...
})
Have fun with stamps!
The rest of the episodes:
- Episode 1. Stamp basics
- Episode 2. Dependency injection in FP
- Episode 3. Comparing with the ES2015 classes
- Episode 4. Implementing stamps yourself in 30 LOC
- Episode 5. Composition design pattern
- Episode 6. Statics — properties on stamps
- Episode 7. Early and late dependency injection
- Episode 8. Tracking and overriding composition
- Episode 9. Detaching compose()
- Episode 10. My stamp mental model
- Episode 11. Interfering composition
- Episode 12. New @stamp home
- Episode 13. Method collision control
- Episode 14. New @stamp/it as a replacement of Stampit
- Episode 15. The @stamp/ modules ecosystem
- Episode 16. TypeScript mix-in classes vs Stamps
- Episode 17. Easy 100% unit test coverage in JS
- Episode 18. Dependency injection paradise
- Episode 19. Java/C# abstract methods in JavaScript
- Episode 20. Stampit v4
- Episode 21. Private data in JavaScript. 4 ways using stamps
- Episode 22. JavaScript instanceof as composable stamp
- Episode 23. New stampit.js.org with all the docs
- Episode 24. New “name” feature (this article)