1 | <p align="center">
|
---|
2 | <img src="https://raw.githubusercontent.com/stampit-org/stampit-logo/master/stampit-logo.png" alt="stampit" width="160" />
|
---|
3 | </p>
|
---|
4 |
|
---|
5 | # Stampit [](https://travis-ci.org/stampit-org/stampit) [](https://www.npmjs.com/package/stampit) [](https://gitter.im/stampit-org/stampit?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://twitter.com/stampit_org) [](https://unpkg.com/stampit@latest/dist/stampit.min.js)
|
---|
6 |
|
---|
7 | **Create objects from reusable, composable behaviors**
|
---|
8 |
|
---|
9 | 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.
|
---|
10 |
|
---|
11 | Stamps are [standardised](https://github.com/stampit-org/stamp-specification) composable factory functions. **Stampit** is a handy implementation of the specification featuring friendly API.
|
---|
12 |
|
---|
13 | Find many more examples in [this series of mini blog posts](https://medium.com/@koresar/fun-with-stamps-episode-1-stamp-basics-e0627d81efe0) or on the [official website](https://stampit.js.org/api/quick-start).
|
---|
14 |
|
---|
15 | ## Example
|
---|
16 |
|
---|
17 | ```js
|
---|
18 | import stampit from 'stampit'
|
---|
19 |
|
---|
20 | const Character = stampit({
|
---|
21 | props: {
|
---|
22 | name: null,
|
---|
23 | health: 100
|
---|
24 | },
|
---|
25 | init({ name = this.name }) {
|
---|
26 | this.name = name
|
---|
27 | }
|
---|
28 | })
|
---|
29 |
|
---|
30 | const Fighter = Character.compose({ // inheriting
|
---|
31 | props: {
|
---|
32 | stamina: 100
|
---|
33 | },
|
---|
34 | init({ stamina = this.stamina }) {
|
---|
35 | this.stamina = stamina;
|
---|
36 | },
|
---|
37 | methods: {
|
---|
38 | fight() {
|
---|
39 | console.log(`${this.name} takes a mighty swing!`)
|
---|
40 | this.stamina--
|
---|
41 | }
|
---|
42 | }
|
---|
43 | })
|
---|
44 |
|
---|
45 | const Mage = Character.compose({ // inheriting
|
---|
46 | props: {
|
---|
47 | mana: 100
|
---|
48 | },
|
---|
49 | init({ mana = this.mana }) {
|
---|
50 | this.mana = mana;
|
---|
51 | },
|
---|
52 | methods: {
|
---|
53 | cast() {
|
---|
54 | console.log(`${this.name} casts a fireball!`)
|
---|
55 | this.mana--
|
---|
56 | }
|
---|
57 | }
|
---|
58 | })
|
---|
59 |
|
---|
60 | const Paladin = stampit(Mage, Fighter) // as simple as that!
|
---|
61 |
|
---|
62 | const fighter = Fighter({ name: 'Thumper' })
|
---|
63 | fighter.fight()
|
---|
64 | const mage = Mage({ name: 'Zapper' })
|
---|
65 | mage.cast()
|
---|
66 | const paladin = Paladin({ name: 'Roland', stamina: 50, mana: 50 })
|
---|
67 | paladin.fight()
|
---|
68 | paladin.cast()
|
---|
69 |
|
---|
70 | console.log(Paladin.compose.properties) // { name: null, health: 100, stamina: 100, mana: 100 }
|
---|
71 | console.log(Paladin.compose.methods) // { fight: [Function: fight], cast: [Function: cast] }
|
---|
72 | ```
|
---|
73 |
|
---|
74 |
|
---|
75 | ## Status
|
---|
76 |
|
---|
77 | * **v1**. `npm i stampit@1`
|
---|
78 | * **v2**. `npm i stampit@2` [Breaking changes](https://github.com/stampit-org/stampit/releases/tag/2.0)
|
---|
79 | * **v3**. `npm i stampit@3` [Breaking changes](https://github.com/stampit-org/stampit/releases/tag/v3.0.0). Compatible with the [stamp specification](https://github.com/stampit-org/stamp-specification) <= 1.4
|
---|
80 | * **v4**. `npm i stampit` [Breaking changes](https://github.com/stampit-org/stampit/releases/tag/v4.0.0). Compatible with the [stamp specification](https://github.com/stampit-org/stamp-specification) v1.5
|
---|
81 | * **next**. `npm i @stamp/it` The [new ecosystem](https://www.npmjs.com/~stamp/) of useful stamps like collision control, etc.
|
---|
82 |
|
---|
83 |
|
---|
84 | ## Install
|
---|
85 |
|
---|
86 | [](https://www.npmjs.com/package/stampit)
|
---|
87 |
|
---|
88 | ## Compatibility
|
---|
89 |
|
---|
90 | Stampit should run fine in any ES5 browser or any node.js.
|
---|
91 |
|
---|
92 | ## API
|
---|
93 |
|
---|
94 | See https://stampit.js.org
|
---|
95 |
|
---|
96 |
|
---|
97 | # What's the Point?
|
---|
98 |
|
---|
99 | Prototypal OO is great, and JavaScript's capabilities give us some really powerful tools to explore it, but it could be easier to use.
|
---|
100 |
|
---|
101 | Basic questions like "how do I inherit privileged methods and private data?" and
|
---|
102 | "what are some good alternatives to inheritance hierarchies?" are stumpers for many JavaScript users.
|
---|
103 |
|
---|
104 | Let's answer both of these questions at the same time.
|
---|
105 |
|
---|
106 | ```js
|
---|
107 | // Some privileged methods with some private data.
|
---|
108 | const Availability = stampit({
|
---|
109 | init() {
|
---|
110 | let isOpen = false; // private
|
---|
111 |
|
---|
112 | this.open = function open() {
|
---|
113 | isOpen = true;
|
---|
114 | return this;
|
---|
115 | };
|
---|
116 | this.close = function close() {
|
---|
117 | isOpen = false;
|
---|
118 | return this;
|
---|
119 | };
|
---|
120 | this.isOpen = function isOpenMethod() {
|
---|
121 | return isOpen;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | });
|
---|
125 |
|
---|
126 | // Here's a stamp with public methods, and some state:
|
---|
127 | const Membership = stampit({
|
---|
128 | props: {
|
---|
129 | members: {}
|
---|
130 | },
|
---|
131 | methods: {
|
---|
132 | add(member) {
|
---|
133 | this.members[member.name] = member;
|
---|
134 | return this;
|
---|
135 | },
|
---|
136 | getMember(name) {
|
---|
137 | return this.members[name];
|
---|
138 | }
|
---|
139 | }
|
---|
140 | });
|
---|
141 |
|
---|
142 | // Let's set some defaults:
|
---|
143 | const Defaults = stampit({
|
---|
144 | props: {
|
---|
145 | name: "The Saloon",
|
---|
146 | specials: "Whisky, Gin, Tequila"
|
---|
147 | },
|
---|
148 | init({ name, specials }) {
|
---|
149 | this.name = name || this.name;
|
---|
150 | this.specials = specials || this.specials;
|
---|
151 | }
|
---|
152 | });
|
---|
153 |
|
---|
154 | // Classical inheritance has nothing on this.
|
---|
155 | // No parent/child coupling. No deep inheritance hierarchies.
|
---|
156 | // Just good, clean code reusability.
|
---|
157 | const Bar = stampit(Defaults, Availability, Membership);
|
---|
158 |
|
---|
159 | // Create an object instance
|
---|
160 | const myBar = Bar({ name: "Moe's" });
|
---|
161 |
|
---|
162 | // Silly, but proves that everything is as it should be.
|
---|
163 | myBar.add({ name: "Homer" }).open().getMember("Homer");
|
---|
164 | ```
|
---|
165 |
|
---|
166 | For more examples see the [API](https://stampit.js.org) or the [Fun With Stamps](https://medium.com/@koresar/fun-with-stamps-episode-1-stamp-basics-e0627d81efe0) mini-blog series.
|
---|
167 |
|
---|
168 | # Development
|
---|
169 |
|
---|
170 | ### Unit tests
|
---|
171 | ```
|
---|
172 | npm t
|
---|
173 | ```
|
---|
174 |
|
---|
175 | ### Unit and benchmark tests
|
---|
176 | ```
|
---|
177 | env CI=1 npm t
|
---|
178 | ```
|
---|
179 |
|
---|
180 | ### Unit tests in a browser
|
---|
181 | To run unit tests in a default browser:
|
---|
182 | ```
|
---|
183 | npm run browsertest
|
---|
184 | ```
|
---|
185 | To run tests in a different browser:
|
---|
186 | * Open the `./test/index.html` in your browser, and
|
---|
187 | * open developer's console. The logs should indicate success.
|
---|
188 |
|
---|
189 | ### Publishing to NPM registry
|
---|
190 | ```bash
|
---|
191 | npx cut-release
|
---|
192 | ```
|
---|
193 | 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.
|
---|