uci-utils-class-merge/src/merge.js

41 lines
1.1 KiB
JavaScript

/* ==== ECMAScript 6 variant ==== */
export default (base, ...mixins) => {
/* create aggregation class */
const merge = class __Merge extends base {
constructor (...args) {
/* call base class constructor */
super(...args)
/* call mixin's initializer */
mixins.forEach((mixin) => {
if (typeof mixin.prototype.initializer === 'function')
mixin.prototype.initializer.apply(this, args)
})
}
}
/* copy properties */
let copyProps = (target, source) => {
Object.getOwnPropertyNames(source)
.concat(Object.getOwnPropertySymbols(source))
.forEach((prop) => {
if (typeof prop.match ==='function') {
if (prop.match(/^(?:initializer|constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/))
return
}
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop))
})
}
/* copy all properties of all mixins into aggregation class */
mixins.forEach((mixin) => {
copyProps(merge.prototype, mixin.prototype)
copyProps(merge, mixin)
})
return merge
}