# @corentinth/friendly-ids 中文文档

规范地址：https://npmcn.com/package/%40corentinth/friendly-ids/

版本：0\.0\.1
npm 元数据更新时间：2025-01-23T14:27:07.782Z
中文译文更新时间：2026-11-16T00:24:39.115Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：60.1 kB，10 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2025-01-23T14:27:07.221Z
- 最新版本发布：2025-01-23T14:27:07.505Z
- 近 30 天下载量：1972（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

人类友好的ID生成器

## 安装

```sh
npm install @corentinth/friendly-ids
```

也可以使用：`pnpm add @corentinth/friendly-ids`、`yarn add @corentinth/friendly-ids`、`bun add @corentinth/friendly-ids`

## README

# @corentinth/ 友好的标识

`@corentinth/friendly-ids` 是一个为您的项目生成人读取的 ID 的 lib。 它的设计简单快活,易于使用.

```javascript
import { generateId } from '@corentinth/friendly-ids';

const id = generateId();

console.log(id);
// fierce-zebra-456
```

## 特征

-   **78 648 000个组合**:默认生成器最多可生成78,648,000个独特的ID(348个名词×226个形容词×1000个数字).
-   **可自定义**:您可以用自定义分隔符和随机整数生成器创建自己的生成器.
-   **无依赖关系**: lib没有依附关系,且重量轻.
-   **类型脚本支持**: lib是用 TypeScript 写的,并带有类型定义.
-   **可动摇的树**: lib是可树摇动并只包含您使用的代码.
-   **数据集**:每个单词都是严格的字母,没有特殊的字符.

## 安装

要安装软件包, 请使用 npm 或 纱线 :

```bash
pnpm install @corentinth/friendly-ids

npm install @corentinth/friendly-ids

yarn add @corentinth/friendly-ids
```

## 使用量

### 生成一个友好的ID

```javascript
import { generateId } from '@corentinth/friendly-ids';

const id = generateId();

console.log(id);
// fierce-zebra-456
```

### 创建自定义生成器

```javascript
import { createIdGenerator } from '@corentinth/friendly-ids';

const generateId = createIdGenerator();
const id = generateId();

console.log(id);
// fierce-zebra-456
```

带有自定义分隔符 :

```javascript
import { createIdGenerator } from '@corentinth/friendly-ids';

const generateId = createIdGenerator({ separator: '_' });
const id = generateId();

console.log(id);
// brave_horse_537
```

所有选项 :

```javascript
import { createIdGenerator, adjectives } from '@corentinth/friendly-ids';

const generateId = createIdGenerator({
  separator: '~',
  // A list of chunks generators, each generator is a function that provides random helpers and expects a string or number as a return value
  chunks: [
    ({ getRandomItem }) => getRandomItem(adjectives),
    ({ getRandomItem }) => getRandomItem(['foo', 'bar', 'baz']),
    ({ getRandomInt }) => getRandomInt({ min: 50, max: 100 }),
    ({ getRandomItem }) => getRandomItem(['a', 'b', 'c']),
  ],
  // A custom implementation of the random int generator, by default based on Math.random
  getRandomInt: ({ min, max }) => /* your implementation */,
});

const id = generateId();

console.log(id);
// fierce~foo~78~b
```

### 数据集

Lib有两个数据集: [`adjectives`](https://npmcn.com/package/%40corentinth/friendly-ids/src/data/adjectives.ts) 财务报告和审定财务报表 [`animals`](https://npmcn.com/package/%40corentinth/friendly-ids/src/data/animals.ts)。 。 。 . 您可以使用它们来定制您的生成器 :

```javascript
import { animals, adjectives } from '@corentinth/friendly-ids';

console.log(adjectives);
// [ 'adaptable', 'adventurous', 'affable', 'affectionate', 'afraid', ... ]

console.log(animals);
// [ 'albatross', 'alligator', 'alpaca', 'ant', 'anteater', ... ]
```

## 捐款

欢迎捐款! 随意解题或提交拉力请求.

## 测试

您可以使用以下命令进行测试:

```bash
# one shot
pnpm run test

# watch mode
pnpm run test:watch
```

## 许可证

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

## 信用和鸣谢

这个项目由 . . [科伦丁·托马斯塞特](https://corentin.tech/)。 。 。 . 如果你认为这个项目有帮助,请考虑 [支持我的工作](https://buymeacoffee.com/cthmsst)。 。 。 .

## 常见问题

### @corentinth/friendly-ids 是什么？

人类友好的ID生成器

### 如何安装 @corentinth/friendly-ids？

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

### @corentinth/friendly-ids 支持 TypeScript 吗？

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

### @corentinth/friendly-ids 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @corentinth/friendly-ids 可以商用吗？

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

### @corentinth/friendly-ids 的下载量有多少？

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

### @corentinth/friendly-ids 还在维护吗？

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

