安装
npm install @elgato/utils
可处置
deferredDisposable
创建执行的新函数 Symbol.dispose 并接受处置者功能。 处置器功能在处置新功能时被调用.
import { deferredDisposable } from "@elgato/utils";
{
using deferredDisposable(() => console.log("Hello world"));
// ...
}
// "Hello world"
DisposableStack
由单一处置者控制。 部分多填 TC39 明确资源管理 DisposableStack。 。 。 .
import { DisposableStack } from "@elgato/utils";
{
using stack = new DisposableStack();
stack.defer(() => "Hello");
stack.defer(() => "world");
// ...
}
// "Hello"
// "world"
活动
EventEmitter
一种强类型的事件发射器,它能够监听和释放事件;在浏览器和Node.js环境中支持.
import { EventEmitter } from "@elgato/utils";
type EventMap = {
created: [name: string];
};
const emitter = new EventEmitter<EventMap>();
emitter.on("created", (name: string) => {
// ...
});
贾森
JsonObject
代表 JSON 内部对象的类型.
import { type JsonObject } from "@elgato/utils";
const obj: JsonObject = {
name: "Elgato",
};
JsonPrimitive
代表JSON内部原始的类型,例如字符串或数字.
import { type JsonPrimitive } from "@elgato/utils";
const value: JsonPrimitive = "Elgato";
JsonValue
代表有效JSON值的类型,例如对象,阵列,或原始.
import { type JsonValue } from "@elgato/utils";
const value: JsonValue = ["Hello", "World"];
杂项
Enumerable
提供仅可读的可移动项集合,这些项还起到部分多填充器的作用,供执行器帮助者使用.
import { Enumerable } from "@elgato/utils";
const items = new Enumerable(["One", "Two", "Three", "Four"]);
items
.drop(1) // Drop "One"
.take(2); // Take "Two" & "Three"
多填充的传动器助手 :
Symbol.iteratorasIndexedPairs()drop(limit)every(predicate)filter(predicate)find(predicate)findLast(predicate)flatMap(mapper)forEach(fn)includes(search)map(mapper)reduce(accumulator, initial)some(predicate)take(limit)toArray()
Lazy
包裹懒惰即时值的对象,类似于 C# Lazy<T>。 。 。 .
import { Lazy } from "@elgato/utils";
const lazy = new Lazy(() => "Hello world");
lazy.value; // "Hello world";
对象
get(source, path)
获取指定(深)路径的值.
import { get } from "@elgato/utils";
const obj = {
name: "Elgato",
};
get(obj, "name"); // Elgato
set(target, path, value)
将值设置在指定的(深)路径上.
import { get } from "@elgato/utils";
const obj = {
name: "Gato",
};
set(obj, "name", "Elgato"); // { name: "Elgato" }
解析器
parseBoolean(value)
解析一个 truthy-boole; 字符串 "0" 财务报告和审定财务报表 "false" 解析为 false。 。 。 .
import { parseBoolean } from "@elgato/utils";
const a = parseBoolean(1); // true
const b = parseBoolean("false"); // false
parseNumber(value)
试图分析一个数字的值; 返回 undefined 当解析失败时.
import { parseNumber } from "@elgato/utils";
const number = parseNumber("13"); // 13
进程
getProcesses()
以跨平台的方式获得运行过程.
import { getProcesses } from "@elgato/utils";
const processes = await getProcesses();
console.log(processes);
承诺
withResolvers()
函数返回包含承诺的对象,以及解决或拒绝它的两个函数。 多元填充量 承诺。 与 Resolvers ()。 。 。 .
import { withResolvers } from "@elgato/utils";
const { promise, resolve, reject } = withResolvers<string>();
字符串
format(format, ...args)
格式化指定的字符串,用相关的参数替换占位符.
import { format } from "@elgato/utils";
format("Hello {0}, from {1}", "world", "Elgato"); // Hello world, from Elgato
计时器
debounce(fn, delay)
环绕一个函数;之后在指定的延迟中被环绕函数的引用被解开来防止多通调用.
import { debounce } from "@elgato/utils";
const fn = debounce(() => console.log("Hello world"), 1000);
fn(); // Debounced
fn(); // "Hello world"