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 🙏

© 2026 – Pkg Stats / Ryan Hefner

yonxin-loadshjs

v0.0.26

Published

用心前端常用的js方法工具包

Readme

一、使用

需要使用哪个函数,只引用当前函数。

在小程序项目:
import { throttle } from 'yonxin-loadshjs/helpers'
在其他项目:
import { throttle } from 'yonxin-loadshjs/dist/helpers'
throttle()
或
import { helpers } from 'yonxin-loadshjs'
helpers.throttle()

二、模块

目前有7个模块:

  • 1.helpers - 常用的助手函数
  • 2.date - 与日期相关的函数
  • 3.string与字符串相关的函数
  • 4.number与数字计算相关的函数
  • 5.money与金钱相关的函数
  • 6.network与网络相关的函数
  • 7.storage与存储相关的函数
  • 7.image对图片的处理(如压缩)

三、文档

3.1:helpers助手函数

restArguments(func, startIndex):查看详细文档
delay(function, wait, args):查看详细文档
throttle(func, wait, options):查看详细文档
debounce(func, wait, options):查看详细文档
compareVersion(v1, v2):版本号对比,v1>v2返回1,v1<v2返回0,相等返回0

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | v1 | 版本号字符串 | String | | | | v2 | 版本号字符串 | String | | |

isNumber(val):校验是否数字,是正负整数,0以及正负浮点数就返回true,否则返回false

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | val | 版本号字符串 | String | | |

cloneDeep(x):深拷贝一个值

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | x | 对象或数组 | Object | | |

sha1(s):字符串加密成 hex 字符串

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | s | 待加密的字符串 | String | | |

copyText(value):复制字符串到剪贴板,复制成功返回true,否则false

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待复制的字符串 | String | | |

3.2:date日期函数

formatDate(time, fmt):将 Date 转化为指定格式的String

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | time | 待转化的时间戳(毫秒) | Number | | | | fmt | 格式化风格 | String | | yyyy-MM-dd hh:mm:ss |

dateToTimestamp(str):把“2000-12-31”或“2000/12/31”格式的字符串转换为js时间戳

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | str | 时间日期字符串 | String | | |

jsTimestamp(timestamp, language):把接口返回的时间戳转为毫秒级的

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | timestamp | 其他编程语言的时间戳 | Number | | | | language | 编程语言 | String | | PHP |

apiTimestamp(timestamp, language):把js的时间戳(毫秒),转成接口所需要的时间戳(PHP是秒)

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | timestamp | 其他编程语言的时间戳 | Number | | | | language | 编程语言 | String | | PHP |

lastSecond(timestamp):把当天的时间转换成当天最后一秒的时间戳(毫秒)

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | timestamp | 时间 | String 或 Number 或 Date对象 | | 0 |

timeSurplus(seconds):剩余时间显示逻辑

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | seconds | 剩余秒数 | Number | | |

timeToTimestamp(value):把时间转换成js时间戳

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 时间 | String或Number或Date对象 | | |

firstSecond(timestamp):把当天的时间转换成当天第一秒的时间戳(毫秒)

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | timestamp | 时间 | String 或 Number 或 Date对象 | | 0 |

3.3:string字符串函数

isJson(string):判断一个字符串是否json

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | string | 字符串 | String | | |

toLowerLine(str):驼峰型字符串转成蛇形 aAbc =>a_abc

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | str | 字符串 | String | | |

toCamel(str):蛇形转驼峰 a_bcd=>aBcd

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | str | 字符串 | String | | |

checkTextLength(value):计算中文字节长度,中文、中文标点、全角字符按1长度,英文、英文符号、数字按0.5长度计算

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 字符串 | String | | |

htmlEscape(string):把特殊字符转换成html实体

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | string | 字符串 | String | | |

htmlUnEscape(string):把html实体转换成特殊字符

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | string | 字符串 | String | | |

getDate(n):[获取今天前后的日期]

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | n | 日期 | Number | | |

3.4:number数字函数

numberRound(value, dit):四舍五入保留dit位小数(若第二位小数为0,则保留一位小数)

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String或Number | | | | dit | 小数点位数(传入负数时,往上取近似值) | Number | | 0 |

