Skip to content

Commit 930a078

Browse files
koresargitbook-bot
authored andcommitted
GitBook: [master] 41 pages and 2 assets modified
1 parent cb1b11d commit 930a078

30 files changed

+313
-279
lines changed
94.5 KB
Loading

.gitbook/assets/class_vs_stamp.png

94.5 KB
Loading

README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
# What is stampit
1+
# Introduction
22

3-
`stampit` is a JavaScript module which implements the [**stamp** specification](https://www.gitbook.com/book/stampit-org/docs/edit#). **Stamps** are composable factory functions.
3+
## What is stampit
4+
5+
`stampit` is a JavaScript module which implements the [**stamp** specification](essentials/specification/). **Stamps** are composable factory functions.
46

57
**Think of **_**stamps**_** as classic classes but without any limits, boundaries, or rules.**
68

79
The stamps brag large amount of features and things you can do with your objects and/or stamps.
810

9-
Head straight to the [Quick Start](/start.md) page for code examples.
11+
Head straight to the [Quick Start](api/quick-start.md) page for code examples.
1012

11-
![](/assets/class_vs_stamp.png)
13+
![](.gitbook/assets/class_vs_stamp%20%281%29.png)
1214

13-
# Differences from classes
15+
## Differences from classes
1416

1517
* Inheritance
1618
* Classes
1719
* Classes do child+parent+parent+parent... inheritance chains. See picture above.
1820
* When you extend a class you link class prototypes.
1921
* Stamps
2022
* Stamp are single object + single prototype. See picture above.
21-
* When you "extend" you separately merge [methods](/methods.md), separately merge [properties](/properties.md), separately merge [static properties](/static-properties.md), separately merge [initializers](/initializers.md) \(aka constructors\), etc etc etc.
22-
* That's why it is not just inheritance, but special type of object composition. The stamp composition algorithm is [standardized](/specification/merging-algorithm.md).
23-
* You can influence composition result with your code at runtime using the [composers](/composers.md) feature.
23+
* When you "extend" you separately merge [methods](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/methods.md), separately merge [properties](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/properties.md), separately merge [static properties](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/static-properties.md), separately merge [initializers](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/initializers.md) \(aka constructors\), etc etc etc.
24+
* That's why it is not just inheritance, but special type of object composition. The stamp composition algorithm is [standardized](essentials/specification/merging-algorithm.md).
25+
* You can influence composition result with your code at runtime using the [composers](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/composers.md) feature.
2426
* Object creation
2527
* Classes
2628
* In most programming languages you execute only one constructor per class.
@@ -30,19 +32,19 @@ Head straight to the [Quick Start](/start.md) page for code examples.
3032
* Stamp executes every initializer \(aka constructor\) it has.
3133
* All initializers receive exactly the same set of arguments, no need to manually pass data.
3234
* The initializer execution sequence is the same as the stamp composition sequence.
33-
* To create an object you [call stamp](/start.md) as a function: `const object = MyStamp()`.
35+
* To create an object you [call stamp](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/start.md) as a function: `const object = MyStamp()`.
3436

35-
# History
37+
## History
3638

37-
The original idea of Stamps belongs to [Eric Elliott](https://ericelliottjs.com/). See his [first commit](https://github.com/stampit-org/stampit/commit/ac330e8537e349a9640bbe4a34c63150db445a20) from 11 Feb 2013 in the stampit repository. Since then the idea evolved to the [specification](/specification.md) and a whole [ecosystem](/ecosystem.md) of mini modules.
39+
The original idea of Stamps belongs to [Eric Elliott](https://ericelliottjs.com/). See his [first commit](https://github.com/stampit-org/stampit/commit/ac330e8537e349a9640bbe4a34c63150db445a20) from 11 Feb 2013 in the stampit repository. Since then the idea evolved to the [specification](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/specification.md) and a whole [ecosystem](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/ecosystem.md) of mini modules.
3840

39-
# Using Stamps
41+
## Using Stamps
4042

41-
Head straight to the [Quick start](/start.md) for code examples.
43+
Head straight to the [Quick start](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/start.md) for code examples.
4244

43-
# Ecosystem
45+
## Ecosystem
4446

4547
Stampit have a lot of helper modules. You can always find the list of official NPM packages here: [https://www.npmjs.com/~stamp](https://www.npmjs.com/~stamp)
4648

47-
See more information on the [Ecosystem](/ecosystem.md) page.
49+
See more information on the [Ecosystem](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/ecosystem.md) page.
4850

SUMMARY.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# Summary
1+
# Table of contents
2+
3+
* [Introduction](README.md)
24

35
## Essentials
46

5-
* [Overview](README.md)
6-
* [What is a Stamp](descriptor.md)
7-
* [Installation](installation.md)
8-
* [Specification](specification/README.md)
9-
* [Merging algorithm](specification/merging-algorithm.md)
10-
* [Object creation internals](specification/object-creation-internals.md)
11-
* [FAQ](faq.md)
7+
* [Overview](essentials/overview.md)
8+
* [What is a Stamp](essentials/what-is-a-stamp.md)
9+
* [Installation](essentials/installation.md)
10+
* [Specification](essentials/specification/README.md)
11+
* [Merging algorithm](essentials/specification/merging-algorithm.md)
12+
* [Object creation internals](essentials/specification/object-creation-internals.md)
13+
* [FAQ](essentials/faq.md)
1214

1315
## API
1416

15-
* [Quick start](api/README.md)
17+
* [Quick start](api/quick-start.md)
1618
* [Basics](api/basics.md)
1719
* [Methods](api/methods.md)
1820
* [Properties](api/properties.md)
@@ -28,10 +30,10 @@
2830

2931
## Ecosystem
3032

31-
* [Ecosystem Overview](ecosystem/README.md)
32-
* [@stamp/collision](ecosystem/stampcollision.md)
33-
* [@stamp/required](ecosystem/stamprequired.md)
34-
* [@stamp/privatize](ecosystem/stampprivatize.md)
35-
* [@stamp/named](ecosystem/stampnamed.md)
36-
* [@stamp/instanceof](ecosystem/stampinstanceof.md)
33+
* [Ecosystem Overview](ecosystem/ecosystem-overview.md)
34+
* [@stamp/collision](ecosystem/stamp-collision.md)
35+
* [@stamp/required](ecosystem/stamp-required.md)
36+
* [@stamp/privatize](ecosystem/stamp-privatize.md)
37+
* [@stamp/named](ecosystem/stamp-named.md)
38+
* [@stamp/instanceof](ecosystem/stamp-instanceof.md)
3739

api/basics.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Stampit gives you several ways to create your objects.
1010
1111
### Pass plain descriptor
1212

13-
You can pass standard [stamp descriptor](/composition.md) to stampit.
13+
You can pass standard [stamp descriptor](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/composition.md) to stampit.
1414

15-
```js
15+
```javascript
1616
const descriptor = {
1717
methods: ...,
1818
properties: ...,
@@ -32,7 +32,7 @@ const MyStamp = stampit(descriptor) // passing the descriptor
3232

3333
Or you can pass _sampit-flavoured_ descriptor \(shorter version of the standard descriptor\).
3434

35-
```js
35+
```javascript
3636
const shorterDescriptor = {
3737
methods: ...,
3838
props: ...,
@@ -54,13 +54,13 @@ const MyStamp = stampit(shorterDescriptor)
5454

5555
Stampit has 18 shortcut functions attached to it. For example:
5656

57-
```js
57+
```javascript
5858
const { props, methods, init } = stampit
5959
```
6060

6161
You can create stamps from them too.
6262

63-
```js
63+
```javascript
6464
const DefaultFoo = props({ foo: null })
6565
const PrintFoo = methods({ printFoo() { console.log(this.foo) } })
6666
const PassFoo = init({ foo }) { this.foo = foo })
@@ -70,7 +70,7 @@ const Foo = DefaultFoo.compose(PrintFoo, PassFoo)
7070

7171
The full list of the shortcut functions matches the list of keys you can pass as stamp descriptor \(see above\).
7272

73-
```js
73+
```javascript
7474
const { // destructuring
7575
methods,
7676

@@ -102,7 +102,7 @@ All the examples below create _exactly the same stamp_.
102102

103103
#### Classic way
104104

105-
```js
105+
```javascript
106106
const logger = require('bunyan').createLogger({ name: 'log' })
107107

108108
const HasLog = stampit({
@@ -114,7 +114,7 @@ const HasLog = stampit({
114114

115115
#### Classic way using shorter API
116116

117-
```js
117+
```javascript
118118
const HasLog = stampit({
119119
props: {
120120
log: logger
@@ -124,7 +124,7 @@ const HasLog = stampit({
124124

125125
#### Shortcut way
126126

127-
```js
127+
```javascript
128128
const HasLog = stampit.props({
129129
log: logger
130130
})
@@ -140,7 +140,7 @@ const HasLog = props({
140140

141141
#### Composition way
142142

143-
```js
143+
```javascript
144144
const HasLog = stampit().props({
145145
log: logger
146146
})
@@ -150,7 +150,7 @@ const HasLog = stampit().props({
150150

151151
You can chain all the shortcut functions \(see the full list above\).
152152

153-
```js
153+
```javascript
154154
const InstanceCounter = stampit()
155155
.conf({
156156
instanceCounter: 0 // number of instances of a particular stamp

api/composers.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Composers are callback functions executed each time a composition happens. There
66
77
It is highly recommended to **not create new stamps** in the composers, but **mutate the given stamp** if necessary.
88

9-
```js
9+
```javascript
1010
const InstanceOf = stampit.composers(({ stamp, composables }) => {
1111
if (!stamp.compose.methods) stamp.compose.methods = {} // mutating stamp
1212
stamp.compose.methods.getStamp = () => stamp // mutating stamp
@@ -33,13 +33,13 @@ The above `InstanceOf` stamp overrides the default `instanceof` behavior using E
3333

3434
> NOTE
3535
>
36-
> You can find the stamp above as the [`@stamp/instanceof`](/stampinstanceof.md) NPM module.
36+
> You can find the stamp above as the [`@stamp/instanceof`](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/stampinstanceof.md) NPM module.
3737
3838
## Descriptor merging algorithm
3939

4040
The composers are concatenated into a deduplicated array. As the result, the order of composition becomes **the order of composer execution**.
4141

42-
```js
42+
```javascript
4343
const {composers} = stampit
4444

4545
const Log1 = composers(() => console.log(1))
@@ -59,7 +59,7 @@ MultiLog.compose.composers.length === 3
5959

6060
Stamps remove duplicate composers.
6161

62-
```js
62+
```javascript
6363
const {composers} = stampit
6464

6565
const func = () => console.log(1)
@@ -87,7 +87,7 @@ Every composer always receive this object: `{ stamp, composables }`. Where:
8787

8888
Exactly the same stamp can be created in few ways. Here they all are.
8989

90-
```js
90+
```javascript
9191
function myDebugComposer ({ stamp, composables }) {
9292
console.log(`Stamp ${stamp} was composed of ${composables}`)
9393
}
@@ -107,5 +107,3 @@ const Logger = stampit().composers(myDebugComposer)
107107
const Logger = stampit().composers([myDebugComposer])
108108
```
109109

110-
111-

api/configuration.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Configuration is a storage of an arbitrary data in the `.configuration` metadata of stamp's descriptor.
44

5-
```js
5+
```javascript
66
let HaveApiKey = stampit({
77
conf: {
88
apiKey: process.env.API_KEY
@@ -12,9 +12,9 @@ let HaveApiKey = stampit({
1212
HaveApiKey.compose.configuration.apiKey === process.env.API_KEY
1313
```
1414

15-
You can set configuration in [static methods](/static-properties.md):
15+
You can set configuration in [static methods](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/static-properties.md):
1616

17-
```js
17+
```javascript
1818
HaveApiKey = HaveApiKey.statics({
1919
setApiKey(key) {
2020
return this.conf({ apiKey: key }) // create new stamp by composing parent stamp with some configuration
@@ -26,9 +26,9 @@ HaveApiKey = HaveApiKey.setApiKey('abcd1234')
2626
HaveApiKey.compose.configuration.apiKey === 'abcd1234'
2727
```
2828

29-
You can access the configuration in stamp [initializers](/initializers.md) \(aka constructors\).
29+
You can access the configuration in stamp [initializers](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/initializers.md) \(aka constructors\).
3030

31-
```js
31+
```javascript
3232
const ApiKeyPrinter = HaveApiKey.init(function (opts, { stamp }) {
3333
const configuration = stamp.compose.configuration
3434

@@ -42,7 +42,7 @@ HaveApiKey() // this will execute the initializer, and will print "abcd1234" to
4242

4343
The configuration properties are copied **by assignment**. In other words - **by reference **using `Object.assign`.
4444

45-
```js
45+
```javascript
4646
HaveApiKey.compose.configuration.apiKey === ApiKeyPrinter.compose.configuration.apiKey
4747
```
4848

@@ -52,7 +52,7 @@ In case of conflicts the last composed property wins.
5252

5353
Exactly the same stamp can be created in few ways. Here they all are.
5454

55-
```js
55+
```javascript
5656
const HasLog = stampit({
5757
conf: {
5858
apiKey: process.env.API_KEY
@@ -82,5 +82,3 @@ const HasLog = stampit().configuration({
8282
})
8383
```
8484

85-
86-

api/deep-configuration.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Deep Configuration
1+
# Deep configuration
22

3-
Deep Configuration is a storage of an arbitrary data in the `.deepConfiguration` metadata of stamp's descriptor. But unlike regular \(shallow\) configuration these are deeply merged together by stamp [deep merging algorithm](/specification/merging-algorithm.md).
3+
Deep Configuration is a storage of an arbitrary data in the `.deepConfiguration` metadata of stamp's descriptor. But unlike regular \(shallow\) configuration these are deeply merged together by stamp [deep merging algorithm](../essentials/specification/merging-algorithm.md).
44

5-
```js
5+
```javascript
66
let Kue = stampit.deepConf({
77
Kue: {
88
name: 'kue',
@@ -26,9 +26,9 @@ MyKue.compose.deepConfiguration.Kue.attempts === 3
2626
MyKue.compose.deepConfiguration.Kue.priority === 'high'
2727
```
2828

29-
You can set deep configuration in [static methods](/static-properties.md):
29+
You can set deep configuration in [static methods](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/static-properties.md):
3030

31-
```js
31+
```javascript
3232
Kue = Kue.statics({
3333
configureKue(options) {
3434
return this.deepConf({Kue: options}) // create new stamp by composing parent stamp with some configuration
@@ -45,9 +45,9 @@ Kue = Kue.statics({
4545
const MyRegularJobKue = Kue.attempts(5).delay(1000).ttl(10).events(true).priority('low')
4646
```
4747
48-
You can access the configuration in stamp [initializers](/initializers.md) \(aka constructors\).
48+
You can access the configuration in stamp [initializers](https://github.com/stampit-org/docs/tree/cb1b11dcef3e3b0b3aa5212adcf9047a2f882b06/initializers.md) \(aka constructors\).
4949
50-
```js
50+
```javascript
5151
Kue = Kue.init(function (payload, { stamp }) {
5252
const conf = stamp.compose.deepConfiguration.Kue
5353
this.conf = conf
@@ -65,17 +65,17 @@ myKue.put({ title: 'Vasyl Boroviak', email: '[email protected]', template: 'welc
6565
6666
## Descriptor merging algorithm
6767
68-
The deepConfiguration is **deeply merged** using stamp [deep merging algorithm](/specification/merging-algorithm.md). See below - the `Kue` value is always a new object.
68+
The deepConfiguration is **deeply merged** using stamp [deep merging algorithm](../essentials/specification/merging-algorithm.md). See below - the `Kue` value is always a new object.
6969
70-
```js
70+
```javascript
7171
Kue.compose.deepConfiguration.Kue !== MyKue.compose.deepConfiguration.Kue // NEVER EQUAL! NO MATTER WHAT!
7272
```
7373
7474
## Other ways to add deep configuration
7575
7676
Exactly the same stamp can be created in few ways. Here they all are.
7777
78-
```js
78+
```javascript
7979
const myKueConf = { Kue: { priority: 'normal' } }
8080

8181
const AuthServiceToken = stampit({
@@ -107,5 +107,3 @@ const HasLog = stampit().deepConfiguration({
107107
})
108108
```
109109
110-
111-

0 commit comments

Comments
 (0)