@byloth/core 1.3.0 → 1.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byloth/core",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
5
5
  "keywords": [
6
6
  "Core",
@@ -57,10 +57,10 @@
57
57
  },
58
58
  "devDependencies": {
59
59
  "@byloth/eslint-config-typescript": "^2.7.0",
60
- "@typescript-eslint/eslint-plugin": "^7.0.2",
61
- "@typescript-eslint/parser": "^7.0.2",
60
+ "@typescript-eslint/eslint-plugin": "^7.3.1",
61
+ "@typescript-eslint/parser": "^7.3.1",
62
62
  "eslint": "^8.57.0",
63
- "typescript": "^5.3.3",
64
- "vite": "^5.1.4"
63
+ "typescript": "^5.4.2",
64
+ "vite": "^5.1.6"
65
65
  }
66
66
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const VERSION = "1.3.0";
1
+ export const VERSION = "1.3.2";
2
2
 
3
3
  export {
4
4
  AggregatedIterator,
@@ -8,6 +8,7 @@ export {
8
8
  JsonStorage,
9
9
  ReducedIterator,
10
10
  SmartIterator,
11
+ SmartPromise,
11
12
  Subscribers
12
13
 
13
14
  } from "./models/index.js";
@@ -178,6 +178,42 @@ export default class AggregatedIterator<T, K extends PropertyKey>
178
178
  }
179
179
  });
180
180
  }
181
+ public first(): ReducedIterator<T, K>
182
+ {
183
+ const firsts = new Map<K, T>();
184
+
185
+ for (const [key, element] of this._elements)
186
+ {
187
+ if (firsts.has(key)) { continue; }
188
+
189
+ firsts.set(key, element);
190
+ }
191
+
192
+ return new ReducedIterator(function* ()
193
+ {
194
+ for (const [key, element] of firsts)
195
+ {
196
+ yield [key, element];
197
+ }
198
+ });
199
+ }
200
+ public last(): ReducedIterator<T, K>
201
+ {
202
+ const lasts = new Map<K, T>();
203
+
204
+ for (const [key, element] of this._elements)
205
+ {
206
+ lasts.set(key, element);
207
+ }
208
+
209
+ return new ReducedIterator(function* ()
210
+ {
211
+ for (const [key, element] of lasts)
212
+ {
213
+ yield [key, element];
214
+ }
215
+ });
216
+ }
181
217
 
182
218
  public toArray(): T[][]