numberToString(value, dit, separator):按指定格式把数字转换成数字字符串

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String或Number | | | | dit | 小数点位数 例如:传入2,会把1转成"1.00",若小数部分长度大于dit,则四舍五入 | Number | | 0 | | separator | 每千位分割符 原"1000"转换后是"1,000" | String | | 空字符串 |

numberToCn(values):传入整数类型的数据,返回中文数字,例如:传入123456返回"十二万三千四百五十六"

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | Number | | |

symbolReplaceNumber(str):传入一串数字,返回除了前三后四位中间是*号的数,例如:传入18188886659返回"181****6659"

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | str | 待处理数值 | String或Number | | |

3.5:money金钱函数

numberToPrice(value, seperator):把数字转换成价钱字符串,强制四舍五入保留2位小数

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String或Number | | | | separator | 每千位分割符 原"1000"转换后是"1,000" | String | | 空字符串 |

numberToPriceCn(num):把金额变成大写,例如12345.67 返回"壹万贰仟叁佰肆拾伍元陆角柒分"

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | num | 待处理数值 | String或Number | | |

checkTel(value):验证手机号是否合法,例如18188886659 返回true

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String或Number | | |

checkiDNumber(value):验证身份证号是否合法,例如440982199702044857 返回true

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String或Number | | |

checkEmail(value):验证邮箱是否合法,例如[email protected] 返回true

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String | | |

checkBankCardNo(value):验证银行卡号是否合法,例如6217003328006865876 返回true

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | value | 待处理数值 | String | | |

3.6:network网络函数

downloadFile(blobObj, fileName):下载二进制文件流

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | blobObj | 二进制文件流 | Object | | | | fileName | 文件名(含后缀) | String | | |

3.6:storage存储函数

wxGetCookie(key):小程序获取储存的值

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | key | 小程序储存的key值 | String | | |

wxSetCookie(key, value):小程序设置储存

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | key | 小程序储存的key值 | String | | | | value | 待储存的值 | 任何基础类型 | | |

wxRemoveCookie(key):小程序删除储存的值

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | key | 小程序储存的key值 | String | | |

3.7:wxCompressImage 压缩图片函数

wxCompressImage(options: Object, callback: Function):压缩图片

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | options | 配置项 | Object | | | | callback | 回调函数,返回压缩好的图片路径 | | |

options 对象参数

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | canvasId | canvas 标签上面设置的canvas-id 属性值 | String | 必填 | | | imagePath | 图片路径http://tmp/xxx.png | 必填 | | | limitSize | 图片最大值 | 选填 | 200kb | | drawWidth | 画布宽度 | 选填 | 宽默认是windowWidth | | quality | 图片质量 | 选填 | 默认是1 | | fileType | 图片格式 | 选填 | 默认是jpg |

wxChangeBase64(img: String): 将图片转成base64

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | img | 图片路径http://tmp/xxx.png | String | | |

wxSubscribeMessage(tmplIds: Array): 请求调用消息订阅

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | tmplIds | 消息模板id | Arrary | 必填 | [] |

3.8:用户定位函数

getLocation(type): 获取当前定位以及定位下的地址,返回一个Promise

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | type | 坐标类型 | string | wgs84 / gcj02 | gcj02 | | needAddress | 是否需要返回详细地址,如果为true,会请求高德地图接口获得详细地址 | boolean | - | true |

返回结果参数:

| 参数 | 说明 | 类型 | | :----: | :----: | :----: | | address | 用户的详细地址 | string | | latitude | 纬度 | number | | longitude | 经度 | number |

定位失败时会进行相应的引导操作或给出相应的提示语

getCurrentPoiDetail(latitude, longitude): 高德地图逆地址解析,返回一个Promise

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: |:---- |:----: | | latitude | 纬度 | number | - | - | | longitude | 经度 | number | - | - |

返回结果,可以参考高德地图官方文档(逆地址编码)

四、单元测试

文档:Mocha中文文档

命令参考
  • 对某个函数进行单元测试
$ npm run mocha test/number.numberRound.test.js

如果全局安装了mocha,则运行

$ mocha test/number.numberRound.test.js

五、新版本发布

文档:np自动化发布工具文档

  1. 先全局安装np包,如已安装,请跳过
npm install --global np
  1. 发布指定版本(例如0.0.16)
np 0.0.16