|
1 | | -"use strict"; |
2 | | -var __extends = (this && this.__extends) || (function () { |
3 | | - var extendStatics = function (d, b) { |
4 | | - extendStatics = Object.setPrototypeOf || |
5 | | - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || |
6 | | - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; |
7 | | - return extendStatics(d, b); |
| 1 | +export class Base { |
| 2 | + constructor(options = {}) { |
| 3 | + this.options = options; |
| 4 | + // apply plugins |
| 5 | + // https://stackoverflow.com/a/16345172 |
| 6 | + const classConstructor = this.constructor; |
| 7 | + classConstructor.plugins.forEach((plugin) => { |
| 8 | + Object.assign(this, plugin(this, options)); |
| 9 | + }); |
| 10 | + } |
| 11 | + static plugin(plugin1, ...additionalPlugins) { |
| 12 | + var _a; |
| 13 | + const currentPlugins = this.plugins; |
| 14 | + let newPlugins = [plugin1, ...additionalPlugins]; |
| 15 | + const BaseWithPlugins = |
| 16 | + ((_a = class extends this {}), |
| 17 | + (_a.plugins = currentPlugins.concat( |
| 18 | + newPlugins.filter((plugin) => !currentPlugins.includes(plugin)) |
| 19 | + )), |
| 20 | + _a); |
| 21 | + return BaseWithPlugins; |
| 22 | + } |
| 23 | + static defaults(defaults) { |
| 24 | + const BaseWitDefaults = class extends this { |
| 25 | + constructor(...args) { |
| 26 | + super(Object.assign({}, defaults, args[0] || {})); |
| 27 | + } |
8 | 28 | }; |
9 | | - return function (d, b) { |
10 | | - extendStatics(d, b); |
11 | | - function __() { this.constructor = d; } |
12 | | - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); |
13 | | - }; |
14 | | -})(); |
15 | | -var __spreadArrays = (this && this.__spreadArrays) || function () { |
16 | | - for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; |
17 | | - for (var r = Array(s), k = 0, i = 0; i < il; i++) |
18 | | - for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) |
19 | | - r[k] = a[j]; |
20 | | - return r; |
21 | | -}; |
22 | | -exports.__esModule = true; |
23 | | -exports.Base = void 0; |
24 | | -var Base = /** @class */ (function () { |
25 | | - function Base(options) { |
26 | | - var _this = this; |
27 | | - if (options === void 0) { options = {}; } |
28 | | - this.options = options; |
29 | | - // apply plugins |
30 | | - // https://stackoverflow.com/a/16345172 |
31 | | - var classConstructor = this.constructor; |
32 | | - classConstructor.plugins.forEach(function (plugin) { |
33 | | - Object.assign(_this, plugin(_this, options)); |
34 | | - }); |
35 | | - } |
36 | | - Base.plugin = function (plugin1) { |
37 | | - var _a; |
38 | | - var additionalPlugins = []; |
39 | | - for (var _i = 1; _i < arguments.length; _i++) { |
40 | | - additionalPlugins[_i - 1] = arguments[_i]; |
41 | | - } |
42 | | - var currentPlugins = this.plugins; |
43 | | - var newPlugins = __spreadArrays([ |
44 | | - plugin1 |
45 | | - ], additionalPlugins); |
46 | | - var BaseWithPlugins = (_a = /** @class */ (function (_super) { |
47 | | - __extends(class_1, _super); |
48 | | - function class_1() { |
49 | | - return _super !== null && _super.apply(this, arguments) || this; |
50 | | - } |
51 | | - return class_1; |
52 | | - }(this)), |
53 | | - _a.plugins = currentPlugins.concat(newPlugins.filter(function (plugin) { return !currentPlugins.includes(plugin); })), |
54 | | - _a); |
55 | | - return BaseWithPlugins; |
56 | | - }; |
57 | | - Base.defaults = function (defaults) { |
58 | | - var BaseWitDefaults = /** @class */ (function (_super) { |
59 | | - __extends(class_2, _super); |
60 | | - function class_2() { |
61 | | - var args = []; |
62 | | - for (var _i = 0; _i < arguments.length; _i++) { |
63 | | - args[_i] = arguments[_i]; |
64 | | - } |
65 | | - return _super.call(this, Object.assign({}, defaults, args[0] || {})) || this; |
66 | | - } |
67 | | - return class_2; |
68 | | - }(this)); |
69 | | - return BaseWitDefaults; |
70 | | - }; |
71 | | - Base.plugins = []; |
72 | | - return Base; |
73 | | -}()); |
74 | | -exports.Base = Base; |
| 29 | + return BaseWitDefaults; |
| 30 | + } |
| 31 | +} |
| 32 | +Base.plugins = []; |
0 commit comments