@strive_molu/utils
v0.0.5
Published
前端常用工具函数
Downloads
8
Readme
molu-utils
前端常用公共工具函数
安装
npm install @strive_molu/utils --save
方法
debounce
解释
防抖函数:高频触发一个事件只执行最后一次(在immediate为ture执行第一次)。
形参
| 名称 | 说明 | 类型 | 默认值 | | --------- | ---------------------------------------------------------- | -------- | ------ | | fn | 执行目标函数 | Function | 无 | | delay | 目标函数延迟多久执行,单位 ms | number | 200 | | immediate | 是否立即执行目标函数,当为true时,目标函数会优先执行第一次 | boolean | false |
throttle
解释
节流函数:高频执行事件在每delay秒内执行一次。
形参
同上debounce的形参。
deepCopy
解释
深拷贝函数。兼容拷贝属性的描述符;属性值为Date
和RegExp
的对象;处理对象中有循环引用的情况。
形参
| 名称 | 说明 | 类型 | 默认值 | | ------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | -------------------------------- | | target | 需要拷贝的目标对象 | any | 无 | | cacheOptions | 处理循环引用配置;cache:缓存类型为对象的属性值,replace:目标对象中有循环引用的属性值的替代符。 | { cache?: WeakMap<Object, CacheOptions['replace']>; replace?: any } | {cache:new WeakMap,replace:null} |
animationNum
解释
js中数字动画函数。可以获取在规定时间内从初始值到目标值中的每一帧的值。
形参
| 名称 | 说明 | 类型 | 默认值 | | -------- | ----------------------------------- | ------------------ | ------ | | from | 起始值 | number | 无 | | to | 目标值 | number | 无 | | callback | 获取当前帧值的回调函数 | (curValue) => void | 无 | | duration | 从起始值到目标值经历的时间,单位 ms | number | 1000 |
parallelTask
解释
并发执行异步任务。
形参
| 名称 | 说明 | 类型 | 默认值 | | ------------- | ------------------------------------------------------------ | ------------------ | ------ | | tasks | 异步任务集合,每一个任务必须是一个函数,其函数返回一个promise对象。 | Array<()=>Promise> | 无 | | parallelCount | 并发数 | number | 2 |
返回值
返回一个promise
对象。该对象的状态为fulfilled
时返回的值为一个数组,数组的值的类型如下。
type TaskResData = {
type: 'success' | "fail"; //异步任务完成状态
data: any; //异步任务状态改变的返回值
}
FileChunks
解释
可以对文件进行分块,获取文件的唯一hash值的一个类。
实例属性
| 名称 | 说明 | 类型 | | ------ | ---------------- | ----------- | | chunks | 文件的分块信息, | Array | | hash | 文件的唯一hash值 | string |
静态方法
readChunkFile
读取Blob类型或File类型对象的具体内容
形参
| 名称 | 说明 | 类型 | 默认值 | | ---- | -------------- | --------------------------------------- | ------ | | blob | 需要读取的对象 | Blob | File | 无 | | type | 读取内容的格式 | 'buffer' | 'text' | 'url' | "binary" | buffer |