stampit API
Search…
Merging algorithm
When composing stamps you actually merge their descriptors (aka metadata).
The Object.assign is used for these descriptor properties:
The special deep merging algorithm (see below) is used for these descriptor properties:
The array concatenation and deduplication (see below) is used for these descriptor properties:

Array concatenation and deduplication

This is how initializers and composers metadata gets merged.
1
const _ = require('lodash')
2
3
descriptor.initializers = _.uniq(_.concat(descriptor1.initializers, descriptor2.initializers))
4
descriptor.composers = _.uniq(_.concat(descriptor1.composers, descriptor2.composers))
Copied!
See the exact line of the reference implementation source code.

Deep merging algorithm

The stamp specification standardized the deep merging algorithm to, basically, this:
  • Plain objects are (recursively) deep merged, including ES6 Symbol keys, ES5 getters and setters.
  • Arrays are concatenated.
  • Functions, Symbols, RegExp, etc. values are copied by reference.
  • The last object type overwrites previous object type.
1
const MyStamp1 = stampit()
2
3
const deepObject1 = {
4
[Symbol('foo')]: { one: 'first' }, // this plain object will be deep merged
5
6
array: [0, 'bar', () => {}, { obj: 'my object' }], // this array will be concatenated with
7
8
func: MyStamp1, // this stamp will be replaced with another stamp
9
10
something: [42], // this array will be replaced with an object
11
12
oldKey: 'some value'
13
}
14
15
const MyStamp2 = stampit()
16
17
const deepObject2 = {
18
[Symbol('foo')]: { two: 'second' },
19
20
array: [0, 'bar', { another: 'object' }],
21
22
func: MyStamp2,
23
24
something: { [0]: 42 },
25
26
newKey: 'some value'
27
}
Copied!
The merged result of the two objects above will be this:
1
const deepResult = {
2
[Symbol('foo')]: { one: 'first', two: 'second' }, // contains properties from both objects
3
4
array: [0, 'bar', () => {}, { obj: 'my object' }, 0, 'bar', { another: 'object' }], // both arrays are here
5
6
func: MyStamp2, // Stamp2 replaced the Stamp1
7
8
something: { [0]: 42 }, // the array was replaced with the object
9
10
oldKey: 'some value', // these two were simply carried across
11
newKey: 'some value'
12
}
Copied!
See the exact line of the reference implementation source code.
Last modified 2yr ago