@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
许可证
这个项目根据麻省理工学院的许可证获得许可。 见 自由主义 文档以获取更多信息.