@elgato/utils v0.2.0
@elgato/utils
Utilities used throughout the Elgato ecosystem.
Installation
npm install @elgato/utilsDisposables
deferredDisposable
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"Events
EventEmitter
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) => {
// ...
});JSON
JsonObject
Type that represents an object within JSON.
import { type JsonObject } from "@elgato/utils";
const obj: JsonObject = {
name: "Elgato",
};JsonPrimitive
Type that represents a primitive within JSON, for example a string or number.
import { type JsonPrimitive } from "@elgato/utils";
const value: JsonPrimitive = "Elgato";JsonValue
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"];Miscellaneous
Enumerable
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.iteratorasIndexedPairs()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()
Lazy
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";Objects
get(source, path)
Gets the value at the specified (deep) path.
import { get } from "@elgato/utils";
const obj = {
name: "Elgato",
};
get(obj, "name"); // Elgatoset(target, path, value)
Sets the value at the specified (deep) path.
import { get } from "@elgato/utils";
const obj = {
name: "Gato",
};
set(obj, "name", "Elgato"); // { name: "Elgato" }Parsers
parseBoolean(value)
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"); // falseparseNumber(value)
Attempts to parse a value to a number; returns undefined when parsing was unsuccessful.
import { parseNumber } from "@elgato/utils";
const number = parseNumber("13"); // 13Promises
withResolvers()
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>();Timers
debounce(fn, delay)
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"