@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Corsair Memory Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,210 @@
1
+ <div align="center">
2
+
3
+ # @elgato/utils
4
+
5
+ Utilities used throughout the Elgato ecosystem.
6
+
7
+ [![@elgato/utils npm package](https://img.shields.io/npm/v/%40elgato/utils?logo=npm&logoColor=white)](https://www.npmjs.com/package/@elgato/utils)
8
+ [![Elgato homepage](https://img.shields.io/badge/Elgato-3431cf?labelColor=grey&logo=Elgato)](https://elgato.com)
9
+
10
+ </div>
11
+
12
+ ## Installation
13
+
14
+ ```
15
+ npm install @elgato/utils
16
+ ```
17
+
18
+ ## Disposables
19
+
20
+ ### `deferredDisposable`
21
+
22
+ Creates a new function that implements `Symbol.dispose` and accepts a disposer function. The disposer function is called when the new function is disposed.
23
+
24
+ ```js
25
+ import { deferredDisposable } from "@elgato/utils";
26
+
27
+ const cleanup = () => console.log("Hello world")
28
+ using (deferredDisposable(cleanup)) {
29
+ // ...
30
+ }
31
+
32
+ // "Hello world"
33
+ ```
34
+
35
+ ## Events
36
+
37
+ ### `EventEmitter`
38
+
39
+ An strongly-typed event emitter that enables the listening for, and emitting of, events; supported in browser and Node.js environments.
40
+
41
+ ```ts
42
+ import { EventEmitter } from "@elgato/utils";
43
+
44
+ type EventMap = {
45
+ created: [name: string];
46
+ };
47
+
48
+ const emitter = new EventEmitter<EventMap>();
49
+ emitter.on("created", (name: string) => {
50
+ // ...
51
+ });
52
+ ```
53
+
54
+ ## JSON
55
+
56
+ ### `JsonObject`
57
+
58
+ Type that represents an object within JSON.
59
+
60
+ ```ts
61
+ import { type JsonObject } from "@elgato/utils";
62
+
63
+ const obj: JsonObject = {
64
+ name: "Elgato",
65
+ };
66
+ ```
67
+
68
+ ### `JsonPrimitive`
69
+
70
+ Type that represents a primitive within JSON, for example a string or number.
71
+
72
+ ```ts
73
+ import { type JsonPrimitive } from "@elgato/utils";
74
+
75
+ const value: JsonPrimitive = "Elgato";
76
+ ```
77
+
78
+ ### `JsonValue`
79
+
80
+ Type that represents a valid JSON value, for example an object, array, or primitive.
81
+
82
+ ```ts
83
+ import { type JsonValue } from "@elgato/utils";
84
+
85
+ const value: JsonValue = ["Hello", "World"];
86
+ ```
87
+
88
+ ## Miscellaneous
89
+
90
+ ### `Enumerable`
91
+
92
+ Provides a read-only iterable collection of items that also acts as a partial polyfill for iterator helpers.
93
+
94
+ ```js
95
+ import { Enumerable } from "@elgato/utils";
96
+
97
+ const items = new Enumerable(["One", "Two", "Three", "Four"]);
98
+ items
99
+ .drop(1) // Drop "One"
100
+ .take(2); // Take "Two" & "Three"
101
+ ```
102
+
103
+ Polyfilled iterator helpers:
104
+
105
+ - `Symbol.iterator`
106
+ - `asIndexedPairs()`
107
+ - `drop(limit)`
108
+ - `every(predicate)`
109
+ - `filter(predicate)`
110
+ - `find(predicate)`
111
+ - `findLast(predicate)`
112
+ - `flapMap(mapper)`
113
+ - `forEach(fn)`
114
+ - `includes(search)`
115
+ - `map(mapper)`
116
+ - `reduce(accumulator, initial)`
117
+ - `some(predicate)`
118
+ - `take(limit)`
119
+ - `toArray()`
120
+
121
+ ### `Lazy`
122
+
123
+ Object that wraps a lazily instantiated value, similar to C# [`Lazy<T>`](https://learn.microsoft.com/en-us/dotnet/framework/performance/lazy-initialization).
124
+
125
+ ```js
126
+ import { Lazy } from "@elgato/utils";
127
+
128
+ const lazy = new Lazy(() => "Hello world");
129
+ lazy.value; // "Hello world";
130
+ ```
131
+
132
+ ## Objects
133
+
134
+ ### `get(source, path)`
135
+
136
+ Gets the value at the specified (deep) path.
137
+
138
+ ```js
139
+ import { get } from "@elgato/utils";
140
+
141
+ const obj = {
142
+ name: "Elgato",
143
+ };
144
+
145
+ get(obj, "name"); // Elgato
146
+ ```
147
+
148
+ ### `set(target, path, value)`
149
+
150
+ Sets the value at the specified (deep) path.
151
+
152
+ ```js
153
+ import { get } from "@elgato/utils";
154
+
155
+ const obj = {
156
+ name: "Gato",
157
+ };
158
+
159
+ set(obj, "name", "Elgato"); // { name: "Elgato" }
160
+ ```
161
+
162
+ ## Parsers
163
+
164
+ ### `parseBoolean(value)`
165
+
166
+ Parses the value a truthy-boolean; the strings `"0"` and `"false"` are parsed as `false`.
167
+
168
+ ```js
169
+ import { parseBoolean } from "@elgato/utils";
170
+
171
+ const a = parseBoolean(1); // true
172
+ const b = parseBoolean("false"); // false
173
+ ```
174
+
175
+ ### `parseNumber(value)`
176
+
177
+ Attempts to parse a value to a number; returns `undefined` when parsing was unsuccessful.
178
+
179
+ ```js
180
+ import { parseNumber } from "@elgato/utils";
181
+
182
+ const number = parseNumber("13"); // 13
183
+ ```
184
+
185
+ ## Promises
186
+
187
+ ### `withResolvers()`
188
+
189
+ Function that returns an object that contains the promise, and two functions to resolve or reject it. Polyfill of [Promise.withResolvers()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers).
190
+
191
+ ```js
192
+ import { withResolvers } "@elgato/utils";
193
+
194
+ const { promise, resolve, reject } = withResolvers<string>();
195
+ ```
196
+
197
+ ## Timers
198
+
199
+ ### `debounce(fn, delay)`
200
+
201
+ Wraps a function; subsequent invocations of the wrapped function made within the specified delay are debounced to prevent multiple calls.
202
+
203
+ ```js
204
+ import { debounce } from "@elgato/utils";
205
+
206
+ const fn = debounce(() => console.log("Hello world"), 1000);
207
+
208
+ fn(); // Debounced
209
+ fn(); // "Hello world"
210
+ ```
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Creates a {@link IDisposable} that defers the disposing to the {@link dispose} function; disposing is guarded so that it may only occur once.
3
+ * @param dispose Function responsible for disposing.
4
+ * @returns Disposable whereby the disposing is delegated to the {@link dispose} function.
5
+ */
6
+ export declare function deferredDisposable(dispose: (...args: unknown[]) => void): IDisposable;
7
+ /**
8
+ * An extension of the upcoming explicit resource management disposable that defines a `dispose()` member.
9
+ */
10
+ export interface IDisposable {
11
+ /**
12
+ * Disposes this instance.
13
+ */
14
+ [Symbol.dispose](): void;
15
+ /**
16
+ * Disposes this instance.
17
+ */
18
+ dispose(): void;
19
+ }
@@ -0,0 +1,21 @@
1
+ // Polyfill, explicit resource management https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ Symbol.dispose ??= Symbol("Symbol.dispose");
4
+ /**
5
+ * Creates a {@link IDisposable} that defers the disposing to the {@link dispose} function; disposing is guarded so that it may only occur once.
6
+ * @param dispose Function responsible for disposing.
7
+ * @returns Disposable whereby the disposing is delegated to the {@link dispose} function.
8
+ */
9
+ export function deferredDisposable(dispose) {
10
+ let isDisposed = false;
11
+ const guardedDispose = () => {
12
+ if (!isDisposed) {
13
+ dispose();
14
+ isDisposed = true;
15
+ }
16
+ };
17
+ return {
18
+ [Symbol.dispose]: guardedDispose,
19
+ dispose: guardedDispose,
20
+ };
21
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Provides a read-only iterable collection of items that also acts as a partial polyfill for iterator helpers.
3
+ */
4
+ export declare class Enumerable<T> implements IterableIterator<T> {
5
+ #private;
6
+ /**
7
+ * Initializes a new instance of the {@link Enumerable} class.
8
+ * @param source Source that contains the items.
9
+ * @returns The enumerable.
10
+ */
11
+ constructor(source: Enumerable<T> | Map<unknown, T> | Set<T> | T[] | (() => IterableIterator<T>));
12
+ /**
13
+ * Gets the number of items in the enumerable.
14
+ * @returns The number of items.
15
+ */
16
+ get length(): number;
17
+ /**
18
+ * Gets the iterator for the enumerable.
19
+ * @yields The items.
20
+ */
21
+ [Symbol.iterator](): IterableIterator<T>;
22
+ /**
23
+ * Transforms each item within this iterator to an indexed pair, with each pair represented as an array.
24
+ * @returns An iterator of indexed pairs.
25
+ */
26
+ asIndexedPairs(): Enumerable<[number, T]>;
27
+ /**
28
+ * Returns an iterator with the first items dropped, up to the specified limit.
29
+ * @param limit The number of elements to drop from the start of the iteration.
30
+ * @returns An iterator of items after the limit.
31
+ */
32
+ drop(limit: number): Enumerable<T>;
33
+ /**
34
+ * Determines whether all items satisfy the specified predicate.
35
+ * @param predicate Function that determines whether each item fulfils the predicate.
36
+ * @returns `true` when all items satisfy the predicate; otherwise `false`.
37
+ */
38
+ every(predicate: (value: T) => boolean): boolean;
39
+ /**
40
+ * Returns an iterator of items that meet the specified predicate..
41
+ * @param predicate Function that determines which items to filter.
42
+ * @returns An iterator of filtered items.
43
+ */
44
+ filter(predicate: (value: T) => boolean): Enumerable<T>;
45
+ /**
46
+ * Finds the first item that satisfies the specified predicate.
47
+ * @param predicate Predicate to match items against.
48
+ * @returns The first item that satisfied the predicate; otherwise `undefined`.
49
+ */
50
+ find(predicate: (value: T) => boolean): T | undefined;
51
+ /**
52
+ * Finds the last item that satisfies the specified predicate.
53
+ * @param predicate Predicate to match items against.
54
+ * @returns The first item that satisfied the predicate; otherwise `undefined`.
55
+ */
56
+ findLast(predicate: (value: T) => boolean): T | undefined;
57
+ /**
58
+ * Returns an iterator containing items transformed using the specified mapper function.
59
+ * @param mapper Function responsible for transforming each item.
60
+ * @returns An iterator of transformed items.
61
+ */
62
+ flatMap<U>(mapper: (item: T) => IterableIterator<U>): Enumerable<U>;
63
+ /**
64
+ * Iterates over each item, and invokes the specified function.
65
+ * @param fn Function to invoke against each item.
66
+ */
67
+ forEach(fn: (item: T) => void): void;
68
+ /**
69
+ * Determines whether the search item exists in the collection exists.
70
+ * @param search Item to search for.
71
+ * @returns `true` when the item was found; otherwise `false`.
72
+ */
73
+ includes(search: T): boolean;
74
+ /**
75
+ * Returns an iterator of mapped items using the mapper function.
76
+ * @param mapper Function responsible for mapping the items.
77
+ * @returns An iterator of mapped items.
78
+ */
79
+ map<U>(mapper: (value: T) => U): Enumerable<U>;
80
+ /**
81
+ * Captures the underlying iterable, if it is not already captured, and gets the next item in the iterator.
82
+ * @param args Optional values to send to the generator.
83
+ * @returns An iterator result of the current iteration; when `done` is `false`, the current `value` is provided.
84
+ */
85
+ next(...args: [] | [undefined]): IteratorResult<T, T>;
86
+ /**
87
+ * Applies the accumulator function to each item, and returns the result.
88
+ * @param accumulator Function responsible for accumulating all items within the collection.
89
+ * @returns Result of accumulating each value.
90
+ */
91
+ reduce(accumulator: (previous: T, current: T) => T): T;
92
+ /**
93
+ * Applies the accumulator function to each item, and returns the result.
94
+ * @param accumulator Function responsible for accumulating all items within the collection.
95
+ * @param initial Initial value supplied to the accumulator.
96
+ * @returns Result of accumulating each value.
97
+ */
98
+ reduce<R>(accumulator: (previous: R, current: T) => R, initial: R): R;
99
+ /**
100
+ * Acts as if a `return` statement is inserted in the generator's body at the current suspended position.
101
+ *
102
+ * Please note, in the context of an {@link Enumerable}, calling {@link Enumerable.return} will clear the captured iterator,
103
+ * if there is one. Subsequent calls to {@link Enumerable.next} will result in re-capturing the underlying iterable, and
104
+ * yielding items from the beginning.
105
+ * @param value Value to return.
106
+ * @returns The value as an iterator result.
107
+ */
108
+ return?<TReturn>(value?: TReturn): IteratorResult<T, TReturn | undefined>;
109
+ /**
110
+ * Determines whether an item in the collection exists that satisfies the specified predicate.
111
+ * @param predicate Function used to search for an item.
112
+ * @returns `true` when the item was found; otherwise `false`.
113
+ */
114
+ some(predicate: (value: T) => boolean): boolean;
115
+ /**
116
+ * Returns an iterator with the items, from 0, up to the specified limit.
117
+ * @param limit Limit of items to take.
118
+ * @returns An iterator of items from 0 to the limit.
119
+ */
120
+ take(limit: number): Enumerable<T>;
121
+ /**
122
+ * Acts as if a `throw` statement is inserted in the generator's body at the current suspended position.
123
+ * @param e Error to throw.
124
+ */
125
+ throw?<TReturn>(e?: TReturn): IteratorResult<T, TReturn | undefined>;
126
+ /**
127
+ * Converts this iterator to an array.
128
+ * @returns The array of items from this iterator.
129
+ */
130
+ toArray(): T[];
131
+ /**
132
+ * Converts this iterator to serializable collection.
133
+ * @returns The serializable collection of items.
134
+ */
135
+ toJSON(): T[];
136
+ /**
137
+ * Converts this iterator to a string.
138
+ * @returns The string.
139
+ */
140
+ toString(): string;
141
+ }