Last active
January 2, 2018 20:41
-
-
Save enten/2ea6e0585bfc7d76066a9fd1188f2064 to your computer and use it in GitHub Desktop.
Stamp factory which allows extra properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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