@yitech/dash
v0.0.1
Published
Opinionated collection of common JavaScript / TypeScript utils by @chruit
Downloads
5
Readme
YiDash
一个精心设计的 JavaScript / TypeScript 工具函数集合,提供高质量的类型定义和实用功能,帮助你编写更简洁、更可靠的代码。
特性
- 🚀 全面的工具函数:涵盖数组、对象、字符串、函数、Promise、数学等常用操作
- 🔧 TypeScript 优先:提供完整的类型定义,享受类型安全的开发体验
- 📦 零依赖:除了少量必要的外部工具库外,几乎不依赖第三方包
- 🎯 简洁高效:每个函数都经过精心设计,保持简洁易用
- 🔄 树摇支持:sideEffects 设置为 false,支持打包工具的树摇优化
安装
# 使用 pnpm
pnpm add @yitech/dash
# 使用 npm
npm install @yitech/dash
# 使用 yarn
yarn add @yitech/dash使用示例
基本用法
import { p, range, shuffle, throttle } from '@yitech/dash'
// 使用 p 管理并发 promise
const results = await p([1, 2, 3, 4, 5])
.map(async i => await someAsyncOperation(i))
.filter(async i => await someFilterCondition(i))
// 生成数字范围数组
const numbers = range(1, 10)
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
// 打乱数组
const shuffled = shuffle([1, 2, 3, 4, 5])
// 创建节流函数
const throttledFn = throttle(1000, () => {
// 每秒最多执行一次
})模块概览
数组操作 (array.ts)
import { flattenArrayable, last, partition, sample, toArray } from '@yitech/dash'
// 转换为数组
const arr = toArray(null) // []
const arr2 = toArray('hello') // ['hello']
// 展平数组
const flat = flattenArrayable([1, [2, 3]]) // [1, 2, 3]
// 分区数组
const [evens, odds] = partition([1, 2, 3, 4], i => i % 2 === 0)
// 获取最后一个元素
const lastItem = last([1, 2, 3]) // 3
// 随机采样
const randomItems = sample([1, 2, 3, 4, 5], 2) // 随机两个元素对象操作 (object.ts)
import { deepMerge, objectId, objectMap, objectPick } from '@yitech/dash'
// 映射对象键值
const mapped = objectMap({ a: 1, b: 2 }, (k, v) => [k.toUpperCase(), v * 2])
// { A: 2, B: 4 }
// 深度合并对象
const merged = deepMerge({ a: { x: 1 } }, { a: { y: 2 } })
// { a: { x: 1, y: 2 } }
// 提取对象属性
const picked = objectPick({ a: 1, b: 2, c: 3 }, ['a', 'c'])
// { a: 1, c: 3 }
// 获取对象唯一标识符
const id = objectId({}) // 唯一字符串字符串操作 (string.ts)
import { capitalize, randomStr, template, unindent } from '@yitech/dash'
// 字符串模板
const result = template('Hello {name}!', { name: 'World' })
// 'Hello World!'
// 首字母大写
const capitalized = capitalize('hello world') // 'Hello world'
// 移除缩进
const unindented = unindent`
function example() {
return true
}
`
// 移除了公共缩进的字符串
// 生成随机字符串
const random = randomStr(10) // 10位随机字符串Promise 工具 (p.ts, promise.ts)
import { createSingletonPromise, p, sleep } from '@yitech/dash'
// Promise 链式操作
await p([1, 2, 3])
.map(async i => i * 2)
.filter(i => i > 3)
// [4, 6]
// 延迟执行
await sleep(1000) // 等待1秒
// 创建单例 Promise
const singleton = createSingletonPromise(async () => {
// 只会执行一次的异步操作
})
await singleton()
await singleton() // 返回缓存的结果类型检查 (is.ts, guards.ts)
import { isObject, isString, notNullish } from '@yitech/dash'
// 类型检查
isObject({}) // true
isString('hello') // true
// 类型守卫
const items = [1, null, 2, undefined, 3]
const validItems = items.filter(notNullish) // [1, 2, 3]核心功能
p - Promise 管理工具
p 是一个强大的 Promise 管理工具,允许你以链式方式处理多个 Promise,支持 map、filter、reduce 等操作,还可以限制并发数量。
import { p } from '@yitech/dash'
// 基本用法
await p([1, 2, 3, 4, 5])
.map(async i => await multiply(i, 3))
.filter(async i => await isEven(i))
// [6, 12]
// 收集和延迟解析
const pInstance = p()
pInstance.add(promiseTask1)
pInstance.add(promiseTask2)
await someOtherTasks()
await pInstance // 等待所有添加的任务完成
// 限制并发
await p(tasks, { concurrency: 5 }) // 最多同时执行5个任务API 参考
开发
# 安装依赖
pnpm install
# 类型检查
pnpm typecheck
# 运行测试
pnpm test
# 构建项目
pnpm build
# 代码检查
pnpm lint许可证
MIT © 2025 Tony Chen [email protected]""
