@chzky/std
v2.0.0
Published
[](https://jsr.io/@chzky/std)
Downloads
153
Readme
@chzky/std
标准库,提供常用工具函数、数据结构和算法实现。
模块
集合操作
- collect - 集合辅助函数(合并、提取、去重、打乱等)
异步工具
- async - 异步工具(防抖、节流、重试、并发池等)
算法
- algorithm - 算法实现(布隆过滤器、数组比较、采样、虚拟洗牌)
数学
- math - 数学函数
数据结构
- dataStructures - 数据结构
其他
- crypto - 密码学工具
- data - 数据处理
- encode - 编码转换
- extend - 扩展类型
- format - 格式化工具
- generator - 生成器集合
- middleware - 中间件
- rules - 校验规则
- time - 时间相关
安装
# Deno
deno add jsr:@chzky/std
# npm / pnpm / yarn
npm install @chzky/std
pnpm add @chzky/std
yarn add @chzky/std快速开始
集合操作 (collect)
import { merge, pick, unique, shuffle } from "@chzky/std";
const merged = merge({ a: 1 }, { b: 2 });
// { a: 1, b: 2 }
const picked = pick({ a: 1, b: 2, c: 3 }, ["a", "c"]);
// { a: 1, c: 3 }
const deduped = unique([1, 2, 2, 3, 3, 3]);
// [1, 2, 3]异步工具 (async)
import { debounce, retry, throttle, sleep } from "@chzky/std";
const debouncedFn = debounce((x: number) => console.log(x), 300);
debouncedFn(1);
debouncedFn(2);
const result = await retry(async () => {
return await fetchData();
}, { times: 3, delay: 1000 });
await sleep(1000);数学函数 (math)
import { percent, random, within } from "@chzky/std";
const p = percent(25, 100);
// 25
const n = random(1, 100);
// 42
const ok = within(50, 0, 100);
// true算法 (algorithm)
import { bloomFilter, diff, virtualShuffle } from "@chzky/std";
const filter = new BloomFilter<string>(1000, 0.01);
filter.add("hello");
filter.has("hello");
// true
const changes = diff([1, 2, 3], [1, 3, 4]);
// { added: [4], removed: [2], kept: [1] }校验规则 (rules)
import { isEmail, isPhone, isUrl } from "@chzky/std";
isEmail("[email protected]");
// true
isPhone("13800138000");
// true
isUrl("https://example.com");
// true格式化 (format)
import { formatBytes, formatDuration } from "@chzky/std";
formatBytes(1024 * 1024);
// "1 MB"
formatDuration(3661000);
// "1h 1m 1s"版本
与 @chzky/core 保持版本一致。
许可证
MIT
