@byloth/core 2.0.1 → 2.1.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/README.md +1 -0
- package/dist/core.cjs +6 -0
- package/dist/core.cjs.map +1 -0
- package/dist/{core.js → core.esm.js} +1098 -779
- package/dist/core.esm.js.map +1 -0
- package/dist/core.global.js +6 -0
- package/dist/core.global.js.map +1 -0
- package/dist/core.umd.cjs +3 -3
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +15 -11
- package/src/core/types.ts +43 -10
- package/src/index.ts +9 -2
- package/src/models/aggregators/aggregated-async-iterator.ts +5 -0
- package/src/models/aggregators/aggregated-iterator.ts +5 -0
- package/src/models/aggregators/reduced-iterator.ts +18 -5
- package/src/models/aggregators/types.ts +35 -0
- package/src/models/callbacks/callable-object.ts +7 -0
- package/src/models/callbacks/publisher.ts +16 -12
- package/src/models/callbacks/switchable-callback.ts +9 -1
- package/src/models/callbacks/types.ts +35 -0
- package/src/models/collections/index.ts +4 -0
- package/src/models/collections/map-view.ts +206 -0
- package/src/models/collections/set-view.ts +204 -0
- package/src/models/collections/types.ts +25 -0
- package/src/models/exceptions/core.ts +10 -1
- package/src/models/exceptions/index.ts +40 -1
- package/src/models/index.ts +1 -0
- package/src/models/iterators/smart-async-iterator.ts +5 -0
- package/src/models/iterators/smart-iterator.ts +5 -0
- package/src/models/iterators/types.ts +79 -1
- package/src/models/json/json-storage.ts +4 -1
- package/src/models/json/types.ts +1 -1
- package/src/models/promises/deferred-promise.ts +5 -0
- package/src/models/promises/smart-promise.ts +5 -0
- package/src/models/promises/timed-promise.ts +5 -0
- package/src/models/promises/types.ts +30 -0
- package/src/models/timers/clock.ts +3 -0
- package/src/models/timers/countdown.ts +5 -0
- package/src/models/timers/game-loop.ts +3 -0
- package/src/models/types.ts +3 -8
- package/src/utils/async.ts +15 -0
- package/src/utils/curve.ts +1 -1
- package/src/utils/date.ts +36 -6
- package/src/utils/dom.ts +5 -0
- package/src/utils/iterator.ts +43 -3
- package/src/utils/math.ts +15 -0
- package/src/utils/random.ts +4 -0
- package/src/utils/string.ts +5 -0
- package/dist/core.js.map +0 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import Publisher from "../callbacks/publisher.js";
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
+
import type SetView from "./set-view.js";
|
|
5
|
+
import type { MapViewEventsMap } from "./types.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A wrapper class around the native {@link Map} class that provides additional functionality
|
|
9
|
+
* for publishing events when entries are added, removed or the collection is cleared.
|
|
10
|
+
* There's also a complementary class that works with the native `Set` class.
|
|
11
|
+
* See also {@link SetView}.
|
|
12
|
+
*
|
|
13
|
+
* ---
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const map = new MapView<string, number>();
|
|
18
|
+
*
|
|
19
|
+
* map.subscribe("entry:add", (key: string, value: number) => console.log(`Added ${key}: ${value}`));
|
|
20
|
+
* map.set("answer", 42); // Added answer: 42
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ---
|
|
24
|
+
*
|
|
25
|
+
* @template K The type of the keys in the map.
|
|
26
|
+
* @template V The type of the values in the map.
|
|
27
|
+
*/
|
|
28
|
+
export default class MapView<K, V> extends Map<K, V>
|
|
29
|
+
{
|
|
30
|
+
/**
|
|
31
|
+
* The internal {@link Publisher} instance used to publish events.
|
|
32
|
+
*/
|
|
33
|
+
protected readonly _publisher: Publisher<MapViewEventsMap<K, V>>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Initializes a new instance of the {@link MapView} class.
|
|
37
|
+
*
|
|
38
|
+
* ---
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const map = new MapView<string, number>([["key1", 2], ["key2", 4], ["key3", 8]]);
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* ---
|
|
46
|
+
*
|
|
47
|
+
* @param iterable An optional iterable of key-value pairs to initialize the {@link Map} with.
|
|
48
|
+
*/
|
|
49
|
+
public constructor(iterable?: Iterable<[K, V]> | null)
|
|
50
|
+
{
|
|
51
|
+
super();
|
|
52
|
+
|
|
53
|
+
this._publisher = new Publisher();
|
|
54
|
+
|
|
55
|
+
if (iterable)
|
|
56
|
+
{
|
|
57
|
+
for (const [key, value] of iterable) { this.set(key, value); }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Adds a new entry with a specified key and value to the {@link Map}.
|
|
63
|
+
* If an entry with the same key already exists, the entry will be overwritten with the new value.
|
|
64
|
+
*
|
|
65
|
+
* ---
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const map = new MapView<string, number>();
|
|
70
|
+
* map.set("key1", 2)
|
|
71
|
+
* .set("key2", 4)
|
|
72
|
+
* .set("key3", 8);
|
|
73
|
+
*
|
|
74
|
+
* console.log(map); // MapView { "key1" => 2, "key2" => 4, "key3" => 8 }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* ---
|
|
78
|
+
*
|
|
79
|
+
* @param key The key of the entry to add.
|
|
80
|
+
* @param value The value of the entry to add.
|
|
81
|
+
*
|
|
82
|
+
* @returns The current instance of the {@link MapView} class.
|
|
83
|
+
*/
|
|
84
|
+
public override set(key: K, value: V): this
|
|
85
|
+
{
|
|
86
|
+
super.set(key, value);
|
|
87
|
+
|
|
88
|
+
this._publisher.publish("entry:add", key, value);
|
|
89
|
+
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Removes an entry with a specified key from the {@link Map}.
|
|
95
|
+
*
|
|
96
|
+
* ---
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const map = new MapView<string, number>([["key1", 2], ["key2", 4], ["key3", 8]]);
|
|
101
|
+
* map.delete("key2"); // true
|
|
102
|
+
* map.delete("key4"); // false
|
|
103
|
+
*
|
|
104
|
+
* console.log(map); // MapView { "key1" => 2, "key3" => 8 }
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* ---
|
|
108
|
+
*
|
|
109
|
+
* @param key The key of the entry to remove.
|
|
110
|
+
*
|
|
111
|
+
* @returns `true` if the entry existed and has been removed; otherwise `false` if the entry doesn't exist.
|
|
112
|
+
*/
|
|
113
|
+
public override delete(key: K): boolean
|
|
114
|
+
{
|
|
115
|
+
const result = super.delete(key);
|
|
116
|
+
if (result) { this._publisher.publish("entry:remove", key); }
|
|
117
|
+
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Removes all entries from the {@link Map}.
|
|
123
|
+
*
|
|
124
|
+
* ---
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const map = new MapView<string, number>([["key1", 2], ["key2", 4], ["key3", 8]]);
|
|
129
|
+
* map.clear();
|
|
130
|
+
*
|
|
131
|
+
* console.log(map); // MapView { }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
public override clear(): void
|
|
135
|
+
{
|
|
136
|
+
const size = this.size;
|
|
137
|
+
|
|
138
|
+
super.clear();
|
|
139
|
+
if (size > 0) { this._publisher.publish("collection:clear"); }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Subscribes to an event and adds a callback to be executed when the event is published.
|
|
144
|
+
*
|
|
145
|
+
* ---
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* const map = new MapView<string, number>();
|
|
150
|
+
* const unsubscribe = map.subscribe("entry:add", (key: string, value: number) =>
|
|
151
|
+
* {
|
|
152
|
+
* if (key === "answer") { unsubscribe(); }
|
|
153
|
+
* console.log(`Added ${key}: ${value}`);
|
|
154
|
+
* });
|
|
155
|
+
*
|
|
156
|
+
* map.set("key1", 2); // Added key1: 2
|
|
157
|
+
* map.set("answer", 42); // Added answer: 42
|
|
158
|
+
* map.set("key2", 4);
|
|
159
|
+
* map.set("key3", 8);
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* ---
|
|
163
|
+
*
|
|
164
|
+
* @template T The key of the map containing the callback signature to subscribe.
|
|
165
|
+
*
|
|
166
|
+
* @param event The name of the event to subscribe to.
|
|
167
|
+
* @param callback The callback to execute when the event is published.
|
|
168
|
+
*
|
|
169
|
+
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
170
|
+
*/
|
|
171
|
+
public subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): () => void
|
|
172
|
+
{
|
|
173
|
+
return this._publisher.subscribe(event, callback);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Unsubscribes from an event and removes a callback from being executed when the event is published.
|
|
178
|
+
*
|
|
179
|
+
* ---
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* const callback = (key: string, value: number) => console.log(`Added ${key}: ${value}`);
|
|
184
|
+
* const map = new MapView<string, number>();
|
|
185
|
+
*
|
|
186
|
+
* map.subscribe("entry:add", callback);
|
|
187
|
+
* map.set("key1", 2); // Added key1: 2
|
|
188
|
+
*
|
|
189
|
+
* map.unsubscribe("entry:add", callback);
|
|
190
|
+
* map.set("key2", 4);
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* ---
|
|
194
|
+
*
|
|
195
|
+
* @template T The key of the map containing the callback signature to unsubscribe.
|
|
196
|
+
*
|
|
197
|
+
* @param event The name of the event to unsubscribe from.
|
|
198
|
+
* @param callback The callback to remove from the event.
|
|
199
|
+
*/
|
|
200
|
+
public unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): void
|
|
201
|
+
{
|
|
202
|
+
this._publisher.unsubscribe(event, callback);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public override readonly [Symbol.toStringTag]: string = "MapView";
|
|
206
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import Publisher from "../callbacks/publisher.js";
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
+
import type MapView from "./map-view.js";
|
|
5
|
+
import type { SetViewEventsMap } from "./types.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A wrapper class around the native {@link Set} class that provides additional functionality
|
|
9
|
+
* for publishing events when entries are added, removed or the collection is cleared.
|
|
10
|
+
* There's also a complementary class that works with the native `Map` class.
|
|
11
|
+
* See also {@link MapView}.
|
|
12
|
+
*
|
|
13
|
+
* ---
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const set = new SetView<number>();
|
|
18
|
+
*
|
|
19
|
+
* set.subscribe("entry:add", (value: number) => console.log(`Added ${value}`));
|
|
20
|
+
* set.add(42); // Added 42
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ---
|
|
24
|
+
*
|
|
25
|
+
* @template T The type of the values in the set.
|
|
26
|
+
*/
|
|
27
|
+
export default class SetView<T> extends Set<T>
|
|
28
|
+
{
|
|
29
|
+
/**
|
|
30
|
+
* The internal {@link Publisher} instance used to publish events.
|
|
31
|
+
*/
|
|
32
|
+
protected readonly _publisher: Publisher<SetViewEventsMap<T>>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Initializes a new instance of the {@link SetView} class.
|
|
36
|
+
*
|
|
37
|
+
* ---
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const set = new SetView<number>([2, 4, 8]);
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* ---
|
|
45
|
+
*
|
|
46
|
+
* @param iterable An optional iterable of values to initialize the {@link Set} with.
|
|
47
|
+
*/
|
|
48
|
+
public constructor(iterable?: Iterable<T> | null)
|
|
49
|
+
{
|
|
50
|
+
super();
|
|
51
|
+
|
|
52
|
+
this._publisher = new Publisher();
|
|
53
|
+
|
|
54
|
+
if (iterable)
|
|
55
|
+
{
|
|
56
|
+
for (const value of iterable) { this.add(value); }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Appends a new element with a specified value to the end of the {@link Set}.
|
|
62
|
+
* If the value already exists, it will not be added again.
|
|
63
|
+
*
|
|
64
|
+
* ---
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const set = new SetView<number>();
|
|
69
|
+
* set.add(2)
|
|
70
|
+
* .add(4)
|
|
71
|
+
* .add(8);
|
|
72
|
+
*
|
|
73
|
+
* console.log(set); // SetView(4) { 2, 4, 8 }
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* ---
|
|
77
|
+
*
|
|
78
|
+
* @param value The value to add.
|
|
79
|
+
*
|
|
80
|
+
* @returns The current instance of the {@link SetView} class.
|
|
81
|
+
*/
|
|
82
|
+
public override add(value: T): this
|
|
83
|
+
{
|
|
84
|
+
super.add(value);
|
|
85
|
+
|
|
86
|
+
this._publisher.publish("entry:add", value);
|
|
87
|
+
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Removes the specified value from the {@link Set}.
|
|
93
|
+
*
|
|
94
|
+
* ---
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const set = new SetView<number>([2, 4, 8]);
|
|
99
|
+
* set.delete(4); // true
|
|
100
|
+
* set.delete(16); // false
|
|
101
|
+
*
|
|
102
|
+
* console.log(set); // SetView(2) { 2, 8 }
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* ---
|
|
106
|
+
*
|
|
107
|
+
* @param value The value to remove.
|
|
108
|
+
*
|
|
109
|
+
* @returns `true` if the entry existed and has been removed; otherwise `false` if the entry doesn't exist.
|
|
110
|
+
*/
|
|
111
|
+
public override delete(value: T): boolean
|
|
112
|
+
{
|
|
113
|
+
const result = super.delete(value);
|
|
114
|
+
if (result) { this._publisher.publish("entry:remove", value); }
|
|
115
|
+
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Removes all entries from the {@link Set}.
|
|
121
|
+
*
|
|
122
|
+
* ---
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const set = new SetView<number>([2, 4, 8]);
|
|
127
|
+
* set.clear();
|
|
128
|
+
*
|
|
129
|
+
* console.log(set); // SetView(0) { }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
public override clear(): void
|
|
133
|
+
{
|
|
134
|
+
const size = this.size;
|
|
135
|
+
|
|
136
|
+
super.clear();
|
|
137
|
+
if (size > 0) { this._publisher.publish("collection:clear"); }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Subscribes to an event and adds a callback to be executed when the event is published.
|
|
142
|
+
*
|
|
143
|
+
* ---
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const set = new SetView<number>();
|
|
148
|
+
* const unsubscribe = set.subscribe("entry:add", (value: number) =>
|
|
149
|
+
* {
|
|
150
|
+
* if (value === 42) { unsubscribe(); }
|
|
151
|
+
* console.log(`Added ${value}`);
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* set.add(2); // Added 2
|
|
155
|
+
* set.add(42); // Added 42
|
|
156
|
+
* set.add(4);
|
|
157
|
+
* set.add(8);
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* ---
|
|
161
|
+
*
|
|
162
|
+
* @template K The key of the map containing the callback signature to subscribe.
|
|
163
|
+
*
|
|
164
|
+
* @param event The name of the event to subscribe to.
|
|
165
|
+
* @param callback The callback to execute when the event is published.
|
|
166
|
+
*
|
|
167
|
+
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
168
|
+
*/
|
|
169
|
+
public subscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): () => void
|
|
170
|
+
{
|
|
171
|
+
return this._publisher.subscribe(event, callback);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Unsubscribes from an event and removes a callback from being executed when the event is published.
|
|
176
|
+
*
|
|
177
|
+
* ---
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const callback = (value: number) => console.log(`Added ${value}`);
|
|
182
|
+
* const set = new SetView<number>();
|
|
183
|
+
*
|
|
184
|
+
* set.subscribe("entry:add", callback);
|
|
185
|
+
* set.add(2); // Added 2
|
|
186
|
+
*
|
|
187
|
+
* set.unsubscribe("entry:add", callback);
|
|
188
|
+
* set.add(4);
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* ---
|
|
192
|
+
*
|
|
193
|
+
* @template K The key of the map containing the callback signature to unsubscribe.
|
|
194
|
+
*
|
|
195
|
+
* @param event The name of the event to unsubscribe from.
|
|
196
|
+
* @param callback The callback to remove from the event.
|
|
197
|
+
*/
|
|
198
|
+
public unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): void
|
|
199
|
+
{
|
|
200
|
+
this._publisher.unsubscribe(event, callback);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public override readonly [Symbol.toStringTag]: string = "SetView";
|
|
204
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface MapViewEventsMap<K, V>
|
|
2
|
+
{
|
|
3
|
+
"entry:add": (key: K, value: V) => void;
|
|
4
|
+
"entry:remove": (key: K) => void;
|
|
5
|
+
|
|
6
|
+
"collection:clear": () => void;
|
|
7
|
+
}
|
|
8
|
+
export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
9
|
+
{
|
|
10
|
+
subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): () => void;
|
|
11
|
+
unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SetViewEventsMap<T>
|
|
15
|
+
{
|
|
16
|
+
"entry:add": (value: T) => void;
|
|
17
|
+
"entry:remove": (value: T) => void;
|
|
18
|
+
|
|
19
|
+
"collection:clear": () => void;
|
|
20
|
+
}
|
|
21
|
+
export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
22
|
+
{
|
|
23
|
+
subscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): () => void;
|
|
24
|
+
unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): void;
|
|
25
|
+
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A class representing an exception, subclass of the native
|
|
2
|
+
* A class representing an exception, subclass of the native {@link Error} class.
|
|
3
3
|
* It's the base class for any other further exception.
|
|
4
4
|
*
|
|
5
5
|
* It allows to chain exceptions together, tracking the initial cause of an error and
|
|
6
6
|
* storing its stack trace while providing a clear and friendly message to the user.
|
|
7
7
|
*
|
|
8
|
+
* ---
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
8
11
|
* ```ts
|
|
9
12
|
* try { loadGameSaves(); }
|
|
10
13
|
* catch (error)
|
|
@@ -109,6 +112,9 @@ export default class Exception extends Error
|
|
|
109
112
|
*
|
|
110
113
|
* It provides a clear and friendly message by default.
|
|
111
114
|
*
|
|
115
|
+
* ---
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
112
118
|
* ```ts
|
|
113
119
|
* function checkCase(value: "A" | "B" | "C"): 1 | 2 | 3
|
|
114
120
|
* {
|
|
@@ -160,6 +166,9 @@ export class FatalErrorException extends Exception
|
|
|
160
166
|
*
|
|
161
167
|
* It provides a clear and friendly message by default.
|
|
162
168
|
*
|
|
169
|
+
* ---
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
163
172
|
* ```ts
|
|
164
173
|
* class Database
|
|
165
174
|
* {
|
|
@@ -6,6 +6,9 @@ import Exception from "./core.js";
|
|
|
6
6
|
*
|
|
7
7
|
* It can also be used to catch all file-related exceptions at once.
|
|
8
8
|
*
|
|
9
|
+
* ---
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
9
12
|
* ```ts
|
|
10
13
|
* try { [...] }
|
|
11
14
|
* catch (error)
|
|
@@ -46,6 +49,9 @@ export class FileException extends Exception
|
|
|
46
49
|
/**
|
|
47
50
|
* A class representing an exception that can be thrown when a file already exists.
|
|
48
51
|
*
|
|
52
|
+
* ---
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
49
55
|
* ```ts
|
|
50
56
|
* import { existsSync } from "node:fs";
|
|
51
57
|
*
|
|
@@ -84,6 +90,9 @@ export class FileExistsException extends FileException
|
|
|
84
90
|
/**
|
|
85
91
|
* A class representing an exception that can be thrown when a file isn't found.
|
|
86
92
|
*
|
|
93
|
+
* ---
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
87
96
|
* ```ts
|
|
88
97
|
* import { existsSync } from "node:fs";
|
|
89
98
|
*
|
|
@@ -123,6 +132,9 @@ export class FileNotFoundException extends FileException
|
|
|
123
132
|
* A class representing an exception that can be thrown when a key is invalid or not found.
|
|
124
133
|
* It's commonly used when working with dictionaries, maps, objects, sets, etc...
|
|
125
134
|
*
|
|
135
|
+
* ---
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
126
138
|
* ```ts
|
|
127
139
|
* const map = new Map<string, number>();
|
|
128
140
|
* if (!map.has("hash"))
|
|
@@ -161,6 +173,9 @@ export class KeyException extends Exception
|
|
|
161
173
|
* A class representing an exception that can be thrown when a network operation fails.
|
|
162
174
|
* It's commonly used when it's unable to connect to a server or when a request times out.
|
|
163
175
|
*
|
|
176
|
+
* ---
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
164
179
|
* ```ts
|
|
165
180
|
* import axios, { isAxiosError } from "axios";
|
|
166
181
|
*
|
|
@@ -205,8 +220,11 @@ export class NetworkException extends Exception
|
|
|
205
220
|
|
|
206
221
|
/**
|
|
207
222
|
* A class representing an exception that can be thrown when a permission is denied.
|
|
208
|
-
* It's commonly used when
|
|
223
|
+
* It's commonly used when an user tries to access a restricted resource or perform a forbidden action.
|
|
224
|
+
*
|
|
225
|
+
* ---
|
|
209
226
|
*
|
|
227
|
+
* @example
|
|
210
228
|
* ```ts
|
|
211
229
|
* const $user = useUserStore();
|
|
212
230
|
* if (!$user.isAdmin)
|
|
@@ -245,6 +263,9 @@ export class PermissionException extends Exception
|
|
|
245
263
|
* A class representing an exception that can be thrown when a reference is invalid or not found.
|
|
246
264
|
* It's commonly used when a variable is `null`, `undefined` or when an object doesn't exist.
|
|
247
265
|
*
|
|
266
|
+
* ---
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
248
269
|
* ```ts
|
|
249
270
|
* const $el = document.getElementById("app");
|
|
250
271
|
* if ($el === null)
|
|
@@ -283,6 +304,9 @@ export class ReferenceException extends Exception
|
|
|
283
304
|
* A class representing an exception that can be thrown when a runtime error occurs.
|
|
284
305
|
* It's commonly used when an unexpected condition is encountered during the execution of a program.
|
|
285
306
|
*
|
|
307
|
+
* ---
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
286
310
|
* ```ts
|
|
287
311
|
* let status: "enabled" | "disabled" = "enabled";
|
|
288
312
|
*
|
|
@@ -324,6 +348,9 @@ export class RuntimeException extends Exception
|
|
|
324
348
|
* isn't properly configured or when a required variable isn't set.
|
|
325
349
|
* It can also be used when the environment on which the program is running is unsupported.
|
|
326
350
|
*
|
|
351
|
+
* ---
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
327
354
|
* ```ts
|
|
328
355
|
* if (!navigator.geolocation)
|
|
329
356
|
* {
|
|
@@ -361,6 +388,9 @@ export class EnvironmentException extends RuntimeException
|
|
|
361
388
|
* A class representing an exception that can be thrown when a timeout occurs.
|
|
362
389
|
* It's commonly used when a task takes too long to complete or when a request times out.
|
|
363
390
|
*
|
|
391
|
+
* ---
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
364
394
|
* ```ts
|
|
365
395
|
* const timeoutId = setTimeout(() => { throw new TimeoutException("The request timed out."); }, 5_000);
|
|
366
396
|
* const response = await fetch("https://api.example.com/data");
|
|
@@ -398,6 +428,9 @@ export class TimeoutException extends Exception
|
|
|
398
428
|
* A class representing an exception that can be thrown when a type is invalid or not supported.
|
|
399
429
|
* It's commonly used when a function receives an unexpected type of argument.
|
|
400
430
|
*
|
|
431
|
+
* ---
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
401
434
|
* ```ts
|
|
402
435
|
* function greet(name: string): void
|
|
403
436
|
* {
|
|
@@ -438,6 +471,9 @@ export class TypeException extends Exception
|
|
|
438
471
|
* A class representing an exception that can be thrown when a value is invalid.
|
|
439
472
|
* It's commonly used when a function receives an unexpected value as an argument.
|
|
440
473
|
*
|
|
474
|
+
* ---
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
441
477
|
* ```ts
|
|
442
478
|
* function setVolume(value: number): void
|
|
443
479
|
* {
|
|
@@ -478,6 +514,9 @@ export class ValueException extends Exception
|
|
|
478
514
|
* A class representing an exception that can be thrown when a value is out of range.
|
|
479
515
|
* It's commonly used when a function receives an unexpected value as an argument.
|
|
480
516
|
*
|
|
517
|
+
* ---
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
481
520
|
* ```ts
|
|
482
521
|
* function setVolume(value: number): void
|
|
483
522
|
* {
|
package/src/models/index.ts
CHANGED
|
@@ -26,6 +26,9 @@ import type {
|
|
|
26
26
|
* This allows to chain multiple transformations without
|
|
27
27
|
* the need to iterate over the elements multiple times.
|
|
28
28
|
*
|
|
29
|
+
* ---
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
29
32
|
* ```ts
|
|
30
33
|
* const result = new SmartAsyncIterator<number>(["-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"])
|
|
31
34
|
* .map((value) => Number(value))
|
|
@@ -37,6 +40,8 @@ import type {
|
|
|
37
40
|
* console.log(await result); // 31
|
|
38
41
|
* ```
|
|
39
42
|
*
|
|
43
|
+
* ---
|
|
44
|
+
*
|
|
40
45
|
* @template T The type of elements in the iterator.
|
|
41
46
|
* @template R The type of the final result of the iterator. Default is `void`.
|
|
42
47
|
* @template N The type of the argument passed to the `next` method. Default is `undefined`.
|
|
@@ -17,6 +17,9 @@ import type { GeneratorFunction, Iteratee, TypeGuardPredicate, Reducer, Iterator
|
|
|
17
17
|
* This allows to chain multiple transformations without
|
|
18
18
|
* the need to iterate over the elements multiple times.
|
|
19
19
|
*
|
|
20
|
+
* ---
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
20
23
|
* ```ts
|
|
21
24
|
* const result = new SmartIterator<number>(["-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"])
|
|
22
25
|
* .map(Number)
|
|
@@ -28,6 +31,8 @@ import type { GeneratorFunction, Iteratee, TypeGuardPredicate, Reducer, Iterator
|
|
|
28
31
|
* console.log(result); // 31
|
|
29
32
|
* ```
|
|
30
33
|
*
|
|
34
|
+
* ---
|
|
35
|
+
*
|
|
31
36
|
* @template T The type of elements in the iterator.
|
|
32
37
|
* @template R The type of the final result of the iterator. Default is `void`.
|
|
33
38
|
* @template N The type of the argument required by the `next` method. Default is `undefined`.
|