Skip to content

feat: native ES module #51

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 13 commits into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 0 additions & 17 deletions .github/workflows/build.yml

This file was deleted.

1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm ci
- run: npm run build
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- master
- "greenkeeper/**"
pull_request:
types: [opened, synchronize]

Expand Down
61 changes: 43 additions & 18 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
declare type Options = {
[key: string]: unknown;
[key: string]: unknown;
};
declare type ApiExtension = {
[key: string]: unknown;
[key: string]: unknown;
};
declare type TestPlugin = (instance: Base, options: Options) => ApiExtension | undefined;
declare type Plugin = (instance: Base, options: Options) => ApiExtension | void;

declare type Constructor<T> = new (...args: any[]) => T;
/**
* @author https://stackoverflow.com/users/2887218/jcalz
* @see https://stackoverflow.com/a/50375286/10325032
*/
declare type UnionToIntersection<Union> = (Union extends any ? (argument: Union) => void : never) extends (argument: infer Intersection) => void ? Intersection : never;
declare type UnionToIntersection<Union> = (
Union extends any ? (argument: Union) => void : never
) extends (argument: infer Intersection) => void
? Intersection
: never;
declare type AnyFunction = (...args: any) => any;
declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> = T extends AnyFunction ? ReturnType<T> : T extends AnyFunction[] ? UnionToIntersection<ReturnType<T[number]>> : never;
declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> =
T extends AnyFunction
? ReturnType<T>
: T extends AnyFunction[]
? UnionToIntersection<ReturnType<T[number]>>
: never;

export declare class Base<TOptions extends Options = Options> {
static plugins: TestPlugin[];
static plugin<S extends Constructor<any> & {
plugins: any[];
}, T1 extends TestPlugin, T2 extends TestPlugin[]>(this: S, plugin1: T1, ...additionalPlugins: T2): S & {
plugins: any[];
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
static defaults<TDefaults extends Options, S extends Constructor<Base<TDefaults>>>(this: S, defaults: TDefaults): {
new (...args: any[]): {
options: TDefaults;
};
} & S;
constructor(options?: TOptions);
options: TOptions;
static plugins: Plugin[];
static plugin<
S extends Constructor<any> & {
plugins: any[];
},
T1 extends Plugin,
T2 extends Plugin[]
>(
this: S,
plugin1: T1,
...additionalPlugins: T2
): S & {
plugins: any[];
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
static defaults<
TDefaults extends Options,
S extends Constructor<Base<TDefaults>>
>(
this: S,
defaults: TDefaults
): {
new (...args: any[]): {
options: TDefaults;
};
} & S;
constructor(options?: TOptions);
options: TOptions;
}
export {};
104 changes: 31 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,32 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
export class Base {
constructor(options = {}) {
this.options = options;
// apply plugins
// https://stackoverflow.com/a/16345172
const classConstructor = this.constructor;
classConstructor.plugins.forEach((plugin) => {
Object.assign(this, plugin(this, options));
});
}
static plugin(plugin1, ...additionalPlugins) {
var _a;
const currentPlugins = this.plugins;
let newPlugins = [plugin1, ...additionalPlugins];
const BaseWithPlugins =
((_a = class extends this {}),
(_a.plugins = currentPlugins.concat(
newPlugins.filter((plugin) => !currentPlugins.includes(plugin))
)),
_a);
return BaseWithPlugins;
}
static defaults(defaults) {
const BaseWitDefaults = class extends this {
constructor(...args) {
super(Object.assign({}, defaults, args[0] || {}));
}
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
exports.__esModule = true;
exports.Base = void 0;
var Base = /** @class */ (function () {
function Base(options) {
var _this = this;
if (options === void 0) { options = {}; }
this.options = options;
// apply plugins
// https://stackoverflow.com/a/16345172
var classConstructor = this.constructor;
classConstructor.plugins.forEach(function (plugin) {
Object.assign(_this, plugin(_this, options));
});
}
Base.plugin = function (plugin1) {
var _a;
var additionalPlugins = [];
for (var _i = 1; _i < arguments.length; _i++) {
additionalPlugins[_i - 1] = arguments[_i];
}
var currentPlugins = this.plugins;
var newPlugins = __spreadArrays([
plugin1
], additionalPlugins);
var BaseWithPlugins = (_a = /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
return class_1;
}(this)),
_a.plugins = currentPlugins.concat(newPlugins.filter(function (plugin) { return !currentPlugins.includes(plugin); })),
_a);
return BaseWithPlugins;
};
Base.defaults = function (defaults) {
var BaseWitDefaults = /** @class */ (function (_super) {
__extends(class_2, _super);
function class_2() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _super.call(this, Object.assign({}, defaults, args[0] || {})) || this;
}
return class_2;
}(this));
return BaseWitDefaults;
};
Base.plugins = [];
return Base;
}());
exports.Base = Base;
return BaseWitDefaults;
}
}
Base.plugins = [];
22 changes: 22 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expectType } from "tsd";
import { Base } from "./index.js";

const fooPlugin = () => {
return {
foo: () => "foo",
};
};

const MyBase = Base.plugin(fooPlugin).defaults({
default: "value",
});
const base = new MyBase({
option: "value",
});

expectType<string>(base.options.default);
expectType<string>(base.options.option);
expectType<string>(base.foo());

// @ts-expect-error unknown properties cannot be used, see #31
base.unknown;
Loading