一个功能丰富的 JavaScript/TypeScript 工具库,提供常用的实用工具函数和类型扩展。
npm install zjh-utils
# 或
yarn add zjh-utils
# 或
pnpm add zjh-utils
import {
typeExpand,
arrayUtils,
stringUtils,
numberUtils,
objectUtils,
timeUtils,
typeUtils,
testUtils,
throttle,
debounce,
qs,
Base64
} from 'zjh-utils'
// 扩展原生类型
typeExpand(['string', 'number', 'object'])
// 使用工具函数
const randomId = arrayUtils.guid()
const formattedDate = timeUtils.formatDate(new Date())
const {
arrayUtils,
stringUtils,
numberUtils
} = require('zjh-utils')
import 'zjh-utils/global'
// 现在可以直接使用扩展的原型方法
typeExpand)扩展 JavaScript 原生类型的原型方法:
import { typeExpand } from 'zjh-utils'
// 扩展字符串、数字、对象的原型方法
typeExpand(['string', 'number', 'object'])
arrayUtils)提供数组相关的实用方法:
import { arrayUtils } from 'zjh-utils'
// 生成 GUID
const id = arrayUtils.guid()
// 数组去重
const uniqueArray = arrayUtils.unique([1, 2, 2, 3])
stringUtils)字符串处理相关工具:
import { stringUtils } from 'zjh-utils'
// 字符串扩展方法(需要先调用 typeExpand(['string']))
'hello'.capitalize() // 'Hello'
'hello world'.camelCase() // 'helloWorld'
numberUtils)数字处理相关工具:
import { numberUtils } from 'zjh-utils'
// 数字扩展方法(需要先调用 typeExpand(['number']))
(123.456).toFixed(2) // '123.46'
(1000).format() // '1,000'
objectUtils)对象操作相关工具:
import { objectUtils } from 'zjh-utils'
// 深拷贝
const cloned = objectUtils.deepClone(obj)
// 对象合并
const merged = objectUtils.merge(obj1, obj2)
timeUtils)时间处理相关工具:
import { timeUtils } from 'zjh-utils'
// 格式化日期
const formatted = timeUtils.formatDate(new Date(), 'YYYY-MM-DD')
// 获取相对时间
const relative = timeUtils.fromNow(new Date())
typeUtils)类型判断相关工具:
import { typeUtils } from 'zjh-utils'
// 类型判断
typeUtils.isString('hello') // true
typeUtils.isArray([1, 2, 3]) // true
typeUtils.isObject({}) // true
dictTransform)import { dictTransform } from 'zjh-utils'
const dict = {
'key1': 'value1',
'key2': 'value2'
}
const transformed = dictTransform(dict, (key, value) => ({
label: key,
value: value
}))
promiseIntercept)import { promiseIntercept } from 'zjh-utils'
const intercepted = promiseIntercept(promise, {
onResolve: (value) => console.log('Resolved:', value),
onReject: (error) => console.log('Rejected:', error)
})
random)import { random } from 'zjh-utils'
const randomNum = random(1, 100) // 1-100 之间的随机数
const randomString = random.string(10) // 10位随机字符串
import { Base64 } from 'zjh-utils'
const encoded = Base64.encode('Hello World')
const decoded = Base64.decode(encoded)
qs)import { qs } from 'zjh-utils'
// 解析查询字符串
const parsed = qs.parse('foo=bar&baz=qux')
// 生成查询字符串
const queryString = qs.stringify({ foo: 'bar', baz: 'qux' })
throttle/debounce)import { throttle, debounce } from 'zjh-utils'
// 节流函数
const throttledFn = throttle(() => {
console.log('Throttled function called')
}, 1000)
// 防抖函数
const debouncedFn = debounce(() => {
console.log('Debounced function called')
}, 300)
pnpm install
# 构建
pnpm build
# 开发模式(监听文件变化)
pnpm dev
# 类型检查
pnpm typecheck
# 运行测试
pnpm test
# 代码检查
pnpm lint
# 自动修复代码问题
pnpm lint-fix
# 生成文档
pnpm doc
src/
├── index.ts # 主入口文件
├── type.ts # 类型定义
├── array.ts # 数组工具
├── string.ts # 字符串工具
├── number.ts # 数字工具
├── object.ts # 对象工具
├── time.ts # 时间工具
├── test.ts # 测试工具
├── vendor.ts # 第三方库集成
├── tool/ # 工具函数
│ ├── guid.ts # GUID 生成
│ ├── random.ts # 随机数生成
│ ├── to.ts # 类型转换
│ ├── promise-intercept.ts # Promise 拦截器
│ └── dict-transform.ts # 字典转换
└── *-expand.ts # 类型扩展模块
MIT License
欢迎提交 Issue 和 Pull Request!
如果这个项目对您有帮助,请给它一个 ⭐️!
Generated using TypeDoc