daily-tool v2.0.1
不断更新的日常用到的函数
下载命令
npm i daily-tool
使用方式如下:
import { isEmpty } from 'daily-tool'
const example = {
name: '张三'
}
if (isEmpty(example)) {
console.log("该对象为空")
} else {
console.log("该对象不为空")
}
判断类型
1、typeJudge
函数名称:typeJudge 作用:判断数据类型
类型:isArray | isObject | isString | isDate | isRegExp | isFunction | isBoolean | isNumber | isNull | isUndefined
例子:typeJudge().isArray(example)
判断是否为空
1、isEmpty
函数名称:isEmpty 作用:判断是否是空 例子:isEmpty(example)
2、isEmptyObject
函数名称:isEmptyObject 作用:判断是否是空对象 例子:isEmptyObject(example)
3、isEmptyArray
函数名称:isEmptyArray 作用:判断是否是空数组 例子:isEmptyArray(example)
4、isEmptyString
函数名称:isEmptyString 作用:判断是否是空字符串 例子:isEmptyString(example)
5、isEmptyNumber
函数名称:isEmptyNumber 作用:判断是否是空数值 例子:isEmptyNumber(example)
路径
1、getParamsByUrl
函数名称:getParamsByUrl 作用:根据url获取参数 例子:getParamsByUrl(example)
节点
1、getElementSize
函数名称:getElementSize 作用:获取元素尺寸 例子:getElementSize(example)
2、getScrollOffset
函数名称:getScrollOffset 作用:获取滚动条偏移量 例子:getScrollOffset(example)
3、getViewportSize
函数名称:getViewportSize 作用:获取视口尺寸 例子:getViewportSize(example)
4、findParents
函数名称:findParents 作用:找元素的第n级父元素 例子:findParents(el, n)
5、matcheParentsEl
函数名称:matcheParentsEl 作用:判断是否是父节点 例子:matcheParentsEl(el, parentSelector)
日期
1、formatDate
函数名称:formatDate 作用:格式化日期(时间戳转换成日期) 例子:formatDate(example)
2、convertToTimestamp
函数名称:convertToTimestamp 作用:日期转换成时间戳 例子:convertToTimestamp('2023-11-25')或convertToTimestamp('2023/11/25')或convertToTimestamp('2023.11.25')
数组
1、sortByProperty
函数名称:sortByProperty 作用:数组按type升序或者降序排列,第三个参数'asc'是升序,'desc'是降序 例子:sortByProperty(data, property, 'desc')
2、bubbleSort
函数名称:bubbleSort 作用:数组冒泡排序 例子:bubbleSort(arr)
时间戳
1、getStartEndTime
函数名称:getStartEndTime 作用:获取各类时间起始时间戳 例子:
const {
nowTimeStamp,
todayStartTimeStamp,
todayEndTimeStamp,
yesterdayStartTimeStamp,
yesterdayEndTimeStamp,
thisWeekStartTimeStamp,
thisWeekEndTimeStamp,
thisMonthStartTimeStamp,
thisMonthEndTimeStamp,
thisYearStartTimeStamp,
thisYearEndTimeStamp,} = getStartEndTime()
其他
1、deepClone
函数名称:deepClone 作用:深拷贝
说明:obj 是要复制的对象。它可以是任何类型的对象,包括但不限于数组、普通对象、日期、正则表达式和错误对象。cache 是可选参数,默认为空数组,用于检测和阻止循环引用。
当传入的对象 obj 是基础类型(如 string, number, boolean, undefined, null)时,函数直接返回其副本;如果是对象(包括数组、函数等),则会进行深度克隆。对于内置的 Date , RegExp 和 Error 对象,进行了特殊处理。对于数组,逐项复制每一项元素。对于其他对象,复制每个属性。
例子:deepClone(example)
2、getStartEndTime
函数名称:randomNum 作用:指定范围内的随机整数 例子:randomNum(minimum, maximum)
3、numFilter
函数名称:randomNum 作用:数字超过99显示99+ 例子:numFilter(num,'+')
4、computeTime
函数名称:computeTime 作用:数字超过99显示99+ 例子:computeTime(func,args)
5、isDevice
函数名称:isDevice 作用:判断是android还是ios还是web 例子:isDevice()
6、isWx
函数名称:isWx 作用:判断是否为微信 例子:isWx()
7、imgLoadAll
函数名称:imgLoadAll 作用:判断图片加载完成 例子:imgLoadAll(arr, callback)
8、copyTxt
函数名称:copyTxt 作用:复制文本 例子:copyTxt(text, fn)
9、downloadBaseFile
函数名称:downloadBaseFile 作用: base64图片下载 例子:downloadBaseFile(base64, fileName)
10、debounce
函数名称:debounce 作用: 防抖 例子:debounce(func, wait, immediate)
11、throttle
函数名称:throttle 作用: 节流 例子:throttle(func, delay)