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

js-tool-lei

v1.0.5

Published

这是一个js工具类、函数库,兼容传统浏览器,包含了日常开发中使用频率较高的一些函数封装,支持ts类型提示。

Downloads

362

Readme

这是一个js工具类、函数库,兼容传统浏览器,包含了日常开发中使用频率较高的一些函数封装,支持ts类型提示。

1、加法运算:add(a, b)

接收两个参数a,b,数据类型

// 调用方法
import { add } from 'js-tool-lei'

add(1, 2) // 返回3

2、减法运算:sub(a, b)

接收两个参数a,b,数据类型

// 调用方法
import { sub } from 'js-tool-lei'

sub(1, 2) // 返回-1

3、乘法运算:mul(a, b)

接收两个参数a,b,数据类型

// 调用方法
import { mul } from 'js-tool-lei'

mul(1, 2) // 返回2

4、除法运算:div(a, b)

接收两个参数a,b,数据类型

// 调用方法
import { div } from 'js-tool-lei'

div(1, 2) // 返回0.5

5、检测页面可见性:backUpdate(visibleBack)

接收两个参数visibleBack(页面可见时执行的回调), hideBack(页面不可见时执行的回调,可选参数)

// 调用方法
import { backUpdate } from 'js-tool-lei'

backUpdate(() => {}) // 页面可见时执行第一个回调,不可见时不执行任何回调
backUpdate(() => {}, () => {}), // 页面可见时执行第一个回调,不可见时执行第二个回调

6、判断是否为微信浏览器:isWeixin

// 调用方法
import { isWeixin } from 'js-tool-lei'

isWeixin() // 返回true或false

7、判断当前设备是哪种设备,如ios、安卓、移动端等等,方法名:browser。

// 调用方法
import { browser } from 'js-tool-lei'

browser().ios // 判断是否为ios
browser().android // 判断是否为安卓
browser().mobile // 判断是否为移动端
browser().pc // 判断是否为pc端
browser().qqBrowser // 判断是否为qq浏览器

8、判断是否是对象:isObject

// 调用方法
import { isObject } from 'js-tool-lei'

isObject({}) // 返回true

9、返回页面路径含查询参数,方法名:paramsString(path, query)

接收连个参数,url(页面路径)、query(对象)

// 调用方法
import { paramsString } from 'js-tool-lei'

paramsString('/index', {name: '张三'}) // 返回 /index?name=张三

10、页面滚动到底部自动加载:loadMore(callback)

接收一个参数,即页面滚动到底部时执行的回调

// 调用方法
import { loadMore } from 'js-tool-lei'

loadMore(() => {})

11、根据给定的字段,返回查询参数的值:getQueryVariable(variable, defaultValue)

接收两个参数,查询参数字段,默认值

// 调用方法
import { getQueryVariable } from 'js-tool-lei'

getQueryVariable('name', 'tom') // 返回 name的值,没有则返回默认值tom

12、js判断对象是否为空:isEmptyObject(obj)

13、防抖函数:debounce(func, wait, immediate)

接收三个参数,func(回调函数),wait(间隔时间),immediate(是否立即执行)

// 调用方法
import { debounce } from 'js-tool-lei'

debounce(() => {}, 100, true)

14、节流函数:throttle(func, wait)

接收两个参数,func(回调函数),wait(间隔时间)

// 调用方法
import { throttle } from 'js-tool-lei'

throttle(() => {}, 100)

15、计算当前进度百分比:getPercent(current, total)

接收两个参数,当前进度、总进度

// 调用方法
import { getPercent } from 'js-tool-lei'

getPercent(10, 20) // 返回当前进度为50%

16、时间戳格式化:timeFormat (time, format)

接收两个参数,倒计时时间戳(秒为单位)、format格式化规则

// 调用方法
import { timeFormat } from 'js-tool-lei'

timeFormat(60, 'hh:mm:ss')  // 返回00:01:00
timeFormat(60, 'hh-mm-ss')  // 返回00-01-00
timeFormat(60, 'dd天hh时mm分ss秒')  // 返回00天00时01分00秒

| 格式化规则 | 说明 | |-------|-----------| | dd | 天 | | hh | 小时 | | mm | 分钟 | | ss | 秒 |

17、格式化时间:formatTime(time, format)

接收两个参数,时间、格式化规则

// 调用方法
import { formatTime } from 'js-tool-lei'

formatTime(1723695955755, 'Y-M-D h:m:s') // 返回格式化后的时间

18、获取指定日期的前n天或后n天,返回格式:yyyy-MM-dd:getBeforeOrAfterDay(date, day)

接收两个参数,日期、天数

// 调用方法
import { getBeforeOrAfterDay } from 'js-tool-lei'

getBeforeOrAfterDay(1723695955755, 10) // 返回当前日期的后10天

19、计算px转换rem:px2rem(px)

接收一个参数,px值,如10或10px

// 调用方法
import { px2rem } from 'js-tool-lei'

px2rem(10) // 返回10/16 + 'rem'

20、局部容器滚动到底部加载更多:localityLoadMore(dom, innerDom, callback)

接收三个参数,容器、局部容器、回调函数

// 调用方法
import { localityLoadMore } from 'js-tool-lei'

localityLoadMore(document.querySelector('.container'), document.querySelector('.inner-container'), () => {})

21、获取当前页面查询参数,返回对象:getQueryObject()

// 调用方法
import { getQueryObject } from 'js-tool-lei'

// 返回当前页面查询参数对象,如https://www.example.com?name=tom&age=20 返回 {name: 'tom', age: '20'}
getQueryObject() 

22、判断是否是合法中国大陆手机号:isValidChinaPhone(phone)

// 调用方法
import { isValidChinaPhone } from 'js-tool-lei'

isValidChinaPhone('13812345678') // 返回true

23、判断是否是合法邮箱:isValidEmail(email)

// 调用方法
import { isValidEmail } from 'js-tool-lei'

isValidEmail('[email protected]') // 返回true

24、判断数据的具体类型:getType(data)

// 调用方法
import { getType } from 'js-tool-lei'

getType([]) // 返回'Array'
getType({}) // 返回'Object'
getType('') // 返回'String'
getType(123) // 返回'Number'
getType(null) // 返回'Null'
getType(undefined) // 返回'Undefined'
getType(new Date()) // 返回'Date'
getType(true) // 返回'Boolean'

25、判断是否是合法中国身份证号,包含15位与18位:isValidID(id)

// 调用方法
import { isValidID } from 'js-tool-lei'

isValidID('130503670401001') // 返回true

26、复制文字到剪切板:copyToClipboard(text)

// 调用方法
import { copyText } from 'js-tool-lei'

// 复制文字到剪切板
copyText('123')
    .then(() => {})
    .catch(() => {})

27、倒计时:countDown(time, format, callback)

接收三个参数,倒计时时间戳(秒为单位)、格式化规则(默认hh:mm:ss)、回调函数

// 调用方法
import { countDown } from 'js-tool-lei'

countDown(60, 'hh:mm:ss', (data) => {
    if (data) {
        console.log(data)
    } else {
        console.log('倒计时结束')
    }

| 格式化规则 | 说明 | |-------|-----------| | dd | 天 | | hh | 小时 | | mm | 分钟 | | ss | 秒 |