Skip to content

Instantly share code, notes, and snippets.

@enten
Last active January 2, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enten/2ea6e0585bfc7d76066a9fd1188f2064 to your computer and use it in GitHub Desktop.
Save enten/2ea6e0585bfc7d76066a9fd1188f2064 to your computer and use it in GitHub Desktop.
Stamp factory which allows extra properties
const assign = require('@stamp/core/assign')
const compose = require('@stamp/compose')
const isStamp = require('@stamp/is/stamp')
const merge = require('@stamp/core/merge')
const STAMP_DESCRIPTOR_VOID = {
'methods': {},
'properties': {},
'deepProperties': {},
'propertyDescriptors': {},
'staticProperties': {},
'staticDeepProperties': {},
'staticPropertyDescriptors': {},
'initializers': [],
'composers': [],
'configuration': {},
'deepConfiguration': {}
}
const compose2 = compose.bind({
composers: [extraComposer]
})
function extraComposer ({stamp, composables}) {
for (let i = 0; i < composables.length; ++i) {
const composable = composables[i]
const descriptor = isStamp(composable) ? composable.compose : composable
for (let key in descriptor) {
if (key in STAMP_DESCRIPTOR_VOID) {
continue
}
if (!(key in stamp.compose)) {
stamp.compose[key] = descriptor[key]
continue
}
if (Array.isArray(stamp.compose[key])) {
stamp.compose[key].push(...descriptor[key])
continue
}
if (/deep|Deep/.test(key)) {
merge(stamp.compose[key], descriptor[key])
continue
}
assign(stamp.compose[key], descriptor[key])
}
}
}
module.exports = compose2
const s = compose2({
foobar: {
foo: {
bar: ['bar']
}
},
deepFoobar: {
foo: {
bar: ['bar']
}
}
}).compose({
foobar: {
foo: {
bar: ['BAR']
}
},
deepFoobar: {
foo: {
bar: ['BAR']
}
}
})
// { [Function: Stamp]
// compose:
// { [Function: _compose]
// composers: [ [Function: extraComposer] ],
// foobar: { foo: { bar: [ 'BAR' ] } },
// deepFoobar: { foo: { bar: [ 'bar', 'BAR' ] } } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment