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

weapp-util

v1.0.0

Published

🌞 为微信小程序定制的工具函数

Downloads

8

Readme

Weapp Util

🌞 为微信小程序定制的工具函数

更多微信小程序开发工具,查看 微信小程序开发全家桶

安装

npm i weapp-util

注意:在小程序中使用npm包前,需先构建 npm

使用

// index.js
import util from 'weapp-util'

// 判断传入值是否为字符串
util.isString('1') // true
// 判断传入值是否为数值
util.isNumber(1) // true
// 判断传入值是否为整数
util.isInteger(1) // true
// 判断传入值是否为正整数
util.isPositiveInteger(1) // true
// 判断传入值是否为浮点数
util.isFloat(1.1) // true
// 判断传入值是否为正浮点数
util.isPositiveFloat(1.1) // true
// 判断传入值是否为布尔值
util.isBoolean(true) // true
// 判断传入值是否为数组、类数组、arguments
util.isArray([]) // true
// 判断传入值是否为对象类型
util.isObject(new Map()) // true
// 判断传入值是否为 `{}`
util.isPlainObject({}) // true
// 判断传入值是否为 `Symbol` 类型
util.isSymbol(Symbol(1)) // true
// 判断传入值是否为函数类型
util.isFunction(() => {}) // true
// 判断传入值是否为 `NaN`
util.isNaN(NaN) // true
// 判断传入值是否为 `undefined`
util.isUndefined(undefined) // true
// 判断传入值是否为 `null`
util.isNull(null) // true
// 判断传入值是否为 `Error` 类型
util.isError(new Error) // true
// 判断传入的年份是否为闰年
util.isLeapYear(2024) // true
// 判断传入值是否为邮箱
util.isEmail('[email protected]') // true
// 判断传入值是否为 URL
util.isUrl('https://www.abc.com') // true
// 判断传入值是否为国内手机号
util.isCnPhoneNumber(18888888888) // true
// 判断传入值是否为国内身份证号码
util.isCnIdCard(111222333444555666) // false
// 判断传入值是否为假值
util.isFalsy(0) // true
// 比较两个值是否相等
util.isEqual(['1', '2', [3, 4, [5]]], ['1', '2', [3, 4, [5]]]) // true

// 将传入值转换为字符串
util.toString(1) // '1'
// 将传入值转换为数值
util.toNumber('1') // 1
// 将传入值转换为整数
util.toInteger('1.5') // 1
// 将传入值转换为 1~2 位浮点数
util.toFloat(1.256) // 1.25
// 将传入值转换为布尔值
util.toBoolean(0) // false
// 将传入值转换为数组
util.toArray(1) // [1]
// 将传入值转换为 `Symbol`
util.toSymbol(1) // Symbol(1)
// 将传入值转换为 `undefined`
util.toUndefined(1) // undefined
// 将传入值转换为 `null`
util.toNull(1) // null
// 将传入的中文首字母转为拼音
util.toCnLetter('你好') // ['NH']
// 将传入值转换为原始值
util.toOriginal('true') // true
// 人民币元转分
util.toCnCent(1.567) // 156
// 人民币分转元
// 要使用分转元,第二个参数需设为 false,第三个参数需设为 true,第四个参数用于判断保留1位还是2位小数
util.toCnCent(156, false, true) // '1.56'
// 将传入值的首字母改为大写
util.toTitle('welcome') // 'Welcome'
// 将传入值转换为百分比
util.toPercentage(0.1) // 10%
// 将传入值进行千分位格式化
util.toThousands(1234567) // 1,234,567

// 生成 uuid
util.genUuid() // '3e479fc2-ab2e-42bc-85f3-c592be4e0cb4'
// 生成随机整数
util.genRandomInteger() // 3

// 数据深拷贝
util.cloneDeep([1, 2, 3]) // [1, 2, 3]

// 解析按钮传递的 `event` 值
// <button data-id="{{ 1 }}" data-age="{{ 12 }}" bindtap="onClick" />
function onClick(e) {
  let id = util.parseDataset(e, 'age') // 12
}

// 接口同步写法
const res = util.apiPromisify(wx.getImageInfo)({ src: '' })
// 等同于
// wx.getImageInfo({
//   src: '',
//   success: res => {}
// })

// 获取图片信息的同步写法
const res = await util.getImageInfo(path)

// 执行当前页面栈的指定事件
// 已打开的页面中如果有 `getList` 函数就会执行
util.triggerEvent('getList')
util.triggerEvent('getList', {
  // 传递给页面栈事件的值
  detail: {
    id: 1
  },
  // 排除的页面栈索引值
  // 最后一个页面不执行该事件
  exclude: [-1]
})