Skip to content

Commit 9af5851

Browse files
committed
wip
1 parent 3378eae commit 9af5851

File tree

5 files changed

+78
-172
lines changed

5 files changed

+78
-172
lines changed

index.d.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,58 @@
11
declare type Options = {
2-
[key: string]: unknown;
2+
[key: string]: unknown;
33
};
44
declare type ApiExtension = {
5-
[key: string]: unknown;
5+
[key: string]: unknown;
66
};
7-
declare type TestPlugin = (instance: Base, options: Options) => ApiExtension | undefined;
7+
declare type TestPlugin = (
8+
instance: Base,
9+
options: Options
10+
) => ApiExtension | undefined;
811
declare type Constructor<T> = new (...args: any[]) => T;
912
/**
1013
* @author https://stackoverflow.com/users/2887218/jcalz
1114
* @see https://stackoverflow.com/a/50375286/10325032
1215
*/
13-
declare type UnionToIntersection<Union> = (Union extends any ? (argument: Union) => void : never) extends (argument: infer Intersection) => void ? Intersection : never;
16+
declare type UnionToIntersection<Union> = (
17+
Union extends any ? (argument: Union) => void : never
18+
) extends (argument: infer Intersection) => void
19+
? Intersection
20+
: never;
1421
declare type AnyFunction = (...args: any) => any;
15-
declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> = T extends AnyFunction ? ReturnType<T> : T extends AnyFunction[] ? UnionToIntersection<ReturnType<T[number]>> : never;
22+
declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> =
23+
T extends AnyFunction
24+
? ReturnType<T>
25+
: T extends AnyFunction[]
26+
? UnionToIntersection<ReturnType<T[number]>>
27+
: never;
28+
1629
export declare class Base<TOptions extends Options = Options> {
17-
static plugins: TestPlugin[];
18-
static plugin<S extends Constructor<any> & {
19-
plugins: any[];
20-
}, T1 extends TestPlugin, T2 extends TestPlugin[]>(this: S, plugin1: T1, ...additionalPlugins: T2): S & {
21-
plugins: any[];
22-
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
23-
static defaults<TDefaults extends Options, S extends Constructor<Base<TDefaults>>>(this: S, defaults: TDefaults): {
24-
new (...args: any[]): {
25-
options: TDefaults;
26-
};
27-
} & S;
28-
constructor(options?: TOptions);
29-
options: TOptions;
30+
static plugins: TestPlugin[];
31+
static plugin<
32+
S extends Constructor<any> & {
33+
plugins: any[];
34+
},
35+
T1 extends TestPlugin,
36+
T2 extends TestPlugin[]
37+
>(
38+
this: S,
39+
plugin1: T1,
40+
...additionalPlugins: T2
41+
): S & {
42+
plugins: any[];
43+
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
44+
static defaults<
45+
TDefaults extends Options,
46+
S extends Constructor<Base<TDefaults>>
47+
>(
48+
this: S,
49+
defaults: TDefaults
50+
): {
51+
new (...args: any[]): {
52+
options: TDefaults;
53+
};
54+
} & S;
55+
constructor(options?: TOptions);
56+
options: TOptions;
3057
}
3158
export {};

index.js

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,32 @@
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+
}
828
};
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 = [];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"types": "./index.d.ts",
77
"scripts": {
8-
"build": "tsc src/index.ts --declaration --outDir .",
8+
"build": "tsc --declaration",
99
"test": "npm run -s test:code && npm run -s test:typescript",
1010
"test:code": "jest --coverage",
1111
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals test-typescript.ts"

src/index.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

test-typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// THIS CODE IS NOT EXECUTED. IT IS JUST FOR TYPECHECKING
33
// ************************************************************
44

5-
import { Base } from "./src";
5+
import { Base } from "./index";
66

77
function isString(what: string) {}
88

0 commit comments

Comments
 (0)