{"name":"stamp-specification","version":"1.0.4","description":"The Composables specification and example implementation","main":"build/compose.js","scripts":{"build":"babel -s -d build compose.js","lint":"eslint compose.js","test":"check-compose .","prepublish":"npm run lint && npm run build && npm test"},"repository":{"type":"git","url":"git+ssh://git@github.com/stampit-org/stamp-specification.git"},"keywords":["stamp","composable","object","oop","factory"],"author":"","license":"MIT","bugs":{"url":"https://github.com/stampit-org/stamp-specification/issues"},"homepage":"https://github.com/stampit-org/stamp-specification#readme","devDependencies":{"babel-cli":"^6.6.5","babel-eslint":"^5.0.0","babel-polyfill":"^6.7.2","babel-preset-es2015":"^6.6.0","babel-register":"^6.7.2","check-compose":"^1.0.1","eslint":"~2.2.0"},"dependencies":{"lodash":"^4.5.1"},"babel":{"presets":["es2015"]},"gitHead":"f18e52889de3f21fbe810643351f58ce7bd9d5b7","_id":"stamp-specification@1.0.4","_shasum":"84cd18c5d86233465f982dba0ba6049290e201b8","_from":".","_npmVersion":"2.14.15","_nodeVersion":"0.10.40","_npmUser":{"name":"koresar","email":"kore.sar@gmail.com"},"dist":{"shasum":"84cd18c5d86233465f982dba0ba6049290e201b8","tarball":"http://registry-npm.ywwl.com/stamp-specification/download/stamp-specification-1.0.4.tgz","integrity":"sha512-Ejs/X2BEEOFr61f+Zi+zea36ho2ubqxiUiOos5xAoRFX1dWX/jyzR30MZumlelG5+TdSNqcbFTOBmSmj8yThtA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDbrJ/LNaQUHMvOclWg9ZpZRpnK4KJC99Dz7EWwd6KPkgIgcMhtk+KKqlF8tHHXmCQEcPMm+nPKBPU+f63GXaOZY6I="}],"size":9708,"noattachment":false,"key":"/stamp-specification/-/stamp-specification-1.0.4.tgz"},"maintainers":[{"name":"koresar","email":"kore.sar@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/stamp-specification-1.0.4.tgz_1458960858426_0.6994425435550511"},"directories":{},"readme":"# Stamp Specification v1.6\n[![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) ![Greenkeeper Badge](https://badges.greenkeeper.io/stampit-org/stamp-specification.svg)\n\n## Introduction\n\nThis specification exists in order to define a standard format for composable factory functions (called **stamps**), and ensure compatibility between different stamp implementations.\n\n## Status\n\nThe specification is currently used by the following officially supported implementations:\n\n* [Reference Implementation](./compose.js)\n* [`@stamp`](https://www.npmjs.com/~stamp) Ecosystem of useful stamps\n* [Stampit 3.0](https://github.com/stampit-org/stampit) V3+ uses the stamp specification.\n* [Stamp Utils](https://github.com/stampit-org/stamp-utils) Functional, microlibrary style stamp utilities.\n* [react-stamp](https://github.com/troutowicz/react-stamp) A great choice for `class`-free React components.\n\n### Reading Function Signatures\n\nThis document uses the [Rtype specification](https://github.com/ericelliott/rtype#rtype) for function signatures:\n\n```js\n(param: Type) => ReturnType\n```\n\n### Composable\n\n```js\ninterface Composable: Stamp|Descriptor\n```\n\nA **composable** is one of:\n\n* A stamp.\n* A POJO (Plain Old JavaScript Object) stamp descriptor.\n\n\n### Stamp\n\nA **stamp** is a composable factory function that returns object instances based on its **descriptor**.\n\n\n```js\nstamp(options?: Object, ...args?: [...Any]) => instance: object\n```\n\n```js\nconst newObject = stamp();\n```\n\nStamps have a method called `.compose()`:\n\n```js\nStamp.compose(...args?: [...Composable]) => Stamp\n```\n\nWhen called the `.compose()` method creates new stamp using the current stamp as a base, composed with a list of *composables* passed as arguments:\n\n```js\nconst combinedStamp = baseStamp.compose(composable1, composable2, composable3);\n```\n\nThe `.compose()` method doubles as the stamp's descriptor. In other words, descriptor properties are attached to the stamp `.compose()` method, e.g. `stamp.compose.methods`.\n\n\n#### Overriding `.compose()` method\n\nIt is possible to override the `.compose()` method of a stamp using `staticProperties`. Handy for debugging purposes.\n\n```js\nimport differentComposeImplementation from 'different-compose-implementation';\nconst composeOverriddenStamp = stamp.compose({\n  staticProperties: {\n    compose: differentComposeImplementation\n  }\n});  \n```\n\n\n### Descriptor\n\n**Composable descriptor** (or just **descriptor**) is a meta data object which contains the information necessary to create an object instance.\n\n\n\n### Standalone `compose()` pure function (optional)\n\n```js\n(...args?: [...Composable]) => Stamp\n```\n\n**Creates stamps.** Take any number of stamps or descriptors. Return a new stamp that encapsulates combined behavior. If nothing is passed in, it returns an empty stamp.\n\n#### Detached `compose()` method\n\nThe `.compose()` method of any stamp can be detached and used as a standalone `compose()` pure function.\n\n```js\nconst compose = thirdPartyStamp.compose;\nconst myStamp = compose(myComposable1, myComposable2);\n```\n\n## Implementation details\n\n### Stamp\n\n```js\nStamp(options?: Object, ...args?: [...Any]) => Instance: Object\n```\n\n**Creates object instances.** Take an options object and return the resulting instance.\n\n\n```js\nStamp.compose(...args?: [...Composable]) => Stamp\n```\n\n**Creates stamps.**\n\nA method exposed by all stamps, identical to `compose()`, except it prepends `this` to the stamp parameters. Stamp descriptor properties are attached to the `.compose` method, e.g. `stamp.compose.methods`.\n\n\n### The Stamp Descriptor\n\n```js\ninterface Descriptor {\n  methods?: Object,\n  properties?: Object,\n  deepProperties?: Object,\n  propertyDescriptors?: Object,\n  staticProperties?: Object,\n  staticDeepProperties?: Object,\n  staticPropertyDescriptors?: Object,\n  initializers?: [...Function],\n  composers?: [...Function],\n  configuration?: Object,\n  deepConfiguration?: Object\n}\n```\n\nThe names and definitions of the fixed properties that form the stamp descriptor.\nThe stamp descriptor properties are made available on each stamp as `stamp.compose.*`\n\n* `methods` - A set of methods that will be added to the object's delegate prototype.\n* `properties` - A set of properties that will be added to new object instances by assignment.\n* `deepProperties` - A set of properties that will be added to new object instances by deep property merge.\n* `propertyDescriptors` - A set of [object property\ndescriptors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties) used for fine-grained control over object property behaviors.\n* `staticProperties` - A set of static properties that will be copied by assignment to the stamp.\n* `staticDeepProperties` - A set of static properties that will be added to the stamp by deep property merge.\n* `staticPropertyDescriptors` - A set of [object property descriptors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties) to apply to the stamp.\n* `initializers` - An array of functions that will run in sequence while creating an object instance from a stamp. Stamp details and arguments get passed to initializers.\n* `composers` - An array of functions that will run in sequence while creating a new stamp from a list of composables. The resulting stamp and the composables get passed to composers. \n* `configuration` - A set of options made available to the stamp and its initializers during object instance creation. These will be copied by assignment.\n* `deepConfiguration` - A set of options made available to the stamp and its initializers during object instance creation. These will be deep merged.\n\n#### Composing Descriptors\n\nDescriptors are composed together to create new descriptors with the following rules:\n\n* `methods` are copied by assignment\n* `properties` are copied by assignment\n* `deepProperties` are deep merged\n* `propertyDescriptors` are copied by assignment\n* `staticProperties` are copied by assignment\n* `staticDeepProperties` are deep merged\n* `staticPropertyDescriptors` are copied by assignment\n* `initializers` are uniquely concatenated as in `_.union()`.\n* `composers` are uniquely concatenated as in `_.union()`.\n* `configuration` are copied by assignment\n* `deepConfiguration` are deep merged\n\n##### Copying by assignment\n\nThe special property assignment algorithm shallow merges the following properties:\n* The regular string key properties `obj.foo = \"bla\"`\n* The `Symbol` key properties `obj[Symbol.for('foo')] = \"bla\"`\n* The JavaScript getters and setters `{ get foo() { return \"bla\"; }, set foo(val) { ... } }`\n\n##### Deep merging\n\nSpecial deep merging algorithm should be used when merging descriptors.\n\nValues:\n* Plain objects are deeply merged (or cloned if destination metadata property is not a plain object)\n* Arrays are concatenated using `Array.prototype.concat` which shallow copies elements to a new array instance\n* All other value types - Functions, Strings, non-plain objects, RegExp, etc. - are copied by reference\n* The last value type always overwrites the previous value type\n\nKeys:\n* The `Symbol` object keys are treated as regular string keys\n* The JavaScript getters and setters are merged as if they are regular functions, i.e. copied by reference\n\n#### Priority Rules\n\nIt is possible for properties to collide, between both stamps, and between different properties of the same stamp. This is often expected behavior, so it must not throw.\n\n**Same descriptor property, different stamps:** Last in wins.\n\n**Different descriptor properties, one or more stamps:**\n\n* Shallow properties override deep properties\n* Property Descriptors override everything\n\n\n### Stamp Arguments\n\nIt is recommended that stamps only take one argument: The stamp `options` argument. There are no reserved properties and no special meaning. However, using multiple arguments for a stamp could create conflicts where multiple stamps expect the same argument to mean different things. Using named parameters, it's possible for stamp creator to resolve conflicts with `options` namespacing. For example, if you want to compose a database connection stamp with a message queue stamp:\n\n```js\nconst db = dbStamp({\n  host: 'localhost',\n  port: 3000,\n  onConnect() {\n    console.log('Database connection established.');\n  }\n});\n\nconst queue = messageQueueStamp({\n  host: 'localhost',\n  port: 5000,\n  onComplete() {\n    console.log('Message queue connection established.');\n  }\n});\n```\n\nIf you tried to compose these directly, they would conflict with each other, but it's easy to namespace the options at compose time:\n\n```js\nconst DbQueue = compose({\n  initializers: [({db, queue}, { instance }) => {\n    instance.db = dbStamp({\n      host: db.host,\n      port: db.port,\n      onConnect: db.onConnect\n    });\n    instance.queue = messageQueueStamp({\n      host: queue.host,\n      port: queue.port,\n      onConnect: queue.onConnect\n    });\n  }]\n});\n\nmyDBQueue = DbQueue({\n  db: {\n    host: 'localhost',\n    port: 3000,\n    onConnect () {\n      console.log('Database connection established.');\n    }\n  },\n  queue: {\n    host: 'localhost',\n    port: 5000,\n    onConnect () {\n      console.log('Message queue connection established.');\n    }\n  }\n});\n```\n\n\n### Initializer Parameters\n\nInitializers have the following signature:\n\n```js\n(options: Object, { instance: Object, stamp: Stamp, args: Array }) => instance?: Object\n```\n\n* `options` The `options` argument passed into the stamp, containing properties that may be used by initializers.\n* `instance` The object instance being produced by the stamp. If the initializer returns a value other than `undefined`, it replaces the instance.\n* `stamp` A reference to the stamp producing the instance.\n* `args` An array of the arguments passed into the stamp, including the `options` argument.\n\nNote that if no `options` object is passed to the factory function, an empty object will be passed to initializers.\n\n### Composer Parameters\n\nComposers have the following signature:\n\n({ stamp: Stamp, composables: Composable[] }) => stamp?: Object\n\n* `stamp` The result of the composables composition.\n* `composables` The list of composables the `stamp` was just composed of.\n\nNote that it's not recommended to return new stamps from a composer. Instead, it's better to mutate the passed `stamp`.\n\n-----\n\n## Similarities With Promises (aka Thenables)\n\n* *Thenable* ~ *Composable*.\n* `.then` ~ `.compose`.\n* *Promise* ~ *Stamp*.\n* `new Promise(function(resolve, reject))` ~ `compose(...composables)`\n\n-----\n\n## Contributing\n\nTo contribute, please refer to the [Contributing guide](./CONTRIBUTING.md).\n","publish_time":1458960858797,"_cnpm_publish_time":1458960858797,"dist-tags":{"latest":"1.6.0"}}