@elgato/utils 0.0.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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +210 -0
  3. package/dist/disposable.d.ts +19 -0
  4. package/dist/disposable.js +21 -0
  5. package/dist/enumerable.d.ts +141 -0
  6. package/dist/enumerable.js +302 -0
  7. package/dist/event-emitter.d.ts +124 -0
  8. package/dist/event-emitter.js +170 -0
  9. package/dist/i18n/index.d.ts +2 -0
  10. package/dist/i18n/index.js +2 -0
  11. package/dist/i18n/language.d.ts +14 -0
  12. package/dist/i18n/language.js +4 -0
  13. package/dist/i18n/provider.d.ts +42 -0
  14. package/dist/i18n/provider.js +76 -0
  15. package/dist/index.d.ts +10 -0
  16. package/dist/index.js +10 -0
  17. package/dist/json.d.ts +14 -0
  18. package/dist/json.js +1 -0
  19. package/dist/lazy.d.ts +16 -0
  20. package/dist/lazy.js +30 -0
  21. package/dist/logging/console-target.d.ts +10 -0
  22. package/dist/logging/console-target.js +20 -0
  23. package/dist/logging/file-target.d.ts +59 -0
  24. package/dist/logging/file-target.js +101 -0
  25. package/dist/logging/format.d.ts +23 -0
  26. package/dist/logging/format.js +47 -0
  27. package/dist/logging/index.d.ts +6 -0
  28. package/dist/logging/index.js +6 -0
  29. package/dist/logging/level.d.ts +30 -0
  30. package/dist/logging/level.js +21 -0
  31. package/dist/logging/logger.d.ts +87 -0
  32. package/dist/logging/logger.js +120 -0
  33. package/dist/logging/target.d.ts +32 -0
  34. package/dist/logging/target.js +1 -0
  35. package/dist/objects.d.ts +19 -0
  36. package/dist/objects.js +33 -0
  37. package/dist/parsers.d.ts +12 -0
  38. package/dist/parsers.js +26 -0
  39. package/dist/promises.d.ts +24 -0
  40. package/dist/promises.js +13 -0
  41. package/dist/rpc/client.d.ts +56 -0
  42. package/dist/rpc/client.js +144 -0
  43. package/dist/rpc/gateway.d.ts +28 -0
  44. package/dist/rpc/gateway.js +38 -0
  45. package/dist/rpc/index.d.ts +9 -0
  46. package/dist/rpc/index.js +9 -0
  47. package/dist/rpc/json-rpc/error.d.ts +52 -0
  48. package/dist/rpc/json-rpc/error.js +34 -0
  49. package/dist/rpc/json-rpc/request.d.ts +28 -0
  50. package/dist/rpc/json-rpc/request.js +13 -0
  51. package/dist/rpc/json-rpc/response.d.ts +53 -0
  52. package/dist/rpc/json-rpc/response.js +22 -0
  53. package/dist/rpc/proxy.d.ts +7 -0
  54. package/dist/rpc/proxy.js +1 -0
  55. package/dist/rpc/request.d.ts +22 -0
  56. package/dist/rpc/request.js +1 -0
  57. package/dist/rpc/response.d.ts +58 -0
  58. package/dist/rpc/response.js +69 -0
  59. package/dist/rpc/server.d.ts +34 -0
  60. package/dist/rpc/server.js +87 -0
  61. package/dist/timers.d.ts +7 -0
  62. package/dist/timers.js +22 -0
  63. package/dist/types.d.ts +20 -0
  64. package/dist/types.js +1 -0
  65. package/package.json +65 -0
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Provides a read-only iterable collection of items that also acts as a partial polyfill for iterator helpers.
3
+ */
4
+ export class Enumerable {
5
+ /**
6
+ * Backing function responsible for providing the iterator of items.
7
+ */
8
+ #items;
9
+ /**
10
+ * Backing function for {@link Enumerable.length}.
11
+ */
12
+ #length;
13
+ /**
14
+ * Captured iterator from the underlying iterable; used to fulfil {@link IterableIterator} methods.
15
+ */
16
+ #iterator;
17
+ /**
18
+ * Initializes a new instance of the {@link Enumerable} class.
19
+ * @param source Source that contains the items.
20
+ * @returns The enumerable.
21
+ */
22
+ constructor(source) {
23
+ if (source instanceof Enumerable) {
24
+ // Enumerable
25
+ this.#items = source.#items;
26
+ this.#length = source.#length;
27
+ }
28
+ else if (Array.isArray(source)) {
29
+ // Array
30
+ this.#items = () => source.values();
31
+ this.#length = () => source.length;
32
+ }
33
+ else if (source instanceof Map || source instanceof Set) {
34
+ // Map or Set
35
+ this.#items = () => source.values();
36
+ this.#length = () => source.size;
37
+ }
38
+ else {
39
+ // IterableIterator delegate
40
+ this.#items = source;
41
+ this.#length = () => {
42
+ let i = 0;
43
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
44
+ for (const _ of this) {
45
+ i++;
46
+ }
47
+ return i;
48
+ };
49
+ }
50
+ }
51
+ /**
52
+ * Gets the number of items in the enumerable.
53
+ * @returns The number of items.
54
+ */
55
+ get length() {
56
+ return this.#length();
57
+ }
58
+ /**
59
+ * Gets the iterator for the enumerable.
60
+ * @yields The items.
61
+ */
62
+ *[Symbol.iterator]() {
63
+ for (const item of this.#items()) {
64
+ yield item;
65
+ }
66
+ }
67
+ /**
68
+ * Transforms each item within this iterator to an indexed pair, with each pair represented as an array.
69
+ * @returns An iterator of indexed pairs.
70
+ */
71
+ asIndexedPairs() {
72
+ return new Enumerable(function* () {
73
+ let i = 0;
74
+ for (const item of this) {
75
+ yield [i++, item];
76
+ }
77
+ }.bind(this));
78
+ }
79
+ /**
80
+ * Returns an iterator with the first items dropped, up to the specified limit.
81
+ * @param limit The number of elements to drop from the start of the iteration.
82
+ * @returns An iterator of items after the limit.
83
+ */
84
+ drop(limit) {
85
+ if (isNaN(limit) || limit < 0) {
86
+ throw new RangeError("limit must be 0, or a positive number");
87
+ }
88
+ return new Enumerable(function* () {
89
+ let i = 0;
90
+ for (const item of this) {
91
+ if (i++ >= limit) {
92
+ yield item;
93
+ }
94
+ }
95
+ }.bind(this));
96
+ }
97
+ /**
98
+ * Determines whether all items satisfy the specified predicate.
99
+ * @param predicate Function that determines whether each item fulfils the predicate.
100
+ * @returns `true` when all items satisfy the predicate; otherwise `false`.
101
+ */
102
+ every(predicate) {
103
+ for (const item of this) {
104
+ if (!predicate(item)) {
105
+ return false;
106
+ }
107
+ }
108
+ return true;
109
+ }
110
+ /**
111
+ * Returns an iterator of items that meet the specified predicate..
112
+ * @param predicate Function that determines which items to filter.
113
+ * @returns An iterator of filtered items.
114
+ */
115
+ filter(predicate) {
116
+ return new Enumerable(function* () {
117
+ for (const item of this) {
118
+ if (predicate(item)) {
119
+ yield item;
120
+ }
121
+ }
122
+ }.bind(this));
123
+ }
124
+ /**
125
+ * Finds the first item that satisfies the specified predicate.
126
+ * @param predicate Predicate to match items against.
127
+ * @returns The first item that satisfied the predicate; otherwise `undefined`.
128
+ */
129
+ find(predicate) {
130
+ for (const item of this) {
131
+ if (predicate(item)) {
132
+ return item;
133
+ }
134
+ }
135
+ }
136
+ /**
137
+ * Finds the last item that satisfies the specified predicate.
138
+ * @param predicate Predicate to match items against.
139
+ * @returns The first item that satisfied the predicate; otherwise `undefined`.
140
+ */
141
+ findLast(predicate) {
142
+ let result = undefined;
143
+ for (const item of this) {
144
+ if (predicate(item)) {
145
+ result = item;
146
+ }
147
+ }
148
+ return result;
149
+ }
150
+ /**
151
+ * Returns an iterator containing items transformed using the specified mapper function.
152
+ * @param mapper Function responsible for transforming each item.
153
+ * @returns An iterator of transformed items.
154
+ */
155
+ flatMap(mapper) {
156
+ return new Enumerable(function* () {
157
+ for (const item of this) {
158
+ for (const mapped of mapper(item)) {
159
+ yield mapped;
160
+ }
161
+ }
162
+ }.bind(this));
163
+ }
164
+ /**
165
+ * Iterates over each item, and invokes the specified function.
166
+ * @param fn Function to invoke against each item.
167
+ */
168
+ forEach(fn) {
169
+ for (const item of this) {
170
+ fn(item);
171
+ }
172
+ }
173
+ /**
174
+ * Determines whether the search item exists in the collection exists.
175
+ * @param search Item to search for.
176
+ * @returns `true` when the item was found; otherwise `false`.
177
+ */
178
+ includes(search) {
179
+ return this.some((item) => item === search);
180
+ }
181
+ /**
182
+ * Returns an iterator of mapped items using the mapper function.
183
+ * @param mapper Function responsible for mapping the items.
184
+ * @returns An iterator of mapped items.
185
+ */
186
+ map(mapper) {
187
+ return new Enumerable(function* () {
188
+ for (const item of this) {
189
+ yield mapper(item);
190
+ }
191
+ }.bind(this));
192
+ }
193
+ /**
194
+ * Captures the underlying iterable, if it is not already captured, and gets the next item in the iterator.
195
+ * @param args Optional values to send to the generator.
196
+ * @returns An iterator result of the current iteration; when `done` is `false`, the current `value` is provided.
197
+ */
198
+ next(...args) {
199
+ this.#iterator ??= this.#items();
200
+ const result = this.#iterator.next(...args);
201
+ if (result.done) {
202
+ this.#iterator = undefined;
203
+ }
204
+ return result;
205
+ }
206
+ /**
207
+ * Applies the accumulator function to each item, and returns the result.
208
+ * @param accumulator Function responsible for accumulating all items within the collection.
209
+ * @param initial Initial value supplied to the accumulator.
210
+ * @returns Result of accumulating each value.
211
+ */
212
+ reduce(accumulator, initial) {
213
+ if (this.length === 0) {
214
+ if (initial === undefined) {
215
+ throw new TypeError("Reduce of empty enumerable with no initial value.");
216
+ }
217
+ return initial;
218
+ }
219
+ let result = initial;
220
+ for (const item of this) {
221
+ if (result === undefined) {
222
+ result = item;
223
+ }
224
+ else {
225
+ result = accumulator(result, item);
226
+ }
227
+ }
228
+ return result;
229
+ }
230
+ /**
231
+ * Acts as if a `return` statement is inserted in the generator's body at the current suspended position.
232
+ *
233
+ * Please note, in the context of an {@link Enumerable}, calling {@link Enumerable.return} will clear the captured iterator,
234
+ * if there is one. Subsequent calls to {@link Enumerable.next} will result in re-capturing the underlying iterable, and
235
+ * yielding items from the beginning.
236
+ * @param value Value to return.
237
+ * @returns The value as an iterator result.
238
+ */
239
+ return(value) {
240
+ this.#iterator = undefined;
241
+ return { done: true, value };
242
+ }
243
+ /**
244
+ * Determines whether an item in the collection exists that satisfies the specified predicate.
245
+ * @param predicate Function used to search for an item.
246
+ * @returns `true` when the item was found; otherwise `false`.
247
+ */
248
+ some(predicate) {
249
+ for (const item of this) {
250
+ if (predicate(item)) {
251
+ return true;
252
+ }
253
+ }
254
+ return false;
255
+ }
256
+ /**
257
+ * Returns an iterator with the items, from 0, up to the specified limit.
258
+ * @param limit Limit of items to take.
259
+ * @returns An iterator of items from 0 to the limit.
260
+ */
261
+ take(limit) {
262
+ if (isNaN(limit) || limit < 0) {
263
+ throw new RangeError("limit must be 0, or a positive number");
264
+ }
265
+ return new Enumerable(function* () {
266
+ let i = 0;
267
+ for (const item of this) {
268
+ if (i++ < limit) {
269
+ yield item;
270
+ }
271
+ }
272
+ }.bind(this));
273
+ }
274
+ /**
275
+ * Acts as if a `throw` statement is inserted in the generator's body at the current suspended position.
276
+ * @param e Error to throw.
277
+ */
278
+ throw(e) {
279
+ throw e;
280
+ }
281
+ /**
282
+ * Converts this iterator to an array.
283
+ * @returns The array of items from this iterator.
284
+ */
285
+ toArray() {
286
+ return Array.from(this);
287
+ }
288
+ /**
289
+ * Converts this iterator to serializable collection.
290
+ * @returns The serializable collection of items.
291
+ */
292
+ toJSON() {
293
+ return this.toArray();
294
+ }
295
+ /**
296
+ * Converts this iterator to a string.
297
+ * @returns The string.
298
+ */
299
+ toString() {
300
+ return `${this.toArray()}`;
301
+ }
302
+ }
@@ -0,0 +1,124 @@
1
+ import { type IDisposable } from "./disposable.js";
2
+ /**
3
+ * An event emitter that enables the listening for, and emitting of, events.
4
+ */
5
+ export declare class EventEmitter<TMap extends EventMap<TMap>> {
6
+ /**
7
+ * Underlying collection of events and their listeners.
8
+ */
9
+ private readonly events;
10
+ /**
11
+ * Adds the event {@link listener} for the event named {@link eventName}.
12
+ * @param eventName Name of the event.
13
+ * @param listener Event handler function.
14
+ * @returns This instance with the {@link listener} added.
15
+ */
16
+ addListener<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
17
+ /**
18
+ * Adds the event {@link listener} for the event named {@link eventName}, and returns a disposable capable of removing the event listener.
19
+ * @param eventName Name of the event.
20
+ * @param listener Event handler function.
21
+ * @returns A disposable that removes the listener when disposed.
22
+ */
23
+ disposableOn<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): IDisposable;
24
+ /**
25
+ * Emits the {@link eventName}, invoking all event listeners with the specified {@link args}.
26
+ * @param eventName Name of the event.
27
+ * @param args Arguments supplied to each event listener.
28
+ * @returns `true` when there was a listener associated with the event; otherwise `false`.
29
+ */
30
+ emit<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, ...args: TArgs): boolean;
31
+ /**
32
+ * Gets the event names with event listeners.
33
+ * @returns Event names.
34
+ */
35
+ eventNames(): EventsOf<TMap>[];
36
+ /**
37
+ * Gets the number of event listeners for the event named {@link eventName}. When a {@link listener} is defined, only matching event listeners are counted.
38
+ * @param eventName Name of the event.
39
+ * @param listener Optional event listener to count.
40
+ * @returns Number of event listeners.
41
+ */
42
+ listenerCount<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener?: (...args: TArgs) => void): number;
43
+ /**
44
+ * Gets the event listeners for the event named {@link eventName}.
45
+ * @param eventName Name of the event.
46
+ * @returns The event listeners.
47
+ */
48
+ listeners<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName): ((...args: TArgs) => void)[];
49
+ /**
50
+ * Removes the event {@link listener} for the event named {@link eventName}.
51
+ * @param eventName Name of the event.
52
+ * @param listener Event handler function.
53
+ * @returns This instance with the event {@link listener} removed.
54
+ */
55
+ off<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
56
+ /**
57
+ * Adds the event {@link listener} for the event named {@link eventName}.
58
+ * @param eventName Name of the event.
59
+ * @param listener Event handler function.
60
+ * @returns This instance with the event {@link listener} added.
61
+ */
62
+ on<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
63
+ /**
64
+ * Adds the **one-time** event {@link listener} for the event named {@link eventName}.
65
+ * @param eventName Name of the event.
66
+ * @param listener Event handler function.
67
+ * @returns This instance with the event {@link listener} added.
68
+ */
69
+ once<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
70
+ /**
71
+ * Adds the event {@link listener} to the beginning of the listeners for the event named {@link eventName}.
72
+ * @param eventName Name of the event.
73
+ * @param listener Event handler function.
74
+ * @returns This instance with the event {@link listener} prepended.
75
+ */
76
+ prependListener<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
77
+ /**
78
+ * Adds the **one-time** event {@link listener} to the beginning of the listeners for the event named {@link eventName}.
79
+ * @param eventName Name of the event.
80
+ * @param listener Event handler function.
81
+ * @returns This instance with the event {@link listener} prepended.
82
+ */
83
+ prependOnceListener<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
84
+ /**
85
+ * Removes all event listeners for the event named {@link eventName}.
86
+ * @param eventName Name of the event.
87
+ * @returns This instance with the event listeners removed
88
+ */
89
+ removeAllListeners<TEventName extends EventsOf<TMap>>(eventName: TEventName): this;
90
+ /**
91
+ * Removes the event {@link listener} for the event named {@link eventName}.
92
+ * @param eventName Name of the event.
93
+ * @param listener Event handler function.
94
+ * @returns This instance with the event {@link listener} removed.
95
+ */
96
+ removeListener<TEventName extends EventsOf<TMap>, TArgs extends EventArgs<TMap, TEventName>>(eventName: TEventName, listener: (...args: TArgs) => void): this;
97
+ /**
98
+ * Adds the event {@link listener} for the event named {@link eventName}.
99
+ * @param eventName Name of the event.
100
+ * @param fn Function responsible for adding the new event handler function.
101
+ * @returns This instance with event {@link listener} added.
102
+ */
103
+ private add;
104
+ }
105
+ /**
106
+ * A map of events and their arguments (represented as an array) that are supplied to the event's listener when the event is emitted.
107
+ * @example
108
+ * type UserService = {
109
+ * created: [id: number, userName: string];
110
+ * deleted: [id: number];
111
+ * }
112
+ */
113
+ type EventMap<T> = {
114
+ [K in keyof T]: K extends string ? (T[K] extends unknown[] ? T[K] : never) : never;
115
+ };
116
+ /**
117
+ * Parsed {@link EventMap} whereby each property is a `string` that denotes an event name, and the associated value type defines the listener arguments.
118
+ */
119
+ export type EventsOf<TMap extends EventMap<TMap>> = keyof TMap | (string & {});
120
+ /**
121
+ * Parses the event arguments for the specified event from the event map.
122
+ */
123
+ export type EventArgs<TMap extends EventMap<TMap>, TEvent extends EventsOf<TMap>> = TEvent extends keyof TMap ? TMap[TEvent] extends unknown[] ? TMap[TEvent] : never : unknown[];
124
+ export {};
@@ -0,0 +1,170 @@
1
+ import { deferredDisposable } from "./disposable.js";
2
+ /**
3
+ * An event emitter that enables the listening for, and emitting of, events.
4
+ */
5
+ export class EventEmitter {
6
+ /**
7
+ * Underlying collection of events and their listeners.
8
+ */
9
+ events = new Map();
10
+ /**
11
+ * Adds the event {@link listener} for the event named {@link eventName}.
12
+ * @param eventName Name of the event.
13
+ * @param listener Event handler function.
14
+ * @returns This instance with the {@link listener} added.
15
+ */
16
+ addListener(eventName, listener) {
17
+ return this.on(eventName, listener);
18
+ }
19
+ /**
20
+ * Adds the event {@link listener} for the event named {@link eventName}, and returns a disposable capable of removing the event listener.
21
+ * @param eventName Name of the event.
22
+ * @param listener Event handler function.
23
+ * @returns A disposable that removes the listener when disposed.
24
+ */
25
+ disposableOn(eventName, listener) {
26
+ this.addListener(eventName, listener);
27
+ return deferredDisposable(() => this.removeListener(eventName, listener));
28
+ }
29
+ /**
30
+ * Emits the {@link eventName}, invoking all event listeners with the specified {@link args}.
31
+ * @param eventName Name of the event.
32
+ * @param args Arguments supplied to each event listener.
33
+ * @returns `true` when there was a listener associated with the event; otherwise `false`.
34
+ */
35
+ emit(eventName, ...args) {
36
+ const listeners = this.events.get(eventName);
37
+ if (listeners === undefined) {
38
+ return false;
39
+ }
40
+ for (let i = 0; i < listeners.length;) {
41
+ const { listener, once } = listeners[i];
42
+ if (once) {
43
+ listeners.splice(i, 1);
44
+ }
45
+ else {
46
+ i++;
47
+ }
48
+ listener(...args);
49
+ }
50
+ return true;
51
+ }
52
+ /**
53
+ * Gets the event names with event listeners.
54
+ * @returns Event names.
55
+ */
56
+ eventNames() {
57
+ return Array.from(this.events.keys());
58
+ }
59
+ /**
60
+ * Gets the number of event listeners for the event named {@link eventName}. When a {@link listener} is defined, only matching event listeners are counted.
61
+ * @param eventName Name of the event.
62
+ * @param listener Optional event listener to count.
63
+ * @returns Number of event listeners.
64
+ */
65
+ listenerCount(eventName, listener) {
66
+ const listeners = this.events.get(eventName);
67
+ if (listeners === undefined || listener == undefined) {
68
+ return listeners?.length || 0;
69
+ }
70
+ let count = 0;
71
+ listeners.forEach((ev) => {
72
+ if (ev.listener === listener) {
73
+ count++;
74
+ }
75
+ });
76
+ return count;
77
+ }
78
+ /**
79
+ * Gets the event listeners for the event named {@link eventName}.
80
+ * @param eventName Name of the event.
81
+ * @returns The event listeners.
82
+ */
83
+ listeners(eventName) {
84
+ return Array.from(this.events.get(eventName) || []).map(({ listener }) => listener);
85
+ }
86
+ /**
87
+ * Removes the event {@link listener} for the event named {@link eventName}.
88
+ * @param eventName Name of the event.
89
+ * @param listener Event handler function.
90
+ * @returns This instance with the event {@link listener} removed.
91
+ */
92
+ off(eventName, listener) {
93
+ const listeners = this.events.get(eventName) || [];
94
+ for (let i = listeners.length - 1; i >= 0; i--) {
95
+ if (listeners[i].listener === listener) {
96
+ listeners.splice(i, 1);
97
+ }
98
+ }
99
+ return this;
100
+ }
101
+ /**
102
+ * Adds the event {@link listener} for the event named {@link eventName}.
103
+ * @param eventName Name of the event.
104
+ * @param listener Event handler function.
105
+ * @returns This instance with the event {@link listener} added.
106
+ */
107
+ on(eventName, listener) {
108
+ return this.add(eventName, (listeners) => listeners.push({ listener }));
109
+ }
110
+ /**
111
+ * Adds the **one-time** event {@link listener} for the event named {@link eventName}.
112
+ * @param eventName Name of the event.
113
+ * @param listener Event handler function.
114
+ * @returns This instance with the event {@link listener} added.
115
+ */
116
+ once(eventName, listener) {
117
+ return this.add(eventName, (listeners) => listeners.push({ listener, once: true }));
118
+ }
119
+ /**
120
+ * Adds the event {@link listener} to the beginning of the listeners for the event named {@link eventName}.
121
+ * @param eventName Name of the event.
122
+ * @param listener Event handler function.
123
+ * @returns This instance with the event {@link listener} prepended.
124
+ */
125
+ prependListener(eventName, listener) {
126
+ return this.add(eventName, (listeners) => listeners.splice(0, 0, { listener }));
127
+ }
128
+ /**
129
+ * Adds the **one-time** event {@link listener} to the beginning of the listeners for the event named {@link eventName}.
130
+ * @param eventName Name of the event.
131
+ * @param listener Event handler function.
132
+ * @returns This instance with the event {@link listener} prepended.
133
+ */
134
+ prependOnceListener(eventName, listener) {
135
+ return this.add(eventName, (listeners) => listeners.splice(0, 0, { listener, once: true }));
136
+ }
137
+ /**
138
+ * Removes all event listeners for the event named {@link eventName}.
139
+ * @param eventName Name of the event.
140
+ * @returns This instance with the event listeners removed
141
+ */
142
+ removeAllListeners(eventName) {
143
+ this.events.delete(eventName);
144
+ return this;
145
+ }
146
+ /**
147
+ * Removes the event {@link listener} for the event named {@link eventName}.
148
+ * @param eventName Name of the event.
149
+ * @param listener Event handler function.
150
+ * @returns This instance with the event {@link listener} removed.
151
+ */
152
+ removeListener(eventName, listener) {
153
+ return this.off(eventName, listener);
154
+ }
155
+ /**
156
+ * Adds the event {@link listener} for the event named {@link eventName}.
157
+ * @param eventName Name of the event.
158
+ * @param fn Function responsible for adding the new event handler function.
159
+ * @returns This instance with event {@link listener} added.
160
+ */
161
+ add(eventName, fn) {
162
+ let listeners = this.events.get(eventName);
163
+ if (listeners === undefined) {
164
+ listeners = [];
165
+ this.events.set(eventName, listeners);
166
+ }
167
+ fn(listeners);
168
+ return this;
169
+ }
170
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./language.js";
2
+ export * from "./provider.js";
@@ -0,0 +1,2 @@
1
+ export * from "./language.js";
2
+ export * from "./provider.js";
@@ -0,0 +1,14 @@
1
+ import type { Alphabetical } from "../types.js";
2
+ /**
3
+ * Default language supported by all i18n providers.
4
+ */
5
+ export declare const defaultLanguage: "en";
6
+ /**
7
+ * Extracts the first portion of strings that include a hyphen.
8
+ */
9
+ export type Language<T extends string> = ExtractLanguage<T> | typeof defaultLanguage;
10
+ /**
11
+ * Extracts the languages from a union of languages or regionalized languages
12
+ */
13
+ type ExtractLanguage<T extends string> = T extends `_` | `-` | `` ? never : T extends Alphabetical<T> ? T : T extends `${infer L}${`_` | `-`}${infer R}` ? L extends Alphabetical<L> ? R extends Alphabetical<R> ? L | T : never : never : never;
14
+ export {};
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Default language supported by all i18n providers.
3
+ */
4
+ export const defaultLanguage = "en";
@@ -0,0 +1,42 @@
1
+ import type { JsonObject } from "../json.js";
2
+ import { type Language } from "./language.js";
3
+ /**
4
+ * Internalization provider, responsible for managing localizations and translating resources.
5
+ */
6
+ export declare class I18nProvider<T extends string> {
7
+ #private;
8
+ /**
9
+ * Initializes a new instance of the {@link I18nProvider} class.
10
+ * @param language The default language to be used when retrieving translations for a given key.
11
+ * @param readTranslations Function responsible for providing localized resources for a given language.
12
+ */
13
+ constructor(language: Language<T>, readTranslations: TranslationsReader<T>);
14
+ /**
15
+ * Translates the specified {@link key}, as defined within the resources for the {@link language}.
16
+ * When the key is not found, the default language is checked. Alias of {@link I18nProvider.translate}.
17
+ * @param key Key of the translation.
18
+ * @param language Optional language to get the translation for; otherwise the default language.
19
+ * @returns The translation; otherwise the key.
20
+ */
21
+ t(key: string, language?: Language<T>): string;
22
+ /**
23
+ * Translates the specified {@link key}, as defined within the resources for the {@link language}.
24
+ * When the key is not found, the default language is checked.
25
+ * @param key Key of the translation.
26
+ * @param language Optional language to get the translation for; otherwise the default language.
27
+ * @returns The translation; otherwise the key.
28
+ */
29
+ translate(key: string, language?: Language<T>): string;
30
+ /**
31
+ * Gets the translations for the specified language.
32
+ * @param language Language whose translations are being retrieved.
33
+ * @returns The translations; otherwise `null`.
34
+ */
35
+ private getTranslations;
36
+ }
37
+ /**
38
+ * Function responsible for providing localized resources for a given language.
39
+ * @param language The language whose resources should be retrieved.
40
+ * @returns Localized resources represented as a JSON object.
41
+ */
42
+ export type TranslationsReader<T extends string> = (language: Language<T>) => JsonObject | null;