source: node_modules/stampit/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 6.0 KB
Line 
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 [![Build Status](https://travis-ci.org/stampit-org/stampit.svg?branch=master)](https://travis-ci.org/stampit-org/stampit) [![npm](https://img.shields.io/npm/dm/stampit.svg)](https://www.npmjs.com/package/stampit) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/stampit-org/stampit?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Twitter Follow](https://img.shields.io/twitter/follow/stampit.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/stampit_org) [![UNPKG](https://img.shields.io/badge/gzip%20size-1.4%20kB-brightgreen.svg)](https://unpkg.com/stampit@latest/dist/stampit.min.js)
6
7**Create objects from reusable, composable behaviors**
8
9Stampit 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
13Find 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
18import stampit from 'stampit'
19
20const Character = stampit({
21 props: {
22 name: null,
23 health: 100
24 },
25 init({ name = this.name }) {
26 this.name = name
27 }
28})
29
30const 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
45const 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
60const Paladin = stampit(Mage, Fighter) // as simple as that!
61
62const fighter = Fighter({ name: 'Thumper' })
63fighter.fight()
64const mage = Mage({ name: 'Zapper' })
65mage.cast()
66const paladin = Paladin({ name: 'Roland', stamina: 50, mana: 50 })
67paladin.fight()
68paladin.cast()
69
70console.log(Paladin.compose.properties) // { name: null, health: 100, stamina: 100, mana: 100 }
71console.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[![NPM](https://nodei.co/npm/stampit.png?compact=true)](https://www.npmjs.com/package/stampit)
87
88## Compatibility
89
90Stampit should run fine in any ES5 browser or any node.js.
91
92## API
93
94See https://stampit.js.org
95
96
97# What's the Point?
98
99Prototypal OO is great, and JavaScript's capabilities give us some really powerful tools to explore it, but it could be easier to use.
100
101Basic 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
104Let's answer both of these questions at the same time.
105
106```js
107// Some privileged methods with some private data.
108const 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:
127const 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:
143const 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.
157const Bar = stampit(Defaults, Availability, Membership);
158
159// Create an object instance
160const myBar = Bar({ name: "Moe's" });
161
162// Silly, but proves that everything is as it should be.
163myBar.add({ name: "Homer" }).open().getMember("Homer");
164```
165
166For 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```
172npm t
173```
174
175### Unit and benchmark tests
176```
177env CI=1 npm t
178```
179
180### Unit tests in a browser
181To run unit tests in a default browser:
182```
183npm run browsertest
184```
185To 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
191npx cut-release
192```
193It 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.
Note: See TracBrowser for help on using the repository browser.