# @elgato/utils 中文文档

规范地址：https://npmcn.com/package/%40elgato/utils/

版本：0\.6\.2
npm 元数据更新时间：2026-10-29T14:38:11.584Z
中文译文更新时间：2026-11-13T12:24:24.415Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40elgato/utils
原文对照：https://npmcn.com/package/%40elgato/utils/?lang=original
项目主页：https://github.com/elgatosf/streamdeck/tree/main/packages/utils#readme
源码仓库：https://github.com/elgatosf/streamdeck

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：119 kB，93 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2025-06-02T12:59:26.546Z
- 最新版本发布：2026-10-29T14:38:09.922Z
- 近 30 天下载量：72796（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：15（近 12 个月 7 个）

## 中文简介

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

## 安装

```sh
npm install @elgato/utils
```

也可以使用：`pnpm add @elgato/utils`、`yarn add @elgato/utils`、`bun add @elgato/utils`

## README

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

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

[![@elgato/utils npm package](https://img.shields.io/npm/v/%40elgato/utils?logo=npm&logoColor=white)](https://www.npmjs.com/package/@elgato/utils) [![Elgato homepage](https://img.shields.io/badge/Elgato-3431cf?labelColor=grey&logo=Elgato)](https://elgato.com/)

## 安装

```
npm install @elgato/utils
```

## 可处置

### `deferredDisposable`

创建执行的新函数 `Symbol.dispose` 并接受处置者功能。 处置器功能在处置新功能时被调用.

```js
import { deferredDisposable } from "@elgato/utils";

{
    using deferredDisposable(() => console.log("Hello world"));
    // ...
}

// "Hello world"
```

### `DisposableStack`

由单一处置者控制。 部分多填 TC39 明确资源管理 `DisposableStack`。 。 。 .

```js
import { DisposableStack } from "@elgato/utils";

{
    using stack = new DisposableStack();
    stack.defer(() => "Hello");
    stack.defer(() => "world");
    // ...
}

// "Hello"
// "world"
```

## 活动

### `EventEmitter`

一种强类型的事件发射器,它能够监听和释放事件;在浏览器和Node.js环境中支持.

```ts
import { EventEmitter } from "@elgato/utils";

type EventMap = {
    created: [name: string];
};

const emitter = new EventEmitter<EventMap>();
emitter.on("created", (name: string) => {
    // ...
});
```

## 贾森

### `JsonObject`

代表 JSON 内部对象的类型.

```ts
import { type JsonObject } from "@elgato/utils";

const obj: JsonObject = {
    name: "Elgato",
};
```

### `JsonPrimitive`

代表JSON内部原始的类型,例如字符串或数字.

```ts
import { type JsonPrimitive } from "@elgato/utils";

const value: JsonPrimitive = "Elgato";
```

### `JsonValue`

代表有效JSON值的类型,例如对象,阵列,或原始.

```ts
import { type JsonValue } from "@elgato/utils";

const value: JsonValue = ["Hello", "World"];
```

## 杂项

### `Enumerable`

提供仅可读的可移动项集合,这些项还起到部分多填充器的作用,供执行器帮助者使用.

```js
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>`](https://learn.microsoft.com/en-us/dotnet/framework/performance/lazy-initialization)。 。 。 .

```js
import { Lazy } from "@elgato/utils";

const lazy = new Lazy(() => "Hello world");
lazy.value; // "Hello world";
```

## 对象

### `get(source, path)`

获取指定(深)路径的值.

```js
import { get } from "@elgato/utils";

const obj = {
    name: "Elgato",
};

get(obj, "name"); // Elgato
```

### `set(target, path, value)`

将值设置在指定的(深)路径上.

```js
import { get } from "@elgato/utils";

const obj = {
    name: "Gato",
};

set(obj, "name", "Elgato"); // { name: "Elgato" }
```

## 解析器

### `parseBoolean(value)`

解析一个 truthy-boole; 字符串 `"0"` 财务报告和审定财务报表 `"false"` 解析为 `false`。 。 。 .

```js
import { parseBoolean } from "@elgato/utils";

const a = parseBoolean(1); // true
const b = parseBoolean("false"); // false
```

### `parseNumber(value)`

试图分析一个数字的值; 返回 `undefined` 当解析失败时.

```js
import { parseNumber } from "@elgato/utils";

const number = parseNumber("13"); // 13
```

## 进程

### `getProcesses()`

以跨平台的方式获得运行过程.

```js
import { getProcesses } from "@elgato/utils";

const processes = await getProcesses();
console.log(processes);
```

## 承诺

### `withResolvers()`

函数返回包含承诺的对象,以及解决或拒绝它的两个函数。 多元填充量 [承诺。 与 Resolvers ()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers)。 。 。 .

```js
import { withResolvers } from "@elgato/utils";

const { promise, resolve, reject } = withResolvers<string>();
```

## 字符串

### `format(format, ...args)`

格式化指定的字符串,用相关的参数替换占位符.

```js
import { format } from "@elgato/utils";

format("Hello {0}, from {1}", "world", "Elgato"); // Hello world, from Elgato
```

## 计时器

### `debounce(fn, delay)`

环绕一个函数;之后在指定的延迟中被环绕函数的引用被解开来防止多通调用.

```js
import { debounce } from "@elgato/utils";

const fn = debounce(() => console.log("Hello world"), 1000);

fn(); // Debounced
fn(); // "Hello world"
```

## 常见问题

### @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 和提交记录判断。

