Skip to content

yangtianxia/txjs

Repository files navigation

txjs

一套简洁实用的 TypeScript 函数库,包括数据类型校验、CSS 命名、常用工具函数等。

Packages

版本 说明
@txjs/bool npm 数据类型的校验工具
@txjs/bem npm BEM 规范的 CSS 类名生成
@txjs/shared npm 实用工具集合
@txjs/types npm 常用类型定义

@txjs/bool

数据类型的校验工具,支持邮箱、手机号、URL、深度相等比较等。

安装:

pnpm add @txjs/bool

函数列表:

函数 说明
is 通用类型验证
isArray 是否为数组
isBoolean 是否为布尔值
isFunction 是否为函数
isNumber 是否为数字(不含 NaN)
isString 是否为字符串
isObject 是否为对象
isNil 是否为 null 或 undefined
isEmail 是否为邮箱格式
isPhone 是否为中国手机号
isUrl 是否为 URL
isEqual 两值是否相等(深度比较)

示例:

import { isEmail, isPhone, isEqual } from '@txjs/bool'

isEmail('user@example.com') // => true
isPhone('13812345678') // => true
isEqual({ a: 1 }, { a: 1 }) // => true

更多函数 →


@txjs/bem

遵循 BEM 规范的 CSS 类名生成工具,支持 CSS Modules。

安装:

pnpm add @txjs/bem

示例:

import { BEM } from '@txjs/bem'

const [name, bem] = BEM('button')

bem()
// => 'button'

bem('text', { small: true })
// => 'button__text button__text--small'

// CSS Modules 模式
const [name, bem] = BEM('button', styles)
bem('text')
// => 'button__text_abc123'

更多用法 →


@txjs/shared

实用工具集合,包含深拷贝、对象操作、字符串转换、拦截器等。

安装:

pnpm add @txjs/shared

函数列表:

函数 说明
cloneDeep 深度克隆
omit 删除对象指定属性
pick 选取对象指定属性
camelize 字符串转驼峰
camelToKebab 驼峰转 kebab
clamp 限制数值范围
chunk 数组分块
toArray 转数组
toFixed 保留小数位
noop 空函数

示例:

import { cloneDeep, omit, pick } from '@txjs/shared'

// 深拷贝
cloneDeep({ a: { b: 1 } })

// 删除属性
omit({ a: 1, b: 2, c: 3 }, ['c'])
// => { a: 1, b: 2 }

// 选取属性
pick({ a: 1, b: 2, c: 3 }, ['a', 'c'])
// => { a: 1, c: 3 }

更多函数 →


@txjs/types

常用 TypeScript 类型定义。

安装:

pnpm add @txjs/types

开发

环境要求

  • Node.js >= 18.12
  • pnpm >= 9.x

安装依赖

pnpm install

构建所有包

pnpm build

运行测试

pnpm test

License

MIT © yangtianxia

About

常用的前端开发工具,有TS类型定义、数据验证、常用方法、数据定义、CSS类命名(BEM)、修复版数学计算等。

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages