@corentinth/friendly-ids

中文文档
0.0.1发布于 2025/01/23MITTypeScriptESM + CJS零依赖近 30 天下载 1972

人类友好的ID生成器

中文使用文档
文档目录10 节
  1. 特征
  2. 安装
  3. 使用量
  4. 生成一个友好的ID
  5. 创建自定义生成器
  6. 数据集
  7. 捐款
  8. 测试
  9. 许可证
  10. 信用和鸣谢

@corentinth/ 友好的标识

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

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 或 纱线 :

pnpm install @corentinth/friendly-ids

npm install @corentinth/friendly-ids

yarn add @corentinth/friendly-ids

使用量

生成一个友好的ID

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

const id = generateId();

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

创建自定义生成器

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

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

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

带有自定义分隔符 :

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

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

console.log(id);
// brave_horse_537

所有选项 :

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 财务报告和审定财务报表 animals。 。 。 . 您可以使用它们来定制您的生成器 :

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

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

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

捐款

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

测试

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

# one shot
pnpm run test

# watch mode
pnpm run test:watch

许可证

这个项目根据麻省理工学院的许可证获得许可。 见 自由主义 文档以获取更多信息.

信用和鸣谢

这个项目由 . . 科伦丁·托马斯塞特。 。 。 . 如果你认为这个项目有帮助,请考虑 支持我的工作。 。 。 .

版本与发布记录

@corentinth/friendly-ids 共发布 1 个版本,首次发布于 2025/01/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2025/01/231.8 年前

关于 @corentinth/friendly-ids 的常见问题

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