npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

daily-tool

v1.0.12

Published

This is an everyday use of the function wrapper library.

Downloads

18

Readme

不断更新的日常用到的函数

下载命令

npm i daily-tool

使用方式如下:

import { isEmpty } from 'daily-tool'

const example = {
	name: '张三'
}

const test = () => {
    if (isEmpty(example)) {
	    return console.log("该对象为空")
    }

    console.log("该对象不为空")
}

test()

判断类型

1、typeJudge

函数名称:typeJudge 作用:判断数据类型 类型:isArray | isObject | isString | isDate | isRegExp | isFunction | isBoolean | isNumber | isNull | isUndefined 例子:typeJudge().isArray(example)

/**
 * 判断数据类型
 * @param {*} target 需要判断的值
 * @returns
 */
typeJudge().isArray(example)
typeJudge().isObject(example)
typeJudge().isString(example)
typeJudge().isDate(example)
typeJudge().isRegExp(example)
typeJudge().isFunction(example)
typeJudge().isBoolean(example)
typeJudge().isNumber(example)
typeJudge().isNull(example)
typeJudge().isUndefined(example)

判断是否为空

1.isEmpty

函数名称:isEmpty 作用:判断是否是空 例子:isEmpty(example)

/**
 * 判断是否是空
 * @param example 需要判断的值
 * @returns {boolean}
 */
isEmpty(example)
2、isEmptyObject

函数名称:isEmptyObject 作用:判断是否是空对象 例子:isEmptyObject(example)

/**
 * 判断是否是空对象
 * @param example 需要判断的值
 * @returns {boolean}
 */
isEmptyObject(example)
3、isEmptyArray

函数名称:isEmptyArray 作用:判断是否是空数组 例子:isEmptyArray(example)

/**
 * 判断是否是空数组
 * @param example 需要判断的数组
 * @returns {boolean}
 */
isEmptyArray(example)
4、isEmptyString

函数名称:isEmptyString 作用:判断是否是空字符串 例子:isEmptyString(example)

/**
 * 判断是否是空字符串
 * @param example 需要判断的字符串
 * @returns {boolean}
 */
isEmptyString(example)
5、isEmptyNumber

函数名称:isEmptyNumber 作用:判断是否是空数值 例子:isEmptyNumber(example)

/**
 * 判断是否是空数值
 * @param example 需要判断的数值
 * @returns {boolean}
 */
isEmptyNumber(example)

路径

1、getParamsByUrl

函数名称:getParamsByUrl 作用:根据 url 获取参数 例子:getParamsByUrl(example)

/**
 * 根据 url 获取参数
 * @param example 路径地址
 * @returns {object}
 */
getParamsByUrl(example)
2、encodeUrl

函数名称:encodeUrl 作用:URL 编码 例子:encodeUrl(url)

/**
 * URL编码
 * @param {string} url
 * @return {string}
 */
encodeUrl(url)
3、decodeUrl

函数名称:decodeUrl 作用:URL 解码 例子:decodeUrl(url)

/**
 * URL解码
 * @param {string} url
 * @return {string}
 */
decodeUrl(url)

节点

1、getElementSize

函数名称:getElementSize 作用:获取元素尺寸 例子:getElementSize(example)

/**
 * 获取元素尺寸
 * @param example 节点
 * @returns {object}
 */
getElementSize(document.getElementById('app'))
2、getScrollOffset

函数名称:getScrollOffset 作用:获取滚动条偏移量 例子:getScrollOffset(example)

/**
 * 获取滚动条偏移量
 * @param example 节点
 * @returns {object}
 */
getScrollOffset(document.getElementById('app'))
3、getViewportSize

函数名称:getViewportSize 作用:获取视口尺寸 例子:getViewportSize()

/**
 * 获取视口尺寸
 * @returns {object}
 */
getViewportSize()
4、findParents

函数名称:findParents 作用:找元素 el 的第 n 级父元素 例子:findParents(el, n)

/**
 * 找元素的第 n 级父元素
 * @param el 节点
 * @param n 级数
 * @returns {object}
 */
findParents(el, n)
5、matcheParentsEl

函数名称:matcheParentsEl 作用:判断 parentSelector 是否是 el 的父节点 例子:matcheParentsEl(el, parentSelector)

/**
 * 判断 parentSelector 是否是 el 的父节点
 * @param el 节点
 * @param parentSelector 节点
 * @returns {Boolean}
 */
matcheParentsEl(el, parentSelector)

日期

1、formatDate

函数名称:formatDate 作用:格式化日期(时间戳转换成日期) 例子:formatDate(example)

/**
 * 格式化日期(时间戳转换成日期)
 * @param example 时间戳
 * @returns {String}
 */
formatDate(example)
2、convertToTimestamp

