npm install @elgato/utils
Creates a new function that implements Symbol.dispose
and accepts a disposer function. The disposer function is called when the new function is disposed.
import { deferredDisposable } from "@elgato/utils";
const cleanup = () => console.log("Hello world")
using (deferredDisposable(cleanup)) {
// ...
}
// "Hello world"
An strongly-typed event emitter that enables the listening for, and emitting of, events; supported in browser and Node.js environments.
import { EventEmitter } from "@elgato/utils";
type EventMap = {
created: [name: string];
};
const emitter = new EventEmitter<EventMap>();
emitter.on("created", (name: string) => {
// ...
});
Type that represents an object within JSON.
import { type JsonObject } from "@elgato/utils";
const obj: JsonObject = {
name: "Elgato",
};
Type that represents a primitive within JSON, for example a string or number.
import { type JsonPrimitive } from "@elgato/utils";
const value: JsonPrimitive = "Elgato";
Type that represents a valid JSON value, for example an object, array, or primitive.
import { type JsonValue } from "@elgato/utils";
const value: JsonValue = ["Hello", "World"];
Provides a read-only iterable collection of items that also acts as a partial polyfill for iterator helpers.
import { Enumerable } from "@elgato/utils";
const items = new Enumerable(["One", "Two", "Three", "Four"]);
items
.drop(1) // Drop "One"
.take(2); // Take "Two" & "Three"
Polyfilled iterator helpers:
Symbol.iterator
asIndexedPairs()
drop(limit)
every(predicate)
filter(predicate)
find(predicate)
findLast(predicate)
flapMap(mapper)
forEach(fn)
includes(search)
map(mapper)
reduce(accumulator, initial)
some(predicate)
take(limit)
toArray()
Object that wraps a lazily instantiated value, similar to C# Lazy<T>
.
import { Lazy } from "@elgato/utils";
const lazy = new Lazy(() => "Hello world");
lazy.value; // "Hello world";
Gets the value at the specified (deep) path.
import { get } from "@elgato/utils";
const obj = {
name: "Elgato",
};
get(obj, "name"); // Elgato
Sets the value at the specified (deep) path.
import { get } from "@elgato/utils";
const obj = {
name: "Gato",
};
set(obj, "name", "Elgato"); // { name: "Elgato" }
Parses the value a truthy-boolean; the strings "0"
and "false"
are parsed as false
.
import { parseBoolean } from "@elgato/utils";
const a = parseBoolean(1); // true
const b = parseBoolean("false"); // false
Attempts to parse a value to a number; returns undefined
when parsing was unsuccessful.
import { parseNumber } from "@elgato/utils";
const number = parseNumber("13"); // 13
Function that returns an object that contains the promise, and two functions to resolve or reject it. Polyfill of Promise.withResolvers().
import { withResolvers } "@elgato/utils";
const { promise, resolve, reject } = withResolvers<string>();
Wraps a function; subsequent invocations of the wrapped function made within the specified delay are debounced to prevent multiple calls.
import { debounce } from "@elgato/utils";
const fn = debounce(() => console.log("Hello world"), 1000);
fn(); // Debounced
fn(); // "Hello world"