@friktion-labs/typescript-mix
Version:
Tweaked version of https://github.com/michaelolof/typescript-mix
73 lines • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.delegate = exports.use = void 0;
function mix(client, mixins) {
if (mixins.map(m => m === undefined).reduce((b1, b2) => b1 || b2))
return;
const clientKeys = Object.getOwnPropertyNames(client.prototype);
for (let mixin of mixins) {
const mixinMixables = getMixables(clientKeys, mixin);
Object.defineProperties(client.prototype, mixinMixables);
}
}
/**
* Returns a map of mixables. That is things that can be mixed in
*/
function getMixables(clientKeys, mixin) {
const visitedPrototypes = [];
let descriptors = {};
switch (typeof mixin) {
case "object":
descriptors = getMixables(mixin);
break;
case "function":
descriptors = getMixables(mixin.prototype);
break;
}
return descriptors;
function getMixables(obj) {
const map = {};
const base = Object.getPrototypeOf(obj);
if (base !== Object.prototype && visitedPrototypes.indexOf(base) === -1) {
var baseDescriptors = getMixables(base) || {};
for (var i in baseDescriptors) {
map[i] = baseDescriptors[i];
}
visitedPrototypes.push(base);
}
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") {
map[key] = descriptor;
}
}
});
return map;
}
}
/**
* Takes a list of classes or object literals and adds their methods
* to the class calling it.
*/
function use(...options) {
return function (target, propertyKey) {
mix(target.constructor, options.reverse());
};
}
exports.use = use;
/**
* Takes a method as a parameter and add it to the class calling it.
*/
function delegate(method) {
return function (target, propertyKey) {
target.constructor.prototype[propertyKey] = method;
};
}
exports.delegate = delegate;
//# sourceMappingURL=index.js.map