Skip to content

明途公共 TypeScript 工具库,提供类型判断、浏览器工具、文件处理、数学计算、字符串处理、函数控制、下载、WebSocket、Worker、主题和国际化等能力。

安装

bash
pnpm add @mingto/tools

使用方式

typescript
import { add, getFileSuffix, isString } from '@mingto/tools'

isString('hello')
add(0.1, 0.2)
getFileSuffix('/demo/file.pdf')

默认导出提供国际化语言设置能力:

typescript
import tools from '@mingto/tools'

tools.locale('zh-cn')
tools.getLocale()

模块清单

array

API说明
chunkArray将数组按指定长度切分

browser

API说明
copyText复制文本到剪贴板
createAudioPermission创建音频播放权限辅助能力
getAbsoluteUrl获取绝对 URL
getBrowserFingerprint获取浏览器指纹
isAndroid判断 Android 环境
isIos判断 iOS 环境
isMobile判断移动端环境
isPc判断 PC 环境

core

API说明
beParsedAsNumber将值按数字解析
canBeParsedAsNumber判断值是否可解析为数字
checkArrayEmpty判断数组是否为空
checkEmpty判断值是否为空
checkEmptyNotZero判断值是否为空但保留数字 0
checkObjectEmpty判断对象是否为空
isArray判断数组
isBoolean判断布尔值
isFunction判断函数
isNullOrUndefined判断 nullundefined
isNumber判断数字
isObject判断对象
isString判断字符串
isUndefined判断 undefined

file

API说明
batchDownloadFile批量下载文件
getFileIcon获取文件图标
getFileName获取文件名
getFilePathNotQuery获取去除 query 的文件路径
getFileQuery获取文件路径 query
getFileSuffix获取文件后缀
getFileSuffixIcon获取文件后缀图标
getFileTitle获取文件标题
isCompressFilePath判断压缩文件路径
isDocumentFilePath判断文档文件路径
isFilePath判断是否为文件路径
isImageFilePath判断图片文件路径
isMarkdownFilePath判断 Markdown 文件路径
isMusicFilePath判断音频文件路径
isPptFilePath判断 PPT 文件路径
isVideoFilePath判断视频文件路径
singleDownloadFile下载单个文件
singleDownloadFileBase64下载 Base64 文件
updateFilePathQuery更新文件路径 query

同时导出文件类型枚举与类型:

typescript
import type { FileSuffix, ImageFileSuffix } from '@mingto/tools'
import { FileSuffixEnum, ImageFileSuffixEnum } from '@mingto/tools'

function

API说明
debounce防抖函数
throttle节流函数

math

API说明
add精确加法
subtract精确减法
multiply精确乘法
divide除法
getDecimalPlaces获取小数位数
calcAspectRatio计算宽高比

object

API说明
deepFreeze深冻结对象
pickObject从对象中选取指定字段

string

API说明
chunkString将字符串按指定长度切分
objectToQueryString对象转 query string
queryStringToObjectquery string 转对象

supply

API说明
getUuid获取 UUID
timeDelay延迟执行

theme

API说明
useTheme主题相关工具

validator

API说明
isValidBase64校验 Base64 字符串
isValidUrl校验 URL

web-socket

API说明
createWebSocket创建 WebSocket 连接
isWebSocketSupported判断当前环境是否支持 WebSocket

worker

API说明
createWorker创建 Worker
closeWorker关闭 Worker

常用示例

类型判断

typescript
import { isArray, isNumber, isObject, isString } from '@mingto/tools'

isArray([1, 2, 3])
isNumber(1)
isObject({ a: 1 })
isString('hello')

精确计算

typescript
import { add, divide, multiply, subtract } from '@mingto/tools'

add(0.1, 0.2)
subtract(0.3, 0.1)
multiply(0.1, 0.2)
divide(10, 2)

文件路径处理

typescript
import { getFileName, getFileSuffix, getFileTitle, isImageFilePath } from '@mingto/tools'

getFileName('/path/to/demo.png')
getFileTitle('/path/to/demo.png')
getFileSuffix('/path/to/demo.png')
isImageFilePath('/path/to/demo.png')

函数控制

typescript
import { debounce, throttle } from '@mingto/tools'

const onResize = debounce(() => {}, 300)
const onScroll = throttle(() => {}, 300)

国际化

支持语言:zh-cnzh-twenmsarthvi

typescript
import tools from '@mingto/tools'

tools.locale('en')
const locale = tools.getLocale()