@elgato/utils

中文文档
0.6.2发布于 2026/10/29MITTypeScriptES Module构建来源已验证近 30 天下载 7.3万

整个埃尔加托生态系统使用的公用事业.

中文使用文档
文档目录27 节
  1. 安装
  2. 可处置
  3. deferredDisposable
  4. DisposableStack
  5. 活动
  6. EventEmitter
  7. 贾森
  8. JsonObject
  9. JsonPrimitive
  10. JsonValue
  11. 杂项
  12. Enumerable
  13. Lazy
  14. 对象
  15. get(source, path)
  16. set(target, path, value)
  17. 解析器
  18. parseBoolean(value)
  19. parseNumber(value)
  20. 进程
  21. getProcesses()
  22. 承诺
  23. withResolvers()
  24. 字符串
  25. format(format, ...args)
  26. 计时器
  27. debounce(fn, delay)

@elgato/ utils (中文(简体) )

整个埃尔加托生态系统使用的公用事业.

@elgato/utils npm package Elgato homepage

安装

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.iterator
  • asIndexedPairs()
  • 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"

版本与发布记录

@elgato/utils 共发布 15 个版本,首次发布于 2025/06/02;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.2latest2026/10/2915 天前
0.6.12026/10/091 个月前
0.6.02026/08/172 个月前
0.5.02026/08/113 个月前
0.4.52026/04/176 个月前
0.4.42026/03/078 个月前

关于 @elgato/utils 的常见问题

@elgato/utils 是什么?

整个埃尔加托生态系统使用的公用事业.

如何安装 @elgato/utils?

在项目目录运行 npm install @elgato/utils,或使用 pnpm add / yarn add / bun add。

@elgato/utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elgato/utils 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@elgato/utils 可以商用吗?

@elgato/utils 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elgato/utils 的下载量有多少?

根据 npm 官方下载统计,@elgato/utils 在 2026/10/14 – 2026/11/12 这 30 天内被下载 72,796 次,日均约 2,427 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elgato/utils 还在维护吗?

最新版本 0.6.2 发布于 2026/10/29(15 天前);至今共发布 15 个版本,其中过去 12 个月发布了 7 个;首次发布于 2025/06/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。