函数名称:convertToTimestamp 作用:日期转换成时间戳 例子:convertToTimestamp('2023-11-25')或 convertToTimestamp('2023/11/25')或 convertToTimestamp('2023.11.25')

/**
 * 日期转换成时间戳
 * @param example 日期
 * @returns {String}
 */
convertToTimestamp(example)

数组

1、sortByProperty

函数名称:sortByProperty 作用:数组按 type 升序或者降序排列,第三个参数'asc'是升序,'desc'是降序 例子:sortByProperty(data, property, 'desc')

/**
 * 数组按 type 升序或者降序排列,第三个参数'asc'是升序,'desc'是降序
 * @param data 数组
 * @param property 排序字段
 * @param type 排序类型
 * @returns {Object}
 */
sortByProperty(data, property, type)
2、bubbleSort

函数名称:bubbleSort 作用:数组冒泡排序 例子:bubbleSort(arr)

/**
 * 数组冒泡排序
 * @param arr 数组
 * @returns {Object}
 */
bubbleSort(arr)
3、removeByCondition

函数名称:removeByCondition 作用:删除数组中符合条件的元素 例子:removeByCondition(arr)

/**
 * 删除数组中符合条件的元素
 * @param arr 数组
 * @param condition 条件
 * @returns {Object}
 */
removeByCondition([1, 2, 3, 4, 5, 6], (item) => [1, 2, 6].includes(item))
4、sumArr

函数名称:sumArr 作用:数组求和 例子:sumArr(arr)

/**
 * 数组求和
 * @param arr 数组
 * @returns {Number}
 */
sumArr([1, 2, 3, 4, 5, 6])
5、averageArr

函数名称:averageArr 作用:数组求平均值 例子:averageArr(arr)

/**
 * 数组求平均值
 * @param arr 数组
 * @returns {Number}
 */
averageArr([1, 2, 3, 4, 5, 6])
6、medianArr

函数名称:medianArr 作用:数组求中位数 例子:medianArr(arr)

/**
 * 数组求中位数
 * @param arr 数组
 * @returns {Number}
 */
medianArr([1, 2, 3, 4, 5, 6])
7、maxArr

函数名称:maxArr 作用:数组求最大值 例子:maxArr(arr)

/**
 * 数组求最大值
 * @param arr 数组
 * @returns {Number}
 */
maxArr([1, 2, 3, 4, 5, 6])
8、minArr

函数名称:minArr 作用:数组求最小值 例子:minArr(arr)

/**
 * 数组求最小值
 * @param arr 数组
 * @returns {Number}
 */
minArr([1, 2, 3, 4, 5, 6])
9、mostFrequent

函数名称:mostFrequent 作用:数组求出现次数最多的元素 例子:mostFrequent(arr)

/**
 * 数组求出现次数最多的元素
 * @param arr 数组
 * @returns {Number}
 */
mostFrequent([1, 2, 3, 4, 5, 6])
10、uniqueArr

函数名称:uniqueArr 作用:数组去重 例子:uniqueArr(arr)

/**
 * 数组去重
 * @param arr 数组
 * @returns {Array}
 */
uniqueArr([1, 2, 3, 4, 5, 6])

时间戳

1、getStartEndTime

函数名称:getStartEndTime 作用:获取各类时间起始时间戳

/**
 *获取各类时间起始时间戳
 */

const {
  nowTimeStamp,
  todayStartTimeStamp,
  todayEndTimeStamp,
  yesterdayStartTimeStamp,
  yesterdayEndTimeStamp,
  thisWeekStartTimeStamp,
  thisWeekEndTimeStamp,
  thisMonthStartTimeStamp,
  thisMonthEndTimeStamp,
  thisYearStartTimeStamp,
  thisYearEndTimeStamp,
} = getStartEndTime()

加载 js 文件

1.loadJs

函数名称:loadJs 作用:加载**.js 文件 例子:loadJs(scriptSrc, successCallback, id, content)

/**
 * 加载**.js文件
 * @param scriptSrc 文件路径
 * @param successCallback 加载成功回调函数
 * @param script 标签id
 * @param content js标签内的内容
 */

<script>loadJs('https://static.wxb.com.cn/xxx.js')</script>
2.isIncludeJs

函数名称:isIncludeJs 作用:是否已经加载过**.js 文件

/**
 *是否已经加载过**.js文件
 * @param name js文件名
 */

<script>isIncludeJs('lodash')</script>

其他

1、deepClone

