danholibraryjs 1.5.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +2 -2
- package/README.md +12 -304
- package/Time.xlsx +0 -0
- package/dist/Classes/{Event.d.ts → Events/Event.d.ts} +9 -8
- package/dist/Classes/{Event.js → Events/Event.js} +17 -9
- package/dist/Classes/{EventCollection.d.ts → Events/EventCollection.d.ts} +10 -10
- package/dist/Classes/{EventCollection.js → Events/EventCollection.js} +28 -29
- package/dist/Classes/{EventEmitter.d.ts → Events/EventEmitter.d.ts} +33 -7
- package/dist/Classes/{EventEmitter.js → Events/EventEmitter.js} +33 -3
- package/dist/Classes/Events/index.d.ts +3 -0
- package/dist/Classes/Events/index.js +19 -0
- package/dist/Classes/Time/Date.d.ts +147 -0
- package/dist/Classes/Time/Date.js +238 -0
- package/dist/Classes/Time/Time.d.ts +65 -0
- package/dist/Classes/Time/Time.js +117 -0
- package/dist/Classes/Time/TimeProperties.d.ts +3 -0
- package/dist/{Interfaces/BaseEventInterface.js → Classes/Time/TimeProperties.js} +0 -0
- package/dist/Classes/Time/TimeSpan.d.ts +123 -0
- package/dist/Classes/Time/TimeSpan.js +179 -0
- package/dist/Classes/Time/index.d.ts +4 -0
- package/dist/Classes/Time/index.js +20 -0
- package/dist/Classes/index.d.ts +2 -3
- package/dist/Classes/index.js +7 -4
- package/dist/Classes/store.d.ts +79 -0
- package/dist/Classes/store.js +85 -0
- package/dist/Extensions/Array.d.ts +31 -0
- package/dist/Extensions/Array.js +26 -0
- package/dist/Extensions/Map.d.ts +38 -0
- package/dist/Extensions/Map.js +31 -0
- package/dist/Extensions/Object.d.ts +16 -0
- package/dist/Extensions/Object.js +8 -0
- package/dist/Extensions/String.d.ts +26 -0
- package/dist/Extensions/String.js +17 -0
- package/dist/Extensions/index.d.ts +29 -0
- package/dist/Extensions/index.js +71 -0
- package/dist/Functions/GetCSSProperty.d.ts +15 -0
- package/dist/Functions/GetCSSProperty.js +26 -0
- package/dist/Functions/HTMLEvent.d.ts +6 -0
- package/dist/Functions/HTMLEvent.js +6 -0
- package/dist/Functions/SetNavigationSelected.js +2 -0
- package/dist/Functions/index.d.ts +1 -0
- package/dist/Functions/index.js +6 -1
- package/dist/Interfaces/ElementOptions.d.ts +12 -15
- package/dist/Interfaces/IReplacement.d.ts +1 -1
- package/dist/Interfaces/index.d.ts +0 -1
- package/dist/Interfaces/index.js +5 -2
- package/dist/Types/BetterTypes.d.ts +9 -0
- package/dist/{Interfaces/IHTMLEvent.js → Types/BetterTypes.js} +0 -0
- package/dist/Types/Date.d.ts +6 -0
- package/dist/Types/{EventHandler.js → Date.js} +0 -0
- package/dist/Types/Events.d.ts +10 -0
- package/dist/Types/{IElement.js → Events.js} +0 -0
- package/dist/Types/PropertiesWith.d.ts +13 -0
- package/dist/Types/{StringRegex.js → PropertiesWith.js} +0 -0
- package/dist/Types/TransformTypes.d.ts +13 -0
- package/dist/Types/TransformTypes.js +2 -0
- package/dist/Types/index.d.ts +31 -3
- package/dist/Types/index.js +10 -4
- package/dist/index.d.ts +5 -5
- package/dist/index.js +19 -10
- package/docs/Classes.md +488 -0
- package/docs/Extensions.md +146 -0
- package/docs/Functions.md +53 -0
- package/docs/Interfaces.md +34 -0
- package/docs/Types.md +137 -0
- package/docs/index.md +29 -0
- package/package.json +22 -21
- package/src/Classes/{Event.ts → Events/Event.ts} +19 -14
- package/src/Classes/Events/EventCollection.ts +109 -0
- package/src/Classes/{EventEmitter.ts → Events/EventEmitter.ts} +37 -11
- package/src/Classes/Events/index.ts +3 -0
- package/src/Classes/Time/Date.ts +264 -0
- package/src/Classes/Time/Time.ts +134 -0
- package/src/Classes/Time/TimeProperties.ts +3 -0
- package/src/Classes/Time/TimeSpan.ts +195 -0
- package/src/Classes/Time/index.ts +4 -0
- package/src/Classes/index.ts +2 -3
- package/src/Classes/store.ts +95 -0
- package/src/Extensions/Array.ts +57 -0
- package/src/Extensions/Map.ts +73 -0
- package/src/Extensions/Object.ts +25 -0
- package/src/Extensions/String.ts +43 -0
- package/src/Extensions/index.ts +83 -0
- package/src/Functions/GetCSSProperty.ts +27 -0
- package/src/Functions/HTMLEvent.ts +6 -0
- package/src/Functions/SetNavigationSelected.ts +1 -0
- package/src/Functions/index.ts +2 -1
- package/src/Interfaces/ElementOptions.ts +18 -14
- package/src/Interfaces/IReplacement.ts +1 -1
- package/src/Interfaces/index.ts +1 -2
- package/src/Types/BetterTypes.ts +10 -0
- package/src/Types/Date.ts +7 -0
- package/src/Types/Events.ts +15 -0
- package/src/Types/PropertiesWith.ts +14 -0
- package/src/Types/TransformTypes.ts +18 -0
- package/src/Types/index.ts +36 -3
- package/src/index.ts +5 -6
- package/tsconfig.json +99 -7
- package/dist/Extensions.d.ts +0 -85
- package/dist/Extensions.js +0 -84
- package/dist/Interfaces/BaseEventInterface.d.ts +0 -4
- package/dist/Interfaces/IHTMLEvent.d.ts +0 -4
- package/dist/Types/EventHandler.d.ts +0 -7
- package/dist/Types/IElement.d.ts +0 -9
- package/dist/Types/StringRegex.d.ts +0 -2
- package/src/Classes/EventCollection.ts +0 -116
- package/src/Extensions.ts +0 -185
- package/src/Interfaces/BaseEventInterface.ts +0 -4
- package/src/Types/EventHandler.ts +0 -12
- package/src/Types/IElement.ts +0 -9
- package/src/Types/StringRegex.ts +0 -2
- package/test.js +0 -20
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# [DanhoLibraryJS](../README.md)
|
|
2
|
+
|
|
3
|
+
## Interfaces
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
/**
|
|
7
|
+
* Construction options when creating an HTML element using:
|
|
8
|
+
* @see Document.createProperElement
|
|
9
|
+
* @borrows IElement
|
|
10
|
+
* @borrows Arrayable
|
|
11
|
+
*/
|
|
12
|
+
interface ElementOptions<K extends keyof HTMLElementTagNameMap> {
|
|
13
|
+
/**css classes to give the element*/
|
|
14
|
+
classes?: Array<string>;
|
|
15
|
+
/**attributes to give the element*/
|
|
16
|
+
attributes?: Array<[string, string]>;
|
|
17
|
+
/**Children of the element*/
|
|
18
|
+
children?: Arrayable<IElement>;
|
|
19
|
+
/**Events for the element to listen to
|
|
20
|
+
* @use HTMLEvent<Event, RetrunType>(name: Event, handler: (e: Event) => ReturnType)
|
|
21
|
+
*/
|
|
22
|
+
events?: Array<{ name: string, handler: (e: Event) => any }>
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Replacement tool for
|
|
26
|
+
* @see String.toSnakeCase
|
|
27
|
+
* @see String.toKebabCase
|
|
28
|
+
* @borrows StringRegex
|
|
29
|
+
*/
|
|
30
|
+
interface IReplacement {
|
|
31
|
+
replacer?: StringRegex,
|
|
32
|
+
replacement?: string
|
|
33
|
+
}
|
|
34
|
+
```
|
package/docs/Types.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# [DanhoLibraryJS](../README.md)
|
|
2
|
+
|
|
3
|
+
## Types
|
|
4
|
+
|
|
5
|
+
### General
|
|
6
|
+
|
|
7
|
+
``` ts
|
|
8
|
+
/**
|
|
9
|
+
* Type's properties are ReturnType
|
|
10
|
+
*/
|
|
11
|
+
export type AllPropsAre<ReturnType> = {
|
|
12
|
+
[key: string]: ReturnType
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Item is single or multiple
|
|
17
|
+
*/
|
|
18
|
+
export type Arrayable<T> = T | Array<T>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Used for HTMLElement.append in ElementOptions, Document.createProperElement.
|
|
22
|
+
* IElement accepts HTML Elements or HTMl-like strings.
|
|
23
|
+
*
|
|
24
|
+
* @see HTMLElement.append
|
|
25
|
+
* @see Document.createProperElement
|
|
26
|
+
*/
|
|
27
|
+
export type IElement = HTMLElement | string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* string or RegExp.. pretty self-explanatory
|
|
31
|
+
*/
|
|
32
|
+
export type StringRegex = string | RegExp;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Return types of T
|
|
36
|
+
*/
|
|
37
|
+
export type ValueOf<T> = T[keyof T];
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### BetterTypes
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
/**
|
|
44
|
+
* Construct a type with the properties of Type except for those in type Properties.
|
|
45
|
+
*/
|
|
46
|
+
export type BetterOmit<Type, Properties extends keyof Type> = Omit<Type, Properties>;
|
|
47
|
+
/**
|
|
48
|
+
* Extract from From those types that are assignable to Properties
|
|
49
|
+
*/
|
|
50
|
+
export type BetterExtract<From, Properties extends From> = Extract<From, Properties>;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Events
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
/**
|
|
57
|
+
* Default eventhandler mapper. Object with properties that are arrays
|
|
58
|
+
*/
|
|
59
|
+
export type BaseEvent<Keys extends string, Types extends Array<any>> = Record<Keys, Types>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Eventhandler type for:
|
|
63
|
+
* @see EventCollection
|
|
64
|
+
* @borrows BaseEvent
|
|
65
|
+
*/
|
|
66
|
+
export type EventHandler<
|
|
67
|
+
Events extends BaseEvent<string, Array<any>> = BaseEvent<string, Array<any>>,
|
|
68
|
+
Event extends keyof Events = keyof Events,
|
|
69
|
+
ReturnType = any
|
|
70
|
+
> = (...args: Events[Event]) => ReturnType;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### PropertiesWith
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
/**
|
|
77
|
+
* Filters all properties from From that has the return type of Type
|
|
78
|
+
*/
|
|
79
|
+
export type PropertiesWith<Type, From> = {
|
|
80
|
+
[Key in keyof From as From[Key] extends Type ? Key : never]: From[Key]
|
|
81
|
+
}
|
|
82
|
+
export default PropertiesWith;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Fitlers all properties from From that don't have the return type of Type
|
|
86
|
+
*/
|
|
87
|
+
export type PropertiesWithout<Type, From> = {
|
|
88
|
+
[Key in keyof From as From[Key] extends Type ? never : Key]: From[Key]
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Time
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
/**
|
|
96
|
+
* Type used to construct DanhoDate.
|
|
97
|
+
* @Data Partial TimeProperties except years & months
|
|
98
|
+
* @DateFormat string as dd/MM/yyyy
|
|
99
|
+
*/
|
|
100
|
+
export type DanhoDateConstructor = Data | DateFormat | number | Date;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Object interface with keys above to number values. If Plural is true, all properties ends with 's'
|
|
104
|
+
*/
|
|
105
|
+
export type TimeProperties<Plural extends boolean = false> = Record<Plural extends true ? `${TimeKeys}s` : TimeKeys, number>
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* What properties to include when using TimeSpan.toString(format: TimeSpanFormat): string
|
|
109
|
+
*/
|
|
110
|
+
export type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean>>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### TransformTypes
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
/**
|
|
117
|
+
* Converts Start types to Switch types in From type
|
|
118
|
+
*/
|
|
119
|
+
export type TransformType<From, Start, Switch> = {
|
|
120
|
+
[Key in keyof From]: From[Key] extends Start ? Switch : From[Key]
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns object with properties matching BaseType with types of NewType
|
|
125
|
+
*/
|
|
126
|
+
export type TransformTypes<From, BaseType, NewType> = Record<keyof {
|
|
127
|
+
[Key in keyof From as From[Key] extends BaseType ? Key : never]: Key
|
|
128
|
+
}, NewType>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Store
|
|
132
|
+
```ts
|
|
133
|
+
/**
|
|
134
|
+
* Reducer function to map wanted parameters when using @see Store.on(action, reducer);
|
|
135
|
+
*/
|
|
136
|
+
export type Reducer<State, Types extends Record<string, any[]>, Action extends keyof Types> = (state: State, ...args: Types[Action]) => State
|
|
137
|
+
```
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# [DanhoLibraryJS](../README.md)
|
|
2
|
+
|
|
3
|
+
[By DanielSimonsen90 // Danhosaur](https://github.com/DanielSimonsen90)
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### Vanilla
|
|
8
|
+
|
|
9
|
+
```html
|
|
10
|
+
<script src="https://cdn.jsdelivr.net/gh/DanielSimonsen90/DanhoLibraryJS@latest/dist/index.js"></script>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Node
|
|
14
|
+
|
|
15
|
+
```cmd
|
|
16
|
+
npm install danholibraryjs
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { ... } from 'DanhoLibraryJS';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
* [Classes](./Classes.md)
|
|
26
|
+
* [Extensions](./Extensions.md)
|
|
27
|
+
* [Functions](./Functions.md)
|
|
28
|
+
* [Interfaces](./Interfaces.md)
|
|
29
|
+
* [Types](./Types.md)
|
package/package.json
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "danholibraryjs",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Library for Javascript.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "danholibraryjs",
|
|
3
|
+
"version": "1.8.0",
|
|
4
|
+
"description": "Library for Javascript.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "npx tsc --project tsconfig.json",
|
|
9
|
+
"build-watch": "npx tsc --project tsconfig.json --watch true"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/DanielSimonsen90/DanhoLibraryJS.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": ["library", "javascript"],
|
|
16
|
+
"author": "DanielSimonsen90 | Danhosaur | Daniel Simonsen",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/DanielSimonsen90/DanhoLibraryJS/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/DanielSimonsen90/DanhoLibraryJS#readme"
|
|
22
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import BaseEvent from "
|
|
2
|
-
import EventHandler from "../Types/EventHandler";
|
|
1
|
+
import { BaseEvent, EventHandler } from "../../Types/Events";
|
|
3
2
|
|
|
4
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Base event for @see EventEmitter, @borrows EventHandler @borrows BaseEvent
|
|
5
|
+
*/
|
|
5
6
|
export class Event<
|
|
6
|
-
Events extends BaseEvent,
|
|
7
|
+
Events extends BaseEvent<string, Array<any>>,
|
|
7
8
|
Name extends keyof Events = keyof Events,
|
|
8
|
-
Params = Events[Name],
|
|
9
9
|
> {
|
|
10
10
|
/**
|
|
11
11
|
* Base event for @see EventEmitter, @borrows EventHandler
|
|
@@ -15,6 +15,7 @@ export class Event<
|
|
|
15
15
|
constructor(name: Name, ...listeners: Array<EventHandler<Events, Name>>) {
|
|
16
16
|
this.name = name;
|
|
17
17
|
this._listeners = listeners;
|
|
18
|
+
this._lastEmitted = new Date();
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
/**Name of event*/
|
|
@@ -35,17 +36,17 @@ export class Event<
|
|
|
35
36
|
/**@private Internal runs*/
|
|
36
37
|
private _runs = 0;
|
|
37
38
|
/**@private Internal lastEmitted*/
|
|
38
|
-
private _lastEmitted: Date
|
|
39
|
+
private _lastEmitted: Date;
|
|
39
40
|
|
|
40
41
|
/**
|
|
41
42
|
* Emits event and returns array of responses
|
|
42
|
-
* @param
|
|
43
|
+
* @param args Arguments required for event listeners
|
|
43
44
|
* @returns Return values of listeners' returns
|
|
44
45
|
*/
|
|
45
|
-
public emit(
|
|
46
|
+
public emit(...args: Events[Name]) {
|
|
46
47
|
this._runs++;
|
|
47
48
|
this._lastEmitted = new Date();
|
|
48
|
-
return this._listeners.map(listener => (listener as any)(
|
|
49
|
+
return this._listeners.map(listener => (listener as any)(...args))
|
|
49
50
|
}
|
|
50
51
|
/**
|
|
51
52
|
* Adds listener to listeners array and returns self with new listener added
|
|
@@ -74,8 +75,8 @@ export class Event<
|
|
|
74
75
|
* @throws Limit error, if limit was reached
|
|
75
76
|
*/
|
|
76
77
|
public once(listener: EventHandler<Events, Name>, prepend = false) {
|
|
77
|
-
const handler = (params:
|
|
78
|
-
const result = (listener as any)(params);
|
|
78
|
+
const handler = (...params: Events[Name]) => {
|
|
79
|
+
const result = (listener as any)(...params);
|
|
79
80
|
this.off(handler as any);
|
|
80
81
|
return result;
|
|
81
82
|
}
|
|
@@ -94,14 +95,18 @@ export class Event<
|
|
|
94
95
|
|
|
95
96
|
/**
|
|
96
97
|
* Removes listener from internal listeners array
|
|
97
|
-
* @param listener Listener to remove
|
|
98
|
+
* @param listener Listener to remove. If none specified, all will be removed
|
|
98
99
|
* @param throwNotFoundError Throw error if listener isn't in listeners array - default: false
|
|
99
100
|
* @returns this, without listener
|
|
100
101
|
*
|
|
101
102
|
* @throws NotFound, if throwNotFoundError is true, and internal listeners array doesn't include listener provided
|
|
102
103
|
*/
|
|
103
|
-
public off(listener
|
|
104
|
-
try {
|
|
104
|
+
public off(listener?: EventHandler<Events, Name>, throwNotFoundError = false) {
|
|
105
|
+
try {
|
|
106
|
+
if (!listener) this._listeners = new Array<EventHandler<Events, Name>>()
|
|
107
|
+
else if (!this._listeners.includes(listener)) return this;
|
|
108
|
+
else this._listeners.remove(listener);
|
|
109
|
+
}
|
|
105
110
|
catch (err) { if (throwNotFoundError) throw err; }
|
|
106
111
|
return this;
|
|
107
112
|
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { BaseEvent, EventHandler } from "../../Types/Events";
|
|
2
|
+
import Event from './Event';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Collection of Events from @see EventEmitter
|
|
6
|
+
* @borrows EventHandler
|
|
7
|
+
* @borrows Event
|
|
8
|
+
* @borrows BaseEvent
|
|
9
|
+
*/
|
|
10
|
+
export class EventCollection<Events extends BaseEvent<string, Array<any>>> {
|
|
11
|
+
constructor(events?: Map<keyof Events, EventHandler<Events>>) {
|
|
12
|
+
this._events = !events ?
|
|
13
|
+
new Map<keyof Events, Event<Events>>() :
|
|
14
|
+
events.array().reduce((result, [event, handlers]) =>
|
|
15
|
+
result.set(event, new Event<Events>(event, handlers)),
|
|
16
|
+
new Map<keyof Events, Event<Events>>());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**Amount of events stored*/
|
|
20
|
+
public get size() {
|
|
21
|
+
return this._events.size;
|
|
22
|
+
}
|
|
23
|
+
/**@private Internal event collection*/
|
|
24
|
+
private _events = new Map<keyof Events, Event<Events>>();
|
|
25
|
+
/**@private limit of events*/
|
|
26
|
+
private _limit = 0;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns true if event is in collection
|
|
30
|
+
* @param event Event name
|
|
31
|
+
* @returns true if event is in collection
|
|
32
|
+
*/
|
|
33
|
+
public has<EventName extends keyof Events>(event: EventName): boolean {
|
|
34
|
+
return this._events.has(event);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Returns event matching event parameter
|
|
38
|
+
* @param event Event name
|
|
39
|
+
* @returns Event
|
|
40
|
+
*/
|
|
41
|
+
public get<EventName extends keyof Events>(event: EventName): Event<Events, EventName> {
|
|
42
|
+
return this._events.get(event) as any;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Adds handler to event collection with name as key
|
|
46
|
+
* @param name Event name
|
|
47
|
+
* @param handler Handler for event
|
|
48
|
+
* @returns this
|
|
49
|
+
*/
|
|
50
|
+
public add<EventName extends keyof Events>(name: EventName, handler: EventHandler<Events, keyof Events>, once = false): this {
|
|
51
|
+
if (this._limit > 0 && this._limit + 1 > this._events.size) {
|
|
52
|
+
throw new Error(`Listener limit, ${this._limit}, reached!`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const event = (this.has(name) && this.get(name)) || new Event(name);
|
|
56
|
+
this._events.set(name, event.on(handler, once) as any);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @summary clear(): Clears all events
|
|
61
|
+
* @summary clear("all", myEventHandler): Removes myEventHandler from all events that have it
|
|
62
|
+
* @summary clear("myEvent"): Clears all handlers tied to "myEvent"
|
|
63
|
+
* @summary clear("myEvent", myEventHandler): Removes myEventHandler from "myEvent"
|
|
64
|
+
*
|
|
65
|
+
* @param name Event name | "all"
|
|
66
|
+
* @param handler Specific handler to remove. If left blank, all handlers in name will be removed
|
|
67
|
+
* @returns this
|
|
68
|
+
*/
|
|
69
|
+
public clear<EventName extends keyof Events>(name: EventName | 'all' = 'all', handler?: EventHandler): this {
|
|
70
|
+
const _name = name as string;
|
|
71
|
+
|
|
72
|
+
if (_name.toLowerCase() == 'all' && handler == null) this._events.clear(); //clear(): Clears all events
|
|
73
|
+
else if (_name.toLowerCase() == 'all' && handler) this._events = (() => { //clear("all", myEventHandler): Removes the "myEventHandler" handler from all events
|
|
74
|
+
this._events.forEach(e => e.off(handler))
|
|
75
|
+
return this._events;
|
|
76
|
+
})();
|
|
77
|
+
else if (_name.toLowerCase() != "all" && handler == null) this._events.delete(name); //clear("myEvent"): Clears All handlers tied to "myEvent"
|
|
78
|
+
else if (_name.toLowerCase() != 'all' && handler) this._events.set(name, this._events.get(name)!.off(handler)); //clear("myEvent", myEventHandler): Removes the "myEventsHandler" handler from "myEvent"
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public emit<Event extends keyof Events>(name: Event, ...args: Events[Event]) {
|
|
83
|
+
return this.get(name)?.emit(...args);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Limits how many events to accept using EventEmitter#on or EventEmitter#once
|
|
88
|
+
* @param limit Limit of events to keep
|
|
89
|
+
* @returns this with the new limit
|
|
90
|
+
*
|
|
91
|
+
* @throws Unknown event, if event name isn't recognized
|
|
92
|
+
*/
|
|
93
|
+
public limit<Event extends keyof Events>(eventName: 'all' | Event, limit: number) {
|
|
94
|
+
if (limit <= 0) return;
|
|
95
|
+
|
|
96
|
+
if (eventName == 'all') {
|
|
97
|
+
this._limit = limit;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const event = this.get(eventName);
|
|
102
|
+
if (!event) throw new Error(`Unknown event, ${eventName}!`);
|
|
103
|
+
|
|
104
|
+
event.limit = limit;
|
|
105
|
+
this._events.set(eventName, event as any);
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export default EventCollection;
|
|
@@ -1,13 +1,39 @@
|
|
|
1
1
|
import EventCollection from "./EventCollection";
|
|
2
|
-
import BaseEvent from '
|
|
3
|
-
import EventHandler from '../Types/EventHandler';
|
|
2
|
+
import { BaseEvent, EventHandler } from '../../Types';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Traditional Node.js EventEmitter for vanilla JavaScript
|
|
6
|
+
* @borrows EventCollection
|
|
7
|
+
* @borrows BaseEvent
|
|
8
|
+
* @borrows EventHandler
|
|
9
|
+
*
|
|
10
|
+
* @example ```ts
|
|
11
|
+
* import { EventEmitter } from 'danholibraryjs';
|
|
12
|
+
*
|
|
13
|
+
* type EventTypes = {
|
|
14
|
+
* create: [username: string, password: string],
|
|
15
|
+
* update: [id: string, user: User],
|
|
16
|
+
* delete: [id: string, reason?: string],
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* const emitter = new EventEmitter<EventTypes>(new Map([
|
|
20
|
+
* ['create', (username, password) => {
|
|
21
|
+
* return { username, password };
|
|
22
|
+
* }],
|
|
23
|
+
* ['update', (id, user) => {
|
|
24
|
+
* return { id, ...user };
|
|
25
|
+
* }]
|
|
26
|
+
* ]));
|
|
27
|
+
*
|
|
28
|
+
* const onDelete = (id: string, reason?: string) => console.log(`User ${id} was deleted because ${reason}`);
|
|
29
|
+
* emitter.on('delete', onDelete);
|
|
30
|
+
* emitter.emit('delete', '1', 'No longer needed');
|
|
31
|
+
* emitter.off('delete', onDelete);
|
|
32
|
+
* ```
|
|
7
33
|
*/
|
|
8
|
-
export class EventEmitter<Events extends BaseEvent
|
|
34
|
+
export class EventEmitter<Events extends BaseEvent<string, Array<any>>> {
|
|
9
35
|
/**@param events Map<name: string, handlers: EventHandler[]>*/
|
|
10
|
-
constructor(events?: Map<keyof Events, EventHandler<Events
|
|
36
|
+
constructor(events?: Map<keyof Events, EventHandler<Events>>) {
|
|
11
37
|
this._events = new EventCollection(events);
|
|
12
38
|
}
|
|
13
39
|
|
|
@@ -21,7 +47,7 @@ export class EventEmitter<Events extends BaseEvent> {
|
|
|
21
47
|
* @returns this
|
|
22
48
|
*/
|
|
23
49
|
public on<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>): this {
|
|
24
|
-
this._events.add(event, listener);
|
|
50
|
+
this._events.add(event, listener as any);
|
|
25
51
|
return this;
|
|
26
52
|
}
|
|
27
53
|
/**
|
|
@@ -30,8 +56,8 @@ export class EventEmitter<Events extends BaseEvent> {
|
|
|
30
56
|
* @param listener Callback function to run, when event occurs
|
|
31
57
|
* @returns this
|
|
32
58
|
*/
|
|
33
|
-
public once<Return extends any, Event extends keyof Events>(event:
|
|
34
|
-
this._events.add(event, listener, true);
|
|
59
|
+
public once<Return extends any, Event extends keyof Events>(event: Event, listener: EventHandler<Events, Event, Return>): this {
|
|
60
|
+
this._events.add(event, listener as any, true);
|
|
35
61
|
return this;
|
|
36
62
|
}
|
|
37
63
|
|
|
@@ -41,8 +67,8 @@ export class EventEmitter<Events extends BaseEvent> {
|
|
|
41
67
|
* @param listener If left null, removes all listeners tied to event, else only removes listener from event
|
|
42
68
|
* @returns this
|
|
43
69
|
*/
|
|
44
|
-
public off<Event extends keyof Events>(event: Event |
|
|
45
|
-
this._events.clear(event
|
|
70
|
+
public off<Return extends any, Event extends keyof Events>(event: Event | 'all' = "all", listener?: EventHandler<Events, Event, Return>): this {
|
|
71
|
+
this._events.clear(event, listener);
|
|
46
72
|
return this;
|
|
47
73
|
}
|
|
48
74
|
|
|
@@ -53,8 +79,8 @@ export class EventEmitter<Events extends BaseEvent> {
|
|
|
53
79
|
* @fires event
|
|
54
80
|
* @returns Array of listeners' reponses
|
|
55
81
|
*/
|
|
56
|
-
public emit<
|
|
57
|
-
return this._events.emit(event
|
|
82
|
+
public emit<Return extends any, Event extends keyof Events>(event: Event, ...args: Events[Event]): Array<Return> {
|
|
83
|
+
return this._events.emit(event, ...args) as Array<Return>;
|
|
58
84
|
}
|
|
59
85
|
|
|
60
86
|
/**
|