183
219
  {
@@ -2,14 +2,13 @@ import Exception from "./exception.js";
2
2
  import SmartIterator from "./smart-iterator.js";
3
3
  import Aggregator, { AggregatedIterator, ReducedIterator } from "./aggregators/index.js";
4
4
 
5
- import DeferredPromise from "./deferred-promise.js";
6
5
  import JsonStorage from "./json-storage.js";
7
6
  import Subscribers from "./subscribers.js";
8
7
 
8
+ export { DeferredPromise, SmartPromise } from "./promises/index.js";
9
9
  export {
10
10
  AggregatedIterator,
11
11
  Aggregator,
12
- DeferredPromise,
13
12
  Exception,
14
13
  JsonStorage,
15
14
  ReducedIterator,
@@ -0,0 +1,117 @@
1
+ import type {
2
+ PromiseResolver,
3
+ PromiseRejecter,
4
+ FulfilledHandler,
5
+ RejectedHandler,
6
+ PromiseExecutor
7
+
8
+ } from "../../types.js";
9
+
10
+ export default class DeferredPromise<T = void, E = unknown, F = T, R = never>
11
+ {
12
+ protected _isPending: boolean;
13
+ protected _isFulfilled: boolean;
14
+ protected _isRejected: boolean;
15
+
16
+ protected _resolve!: PromiseResolver<T>;
17
+ protected _reject!: PromiseRejecter<E>;
18
+
19
+ protected _promise: Promise<F | R>;
20
+
21
+ public constructor(onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<E, R> | null)
22
+ {
23
+ this._isPending = true;
24
+ this._isFulfilled = false;
25
+ this._isRejected = false;
26
+
27
+ let _resolve: PromiseResolver<T>;
28
+ let _reject: PromiseRejecter<E>;
29
+
30
+ const executor: PromiseExecutor<T, E> = (resolve, reject) =>
31
+ {
32
+ _resolve = resolve;
33
+ _reject = reject;
34
+ };
35
+
36
+ let _onFulfilled: FulfilledHandler<T, F>;
37
+ if (onFulfilled)
38
+ {
39
+ _onFulfilled = (value) =>
40
+ {
41
+ this._isPending = false;
42
+ this._isFulfilled = true;
43
+
44
+ return onFulfilled!(value);
45
+ };
46
+ }
47
+ else
48
+ {
49
+ _onFulfilled = (value) =>
50
+ {
51
+ this._isPending = false;
52
+ this._isFulfilled = true;
53
+
54
+ return (value as unknown) as F;
55
+ };
56
+ }
57
+
58
+ let _onRejected: RejectedHandler<E, R>;
59
+ if (onRejected)
60
+ {
61
+ _onRejected = (reason) =>
62
+ {
63
+ this._isPending = false;
64
+ this._isRejected = true;
65
+
66
+ return onRejected!(reason);
67
+ };
68
+ }
69
+ else
70
+ {
71
+ _onRejected = (reason) =>
72
+ {
73
+ this._isPending = false;
74
+ this._isRejected = true;
75
+
76
+ return (reason as unknown) as R;
77
+ };
78
+ }
79
+
80
+ this._promise = new Promise<T>(executor)
81
+ .then(_onFulfilled, _onRejected);
82
+
83
+ this._resolve = _resolve!;
84
+ this._reject = _reject!;
85
+ }
86
+
87
+ public get isPending(): boolean { return this._isPending; }
88
+ public get isFulfilled(): boolean { return this._isFulfilled; }
89
+ public get isRejected(): boolean { return this._isRejected; }
90
+
91
+ public get resolve(): PromiseResolver<T> { return this._resolve; }
92
+ public get reject(): PromiseRejecter<E> { return this._reject; }
93
+
94
+ public then<N = F | R, H = R>(
95
+ onFulfilled?: FulfilledHandler<F | R, N> | null,
96
+ onRejected?: RejectedHandler<R, H> | null): Promise<N | H>
97
+ {
98
+ return this._promise.then(onFulfilled, onRejected);
99
+ }
100
+ public catch<H = R>(onRejected?: RejectedHandler<R, H> | null): Promise<F | R | H>
101
+ {
102
+ return this._promise.catch(onRejected);
103
+ }
104
+ public finally(onFinally?: (() => void) | null): Promise<F | R>
105
+ {
106
+ return this._promise.finally(onFinally);
107
+ }
108
+
109
+ public watch(promise: Promise<T>): this
110
+ {
111
+ promise.then(this.resolve, this.reject);
112
+
113
+ return this;
114
+ }
115
+
116
+ public get [Symbol.toStringTag]() { return "DeferredPromise"; }
117
+ }
@@ -0,0 +1,4 @@
1
+ import DeferredPromise from "./deferred-promise.js";
2
+ import SmartPromise from "./smart-promise.js";
3
+
4
+ export { DeferredPromise, SmartPromise };
@@ -0,0 +1,56 @@
1
+ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "../../types.js";
2
+
3
+ export default class SmartPromise<T = void, E = unknown>
4
+ {
5
+ protected _isPending: boolean;
6
+ protected _isFulfilled: boolean;
7
+ protected _isRejected: boolean;
8
+
9
+ protected _promise: Promise<T | E>;
10
+
11
+ public constructor(executor: PromiseExecutor<T, E>)
12
+ {
13
+ this._isPending = true;
14
+ this._isFulfilled = false;
15
+ this._isRejected = false;
16
+
17
+ const _resolve = (result: T): T =>
18
+ {
19
+ this._isPending = false;
20
+ this._isFulfilled = true;
21
+
22
+ return result;
23
+ };
24
+ const _reject = (reason: E): E =>
25
+ {
26
+ this._isPending = false;
27
+ this._isRejected = true;
28
+
29
+ return reason;
30
+ };
31
+
32
+ this._promise = new Promise<T>(executor)
33
+ .then(_resolve, _reject);
34
+ }
35
+
36
+ public get isPending(): boolean { return this._isPending; }
37
+ public get isFulfilled(): boolean { return this._isFulfilled; }
38
+ public get isRejected(): boolean { return this._isRejected; }
39
+
40
+ public then<F = T, R = E>(
41
+ onFulfilled?: FulfilledHandler<T | E, F> | null,
42
+ onRejected?: RejectedHandler<E, R> | null): Promise<F | R>
43
+ {
44
+ return this._promise.then(onFulfilled, onRejected);
45
+ }
46
+ public catch<R = E>(onRejected?: RejectedHandler<E, R> | null): Promise<T | E | R>
47
+ {
48
+ return this._promise.catch(onRejected);
49
+ }
50
+ public finally(onFinally?: (() => void) | null): Promise<T | E>
51
+ {
52
+ return this._promise.finally(onFinally);
53
+ }
54
+
55
+ public get [Symbol.toStringTag]() { return "SmartPromise"; }
56
+ }
package/src/types.ts CHANGED
@@ -10,7 +10,7 @@ export type MaybePromise<T> = T | PromiseLike<T>;
10
10
  export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
11
11
  export type RejectedHandler<E = unknown, R = never> = (reason: E) => MaybePromise<R>;
12
12
 
13
- export type PromiseResolver<T = void> = (result?: MaybePromise<T>) => void;
13
+ export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
14
14
  export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
15
15
  export type PromiseExecutor<T = void, E = unknown> = (resolve: PromiseResolver<T>, reject: PromiseRejecter<E>) => void;
16
16
 
@@ -1,61 +0,0 @@
1
- import type { PromiseResolver, PromiseRejecter, FulfilledHandler, RejectedHandler } from "../types.js";
2
-
3
- export default class DeferredPromise<T = void, E = unknown, F = T, R = never>
4
- {
5
- protected _resolve!: PromiseResolver<T>;
6
- protected _reject!: PromiseRejecter<E>;
7
-
8
- protected _promise: Promise<F | R>;
9
-
10
- public constructor(onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<E, R> | null)
11
- {
12
- let _resolve: PromiseResolver<T>;
13
- let _reject: PromiseRejecter<E>;
14
-
15
- this._promise = new Promise<T>((resolve, reject) =>
16
- {
17
- _resolve = resolve as PromiseResolver<T>;
18
- _reject = reject as PromiseRejecter<E>;
19
-
20
- }).then(onFulfilled, onRejected);
21
-
22
- this._resolve = _resolve!;
23
- this._reject = _reject!;
24
- }
25
-
26
- public get resolve(): PromiseResolver<T>
27
- {
28
- return this._resolve;
29
- }
30
- public get reject(): PromiseRejecter<E>
31
- {
32
- return this._reject;
33
- }
34
-
35
- public then<N = F | R, H = R>(
36
- onFulfilled?: FulfilledHandler<F | R, N> | null,
37
- onRejected?: RejectedHandler<R, H> | null): Promise<N | H>
38
- {
39
- return this._promise.then(onFulfilled, onRejected);
40
- }
41
- public catch<H = R>(onRejected?: RejectedHandler<R, H> | null): Promise<F | R | H>
42
- {
43
- return this._promise.catch(onRejected);
44
- }
45
- public finally(onFinally?: (() => void) | null): Promise<F | R>
46
- {
47
- return this._promise.finally(onFinally);
48
- }
49
-
50
- public watch(promise: Promise<T>): this
51
- {
52
- promise.then(this.resolve, this.reject);
53
-
54
- return this;
55
- }
56
-
57
- public get [Symbol.toStringTag]()
58
- {
59
- return "DeferredPromise";
60
- }
61
- }