函数名称:deepClone 作用:深拷贝 说明:obj 是要复制的对象。它可以是任何类型的对象,包括但不限于数组、普通对象、日期、正则表达式和错误对象。cache 是可选参数,默认为空数组,用于检测和阻止循环引用。 当传入的对象 obj 是基础类型(如 string, number, boolean, undefined, null)时,函数直接返回其副本;如果是对象(包括数组、函数等),则会进行深度克隆。对于内置的 Date , RegExp 和 Error 对象,进行了特殊处理。对于数组,逐项复制每一项元素。对于其他对象,复制每个属性。

例子:deepClone(example)

/**
 * 深拷贝
 * @param example 要复制的对象
 * @returns {any}
 */
deepClone(example)
2、getStartEndTime

函数名称:randomNum 作用:指定范围内的随机整数 例子:randomNum(minimum, maximum)

/**
 * 指定范围内的随机整数
 * @param minimum 最小值
 * @param maximum 最大值
 * @returns {int}
 */
randomNum(minimum, maximum)
3、numFilter

函数名称:randomNum 作用:数字超过 99 显示 99+ 例子:numFilter(num,'+')

/**
 * 数字超过 99 显示 99+
 * @param num 数字
 * @param type 超出字符
 * @returns {String}
 */
numFilter(num, '+')
4、computeTime

函数名称:computeTime 作用:计算函数执行所用时间 例子:computeTime(func,args)

/**
 * 计算函数执行所用时间 可带参数
 * @param {*} func 函数
 * @param {*} args 函数参数
 * @returns
 */
computeTime(func, args)
5、isDevice

函数名称:isDevice 作用:判断是 android 还是 ios 还是 web 例子:isDevice()

/**
 * 判断是 android 还是 ios 还是 web
 * @returns
 */
isDevice()
6、isWx

函数名称:isWx 作用:判断是否为微信 例子:isWx()

/**
 * 判断是否为微信
 * @returns
 */
isWx()
7、imgLoadAll

函数名称:imgLoadAll 作用:判断图片是否全部加载完成 例子:imgLoadAll(arr, callback)

/**
 * 判断图片是否全部加载完成
 * @param arr 图片数组
 * @param callback 加载完成回调函数
 * @returns
 */
imgLoadAll(arr, callback)
8、copyTxt

函数名称:copyTxt 作用:复制文本 例子:copyTxt(text, fn)

/**
 * 复制文本
 * @param text 文本
 * @param fn 回调函数
 * @returns
 */
copyTxt(text, fn)
9、downloadBaseFile

函数名称:downloadBaseFile 作用: base64 图片下载 例子:downloadBaseFile(base64, fileName)

/**
 * base64 图片下载
 * @param base64 base64图片
 * @param fileName 文件名
 * @returns
 */
downloadBaseFile(base64, fileName)
10、debounce

函数名称:debounce 作用: 防抖 例子:debounce(func, wait, immediate)

/**
 * 防抖
 * @param func 函数
 * @param wait 延迟时间
 * @param immediate 是否立即执行
 * @returns
 */
debounce(func, wait, immediate)
11、throttle

函数名称:throttle 作用: 节流 例子:throttle(func, delay)

/**
 * 节流
 * @param func 函数
 * @param delay 延迟时间
 * @returns
 */
throttle(func, delay)
12、validatePasswordStrength

函数名称:validatePasswordStrength 作用: 密码强度校验 例子:validatePasswordStrength(password)

/**
 * 密码强度校验
 * @param password 密码
 * @returns
 */
validatePasswordStrength(password)
13、once

函数名称:once 作用: 只执行一次 例子:once(func)

/**
 * 只执行一次
 * @param func 函数
 * @returns
 */
const sayHello = once(() => {
  console.log('Hello, this will be logged only once!')
})

// 调用包装后的函数多次,但实际只执行一次
sayHello() // 输出 "Hello, this will be logged only once!"
sayHello() // 不会再次输出
sayHello() // 也不会再次输出
14、generateUUID

函数名称:generateUUID 作用: 生成 UUID 例子:generateUUID()

/**
 * 生成 UUID
 * @returns
 */
generateUUID()

正则表达式判断

1、checkStr

函数名称:checkStr 作用:正则表达式判断类型 例子:checkStr(str, type)

/**
 * 正则表达式判断
 * @param str 字符串
 * @param type 类型
 * @returns
 */
checkStr(str, type)

type 类型:

| 类型 | 描述 | | :-----: | :----------------------------------------------------------: | | phone | 手机号码 | | tel | 座机 | | card | 身份证 | | pwd | 密码以字母开头,长度在 6~18 之间,只能包含字母、数字和下划线 | | postal | 邮政编码 | | QQ | QQ 号 | | email | 邮箱 | | money | 金额(小数点 2 位) | | URL | 网址 | | IP | IP | | date | 日期时间 | | number | 数字 | | english | 英文 | | chinese | 中文 | | lower | 小写 | | upper | 大写 | | HTML | HTML 标记 |