1.0.0 • Published 6 years ago
merge-members v1.0.0
merge-class
Merge members of multiple es6 classes.
Installation
npm i merge-class
Quick Start
import Members from 'merge-class'
class Bar {}
class Foo {
example() {
console.log('Hello World!')
}
}
Members.from(Foo).to(Bar)
new Bar().example() // -> Hello World!
Documentation
The Members
class is a map of property keys to property descriptors.
import Members from 'merge-class'
new Members(members)
Create a new member map.
- members
<array>
- Optional. An array with array key-value pairs where the keys are the member keys (strings or symbols) and the values are property descriptors.
Members.fromObject(source, object)
Get members from an object.
This includes all property descriptors returned by Object.getOwnPropertyDescriptors(..)
and property descriptors for this symbols: Symbol.iterator, Symbol.asyncIterator, Symbol.match, Symbol.replace, Symbol.search, Symbol.split, Symbol.hasInstance, Symbol.toPrimitive
const members = Members.fromObject(MyClass.prototype)
// To get static members:
const members = Members.fromObject(MyClass)
- source
<object> | <function>
- The source object to get members from. - options
<object> | <function>
- Optional. An object with the following options or the.filter
option itself: + filter<function>
- Optional. Filter which members are added to the member map. See filter functions. + symbols<array>
- Optional. An array with additional symbols to get property descriptors. - returns
<Members>
- A map with members.
Members.from(source, options)
Members.from(MyClass, options)
// is a shorthand for:
Members.fromObject(MyClass.prototype, options)
members.filter(filter)
Get a new member map with members that match the specified filter.
const filteredMembers = members.filter(filter)
- filter
<function>
- See filter functions. - returns
<Members>
- A map with filtered members.
members.toObject(target, filter)
Define members for the specified target.
members.toObject(MyOtherClass.prototype)
- target
<object> | <function>
- The target object to define members for. - filter
<function>
- A function to filter members. See filter functions
members.to(target, filter)
members.to(MyOtherClass, filter)
// is a shorthand for:
members.toObject(MyOtherClass.prototype, filter)
Filter Functions
function filter(name, descriptor) {
return include
}
- name
<string> | <symbol>
- The name (or the symbol) of the member. - descriptor
<object>
- The property descriptor. - returns
<boolean>
-true
to include the member,false
to exclude.
Members.methodsOnly
Include only methods:
const filteredMembers = members.filter(Members.methodsOnly)
Members.propertiesOnly
Include only properties:
const filteredMembers = members.filter(Members.propertiesOnly)
1.0.0
6 years ago