Skip to content

Commit

Permalink
fixed esm project structure (#14)
Browse files Browse the repository at this point in the history
* esm import style
* fixed esm in package.json
  • Loading branch information
maddin1502 authored Jun 28, 2023
1 parent c619e81 commit 88d8f96
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"files.encoding": "utf8",
"files.trimTrailingWhitespace": true,
"diffEditor.ignoreTrimWhitespace": false,
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"typescript.preferences.importModuleSpecifierEnding": "js"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "ts-lib-extended",
"version": "2.0.4",
"version": "2.0.5",
"description": "Additional types and tools for typescript",
"files": [
"dist"
],
"module": "dist",
"types": "dist",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"build": "tsc -p ./tsconfig.prod.json",
Expand Down
24 changes: 15 additions & 9 deletions src/array.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export type ArrayItem<T extends IterableIterator<any> | ArrayLike<any>>
= T extends { [Symbol.iterator](): IterableIterator<infer P> }
export type ArrayItem<T extends IterableIterator<any> | ArrayLike<any>> =
T extends { [Symbol.iterator](): IterableIterator<infer P> }
? P
: T extends ArrayLike<infer P>
? P
: never;
? P
: never;

type BuildMinArray<TItem, TMin extends number, TFix extends TItem[]>
= TFix['length'] extends TMin
? TFix
: BuildMinArray<TItem, TMin, [...TFix, TItem]>;
export type MinArray<TItem, TMin extends number> = [...BuildMinArray<TItem, TMin, []>, ...TItem[]];
type BuildMinArray<
TItem,
TMin extends number,
TFix extends TItem[]
> = TFix['length'] extends TMin
? TFix
: BuildMinArray<TItem, TMin, [...TFix, TItem]>;
export type MinArray<TItem, TMin extends number> = [
...BuildMinArray<TItem, TMin, []>,
...TItem[]
];
29 changes: 18 additions & 11 deletions src/constructor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
export type StandardConstructor<T = any, A extends any[] = any[]> = new (...args: A) => T;
export type AbstractConstructor<T = any, A extends any[] = any[]> = abstract new (...params: A) => T;
export type Constructor<T = any> = StandardConstructor<T> | AbstractConstructor<T>;
export type ConstructorInstance<C extends Constructor>
= C extends StandardConstructor<infer Instance>
export type StandardConstructor<T = any, A extends any[] = any[]> = new (
...args: A
) => T;
export type AbstractConstructor<
T = any,
A extends any[] = any[]
> = abstract new (...params: A) => T;
export type Constructor<T = any> =
| StandardConstructor<T>
| AbstractConstructor<T>;
export type ConstructorInstance<C extends Constructor> =
C extends StandardConstructor<infer Instance>
? Instance
: C extends AbstractConstructor<infer Instance>
? Instance
: never;
export type ConstructorParameters<C extends Constructor>
= C extends StandardConstructor<any, infer Arguments>
? Instance
: never;
export type ConstructorParameters<C extends Constructor> =
C extends StandardConstructor<any, infer Arguments>
? Arguments
: C extends AbstractConstructor<any, infer Arguments>
? Arguments
: never;
? Arguments
: never;
5 changes: 4 additions & 1 deletion src/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type Dictionary<T = any, K extends string | number = string> = {
[key in K]?: T;
};
export type ReadonlyDictionary<T = any, K extends string | number = string> = Readonly<Dictionary<T,K>>;
export type ReadonlyDictionary<
T = any,
K extends string | number = string
> = Readonly<Dictionary<T, K>>;
export type DictionaryKey<D> = D extends Dictionary<any, infer P> ? P : never;
export type DictionaryValue<D> = D extends Dictionary<infer P> ? P : never;
4 changes: 3 additions & 1 deletion src/disposable/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export abstract class DisposableBase {
this._isDisposed = false;
}

public get isDisposed(): boolean { return this._isDisposed; }
public get isDisposed(): boolean {
return this._isDisposed;
}

/**
* Dispose the instance (cleanup internals). The instance should and can no longer be used afterwards
Expand Down
17 changes: 10 additions & 7 deletions src/disposable/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Event } from '../event';
import { EventHandler } from '../event/handler';
import { DisposableBase } from './base';
import { EventHandler } from '../event/handler.js';
import type { Event } from '../event/index.js';
import { DisposableBase } from './base.js';

/**
* Core for disposable instances
Expand All @@ -9,8 +9,7 @@ import { DisposableBase } from './base';
* @class Disposable
* @extends {DisposableBase}
*/
export class Disposable extends DisposableBase
{
export class Disposable extends DisposableBase {
private _disposingHandler: EventHandler<this>;
private _disposedHandler: EventHandler<this>;
protected _disposers: (() => void)[];
Expand All @@ -29,7 +28,9 @@ export class Disposable extends DisposableBase
* @type {Event<this>}
* @memberof Disposable
*/
public get disposing(): Event<this> { return this._disposingHandler.event; }
public get disposing(): Event<this> {
return this._disposingHandler.event;
}

/**
* Event that is invoked after the instance is disposed
Expand All @@ -38,7 +39,9 @@ export class Disposable extends DisposableBase
* @type {Event<this>}
* @memberof Disposable
*/
public get disposed(): Event<this> { return this._disposedHandler.event; }
public get disposed(): Event<this> {
return this._disposedHandler.event;
}

protected disposingInstance(): void {
this._disposingHandler.invoke(this);
Expand Down
45 changes: 25 additions & 20 deletions src/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ export type Enumerable<T = any> = {
[nu: number]: string;
};

export type EnumerableValue<TEnum extends Enumerable = Enumerable>
= TEnum extends Record<infer K, infer V>
export type EnumerableValue<TEnum extends Enumerable = Enumerable> =
TEnum extends Record<infer K, infer V>
? K extends string
? V
: never
: never;

export type EnumerableBase<TEnumValue extends EnumerableValue>
= TEnumValue extends string
export type EnumerableBase<TEnumValue extends EnumerableValue> =
TEnumValue extends string
? TEnumValue extends number
? string | number
: string
: TEnumValue extends number
? number
: never;
? number
: never;

export type EnumerableEntry<E extends Enumerable> = [ keyof E, EnumerableValue<E> ];
export type EnumerableEntry<E extends Enumerable> = [
keyof E,
EnumerableValue<E>
];

/**
* Gain keys and values from enum instances. Works with string, numeric and mixed enums
Expand All @@ -37,12 +40,13 @@ export class EnumerableObject {
* @return {*} {ReadonlyArray<EnumerableValue<E>>}
* @memberof EnumerableObject
*/
public values<E extends Enumerable>(enum_: E): ReadonlyArray<EnumerableValue<E>> {
public values<E extends Enumerable>(
enum_: E
): ReadonlyArray<EnumerableValue<E>> {
const values: EnumerableValue<E>[] = [];

this.disassemble(
enum_,
key_ => values.push(enum_[key_] as EnumerableValue<E>)
this.disassemble(enum_, (key_) =>
values.push(enum_[key_] as EnumerableValue<E>)
);

return values;
Expand All @@ -59,10 +63,7 @@ export class EnumerableObject {
public keys<E extends Enumerable>(enum_: E): ReadonlyArray<keyof E> {
const keys: (keyof E)[] = [];

this.disassemble(
enum_,
key_ => keys.push(key_)
);
this.disassemble(enum_, (key_) => keys.push(key_));

return keys;
}
Expand All @@ -75,18 +76,22 @@ export class EnumerableObject {
* @return {*} {ReadonlyArray<EnumerableEntry<E>>}
* @memberof EnumerableObject
*/
public entries<E extends Enumerable>(enum_: E): ReadonlyArray<EnumerableEntry<E>> {
public entries<E extends Enumerable>(
enum_: E
): ReadonlyArray<EnumerableEntry<E>> {
const entries: EnumerableEntry<E>[] = [];

this.disassemble(
enum_,
key_ => entries.push([ key_, enum_[key_] ] as EnumerableEntry<E>)
this.disassemble(enum_, (key_) =>
entries.push([key_, enum_[key_]] as EnumerableEntry<E>)
);

return entries;
}

private disassemble<E extends Enumerable>(enum_: E, processKey_: (key_: keyof E) => void): void {
private disassemble<E extends Enumerable>(
enum_: E,
processKey_: (key_: keyof E) => void
): void {
const keys = Object.keys(enum_);

for (let i = 0; i < keys.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/event/args/cancel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventArgs } from '.';
import { EventArgs } from './index.js';

/**
* EventArgs that can be used to cancel the execution of an event
Expand Down
6 changes: 2 additions & 4 deletions src/event/args/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @class EventArgs
* @template TValue
*/
export class EventArgs<TValue = any> {
constructor(
public readonly value: TValue
) {}
export class EventArgs<TValue = any> {
constructor(public readonly value: TValue) {}
}
22 changes: 14 additions & 8 deletions src/event/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Event } from '.';
import type { Dictionary } from '../dictionary';
import { DisposableBase } from '../disposable/base';
import type { EventArgs } from './args';
import type { EventCallback } from './types';
import type { Dictionary } from '../dictionary.js';
import { DisposableBase } from '../disposable/base.js';
import type { EventArgs } from './args/index.js';
import { Event } from './index.js';
import type { EventCallback } from './types.js';

/**
* A handler that provides subscribable, public events.
Expand All @@ -14,7 +14,10 @@ import type { EventCallback } from './types';
* @template TSender
* @template TArgs
*/
export class EventHandler<TSender, TArgs extends EventArgs | void = void> extends DisposableBase {
export class EventHandler<
TSender,
TArgs extends EventArgs | void = void
> extends DisposableBase {
private _callbacks: Dictionary<EventCallback<TSender, TArgs>>;
private _event: Event<TSender, TArgs> | undefined;
private _detachEvent: (() => void) | undefined;
Expand All @@ -25,7 +28,7 @@ export class EventHandler<TSender, TArgs extends EventArgs | void = void> extend
this._event = new Event(
(...args_) => this.subscribe(...args_),
(...args_) => this.unsubscribe(...args_),
detachEvent_ => {
(detachEvent_) => {
this._detachEvent = detachEvent_;
}
);
Expand Down Expand Up @@ -57,7 +60,10 @@ export class EventHandler<TSender, TArgs extends EventArgs | void = void> extend
}
}

private subscribe(identifier_: string, callback_: EventCallback<TSender, TArgs>): boolean {
private subscribe(
identifier_: string,
callback_: EventCallback<TSender, TArgs>
): boolean {
if (identifier_ in this._callbacks) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { EventArgs } from './args';
import type { EventSubscription, EventUnsubscription } from './types';
import type { EventArgs } from './args/index.js';
import type { EventSubscription, EventUnsubscription } from './types.js';

/**
* Subscribable listener to certain events
Expand Down
15 changes: 10 additions & 5 deletions src/event/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { EventArgs } from './args';
import type { EventArgs } from './args/index.js';

export type EventSubscription<TSender, TArgs extends EventArgs | void = void>
= (identifier_: string, callback_: EventCallback<TSender, TArgs>) => boolean;

export type EventSubscription<
TSender,
TArgs extends EventArgs | void = void
> = (identifier_: string, callback_: EventCallback<TSender, TArgs>) => boolean;

export type EventUnsubscription = (identifier_: string) => boolean;

export type EventCallback<TSender, TArgs extends EventArgs | void = void>
= (sender_: TSender, eventArgs_: TArgs) => void;
export type EventCallback<TSender, TArgs extends EventArgs | void = void> = (
sender_: TSender,
eventArgs_: TArgs
) => void;
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { EnumerableObject } from './enumerable';
import { EnumerableObject } from './enumerable.js';

export type { ArrayItem, MinArray } from './array';
export type { ArrayItem, MinArray } from './array.js';
export type {
AbstractConstructor,
Constructor,
ConstructorInstance,
ConstructorParameters,
StandardConstructor
} from './constructor';
} from './constructor.js';
export type {
Dictionary,
DictionaryKey,
DictionaryValue,
ReadonlyDictionary
} from './dictionary';
export { Disposable } from './disposable';
export { DisposableBase } from './disposable/base';
} from './dictionary.js';
export { DisposableBase } from './disposable/base.js';
export { Disposable } from './disposable/index.js';
export type {
Enumerable,
EnumerableBase,
EnumerableEntry,
EnumerableValue
} from './enumerable';
export { Event } from './event';
export { EventArgs } from './event/args';
export { CancelEventArgs } from './event/args/cancel';
export { EventHandler } from './event/handler';
} from './enumerable.js';
export { CancelEventArgs } from './event/args/cancel.js';
export { EventArgs } from './event/args/index.js';
export { EventHandler } from './event/handler.js';
export { Event } from './event/index.js';
export type {
EventCallback,
EventSubscription,
EventUnsubscription
} from './event/types';
export type { PublicMembers } from './mapping';
} from './event/types.js';
export type { PublicMembers } from './mapping.js';
export { EnumerableObject };

export const enumerableObject = new EnumerableObject();
Loading

0 comments on commit 88d8f96

Please sign in to comment.