# @corentinth/chisels 中文文档

规范地址：https://npmcn.com/package/%40corentinth/chisels/

版本：2\.1\.0
npm 元数据更新时间：2025-10-26T16:28:16.928Z
中文译文更新时间：2026-11-10T19:04:04.521Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40corentinth/chisels
原文对照：https://npmcn.com/package/%40corentinth/chisels/?lang=original
项目主页：https://github.com/CorentinTh/chisels#readme
源码仓库：https://github.com/CorentinTh/chisels

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

- 包体积（解压后）：17.1 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2024-09-08T12:25:47.356Z
- 最新版本发布：2025-10-26T16:28:16.517Z
- 近 30 天下载量：5772（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 0 个）

## 中文简介

收集JavaScript和TypeScript,轻量级和可树摇动的公用设备.

## 安装

```sh
npm install @corentinth/chisels
```

也可以使用：`pnpm add @corentinth/chisels`、`yarn add @corentinth/chisels`、`bun add @corentinth/chisels`

## README

# Chisels - JS/TS公用事业和类型

收集可重复使用的JS/TS公用事业和类型的意见.

-   **可树动摇的ES模块**: 只导入您需要的东西
-   **完全测试**:全面覆盖的严格单位测试
-   **无依赖性**: 无运行时间依赖性
-   **类型脚本支持**: 在 TypeScript 中以完整类型定义写入
-   **轻巧**: 小捆绑大小
-   **现代**: 用现代JS/TS语法编写
-   **命名参数**: 函数, 最好是命名参数

## 安装

```bash
# Using npm
npm install @corentinth/chisels

# Using yarn
yarn add @corentinth/chisels

# Using pnpm
pnpm add @corentinth/chisels
```

## 使用量

导入示例 `safely` 函数 :

```ts
// Using ES6 import
import { safely } from '@corentinth/chisels';

const [result, error] = safely(mayThrowError);

console.log({ result, error });
```

## API 密码

### `safelySync`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/safely.ts#L42)

安全执行一个函数,并返回一个有结果和任何错误的拖拉.

```typescript
const [result, error] = safelySync(myFunction);

if (error) {
 console.error(error);
}

console.log(result);
```

### `safely`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/safely.ts#L19)

安全地执行 aync 函数或承诺,并返回一拖拉和任何错误.

```typescript
const [result, error] = await safely(myFunction);

if (error) {
 console.error(error);
}

console.log(result);
```

### `formatBytes`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/bytes.ts#L18)

将若干字节格式化为可读字符串.

```typescript
const formatted = formatBytes({ bytes: 4194304 });

console.log(formatted); // 4 MiB
```

### `castError`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/errors.ts#L17)

将未知值刻入错误.

```typescript
 try {
     // ...
 } catch (rawError) {
   const error = castError(rawError);

   // Do something with a proper Error instance
 }
```

### `joinUrlPaths`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/url.ts#L13)

加入 URL 部件并修剪斜线 .

```typescript
const url = joinUrlPaths('/part1/', '/part2/', 'part3', 'part4/');

console.log(url); // 'part1/part2/part3/part4'
```

### `buildUrl`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/url.ts#L27)

围绕 URL 构建器的功能包来从基址和可选路径构建 URL 字符串,查询参数和散列.

```typescript
const url = buildUrl({ baseUrl: 'https://example.com', path: 'foo', queryParams: { a: '1', b: '2' }, hash: 'hash' });

console.log(url); // 'https://example.com/foo?a=1&b=2#hash'
```

### `injectArguments`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/injection.ts#L21)

将参数注入一组函数。 可用于寄存器、服务等的编目.

```typescript
 const functions = {
  getUser: ({ userId, db }) => db.users.find({ id: userId }),
  removeUser: ({ userId, db }) => db.users.remove({ id: userId }),
 };

 const { getUser, removeUser } = injectArguments(functions, { db });

 getUser({ userId: 1 });
 removeUser({ userId: 1 });
```

### `memoizeOnce`

函数 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/memo.ts#L13)

此函数具有返回值并返回缓存第一次调用结果的新函数的函数. 基本上是一种毫无争议的回忆.

```typescript
const getCwd = memoizeOnce(() => process.cwd());

// process.cwd() is only called once
console.log(getCwd());
console.log(getCwd());
```

### `PartialBy`

类型别名 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/types.ts#L17)

使 T 的一些属性可选

```typescript
type User = {
  id: number;
  name: string;
  email: string;
};

type PartialUser = PartialBy<User, 'email' | 'name'>;

const user: PartialUser = { id: 1 };
```

### `Expand`

类型别名 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/types.ts#L22)

为更好的 IDE 支持而平整对象类型

### `Dictionary`

类型别名 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/types.ts#L35)

记录 < 字符串, T > 别名

```typescript
const dictionary: Dictionary<number> = {
  a: 1,
  b: 2,
};
```

### `DeepPartial`

类型别名 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/types.ts#L40)

使 T 的所有属性递归

### `Subtract`

类型别名 - [见来源](https://npmcn.com/package/%40corentinth/chisels/src/types.ts#L64)

不包括在 U 中的 T 属性

```typescript
type User = {
  id: number;
  name: string;
  email: string;
};

type WithId = {
  id: number;
}

type UserWithoutId = Subtract<User, WithId>;
```

## 许可证

这个项目根据麻省理工学院的许可证获得许可。 见 [自由主义](https://npmcn.com/package/%40corentinth/chisels/LICENSE) 文档以获取更多信息.

## 信用和鸣谢

这个项目由 . . [科伦丁·托马斯塞特](https://corentin.tech/)。 。 。 .

## 常见问题

### @corentinth/chisels 是什么？

收集JavaScript和TypeScript,轻量级和可树摇动的公用设备\.

### 如何安装 @corentinth/chisels？

在项目目录运行 npm install @corentinth/chisels，或使用 pnpm add / yarn add / bun add。

### @corentinth/chisels 支持 TypeScript 吗？

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

### @corentinth/chisels 支持 ESM 还是 CommonJS？

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

### @corentinth/chisels 可以商用吗？

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

### @corentinth/chisels 的下载量有多少？

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

### @corentinth/chisels 还在维护吗？

最新版本 2\.1\.0 发布于 2025/10/27（1 年前）；至今共发布 14 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/09/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

