Skip to content

fix: simplify code #52

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

Merged
merged 1 commit into from
Jun 13, 2021
Merged
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
26 changes: 10 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
export class Base {
constructor(options = {}) {
this.options = options;
// apply plugins
// https://stackoverflow.com/a/16345172
const classConstructor = this.constructor;
classConstructor.plugins.forEach((plugin) => {
this.constructor.plugins.forEach((plugin) => {
Object.assign(this, plugin(this, options));
});
}
static plugin(plugin1, ...additionalPlugins) {
var _a;

static plugin(...newPlugins) {
const currentPlugins = this.plugins;
let newPlugins = [plugin1, ...additionalPlugins];
const BaseWithPlugins =
((_a = class extends this {}),
(_a.plugins = currentPlugins.concat(
return class extends this {
static plugins = currentPlugins.concat(
newPlugins.filter((plugin) => !currentPlugins.includes(plugin))
)),
_a);
return BaseWithPlugins;
);
};
}
static defaults(defaults) {
const BaseWitDefaults = class extends this {
return class extends this {
constructor(...args) {
super(Object.assign({}, defaults, args[0] || {}));
}
};
return BaseWitDefaults;
}

static plugins = [];
}
Base.plugins = [];