Skip to content

added check on base class descriptors #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 22 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ export type Constructor<T> = new (...args: any[]) => T;
export type Mixin<T> = Constructor<T> | object;

function mix(client: Constructor<any>, mixins: Mixin<any>[]) {
const clientKeys = Object.getOwnPropertyNames( client.prototype );
const clientKeys = Object.getOwnPropertyNames(client.prototype);
for (let mixin of mixins) {
const mixinMixables = getMixables(clientKeys, mixin);
Object.defineProperties( client.prototype, mixinMixables );
Object.defineProperties(client.prototype, mixinMixables);
}
}

/**
* Returns a map of mixables. That is things that can be mixed in
*/
function getMixables(clientKeys:string[], mixin: Mixin<any>) {
let descriptors:PropertyDescriptorMap = {};
function getMixables(clientKeys: string[], mixin: Mixin<any>) {
let descriptors: PropertyDescriptorMap = {};
switch (typeof mixin) {
case "object":
descriptors = getMixables(mixin);
Expand All @@ -24,24 +24,31 @@ function getMixables(clientKeys:string[], mixin: Mixin<any>) {
}
return descriptors;

function getMixables(obj:object):PropertyDescriptorMap {
const map:PropertyDescriptorMap = {};
Object.getOwnPropertyNames( obj ).map( key => {
if( clientKeys.indexOf( key ) < 0 ) {
const descriptor = Object.getOwnPropertyDescriptor( obj, key );
if( descriptor === undefined ) return
if( descriptor.get || descriptor.set ) {
function getMixables(obj: object):PropertyDescriptorMap {
const map: PropertyDescriptorMap = {};
const base = Object.getPrototypeOf(obj);
if (base !== Object.prototype) {
var baseDescriptors = getMixables(base) || {};
for (var i in baseDescriptors) {
map[i] = baseDescriptors[i];
}
}
Object.getOwnPropertyNames(obj).map(key => {
if(clientKeys.indexOf(key) < 0) {
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if(descriptor === undefined) return
if(descriptor.get || descriptor.set) {
map[ key ] = descriptor;
}
else
if ( typeof descriptor.value === "function" ) {
if (typeof descriptor.value === "function") {
map[ key ] = descriptor;
}
}
})
return map;
}

}

/**
Expand All @@ -55,10 +62,10 @@ export function use(...options: Mixin<any>[]) {
}

/**
* Takes a method as a parameter and add it to the class calling it.
* Takes a method as a parameter and add it to the class calling it.
*/
export function delegate(method: (...args: any[]) => any) {
return function (target: any, propertyKey: string) {
target.constructor.prototype[propertyKey] = method;
}
}
}