UNPKG

foibles

Version:

Composition and mixins and TypeScript classes

59 lines 1.7 kB
"use strict"; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Take the given base class and turn it into an extendable class, giving it * a static method `with` that can be used to apply mixins. * * When using TypeScript this should be accompanied with a type definition * using `Extendable`: * * ```typescript * type ExampleBaseType = Extendable<typeof ExampleBaseType>; * const ExampleBaseType = toExtendable(class ExampleBaseType { * ... * }); * ``` * * @param o */ function toExtendable(o) { Object.defineProperty(o, 'with', { enumerable: false, value: mixer }); return o; } exports.toExtendable = toExtendable; var mixer = function () { var e_1, _a; var mixins = []; for (var _i = 0; _i < arguments.length; _i++) { mixins[_i] = arguments[_i]; } var result = this; try { for (var mixins_1 = __values(mixins), mixins_1_1 = mixins_1.next(); !mixins_1_1.done; mixins_1_1 = mixins_1.next()) { var mixin = mixins_1_1.value; result = mixin(result); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (mixins_1_1 && !mixins_1_1.done && (_a = mixins_1.return)) _a.call(mixins_1); } finally { if (e_1) throw e_1.error; } } return result; }; //# sourceMappingURL=extendable.js.map