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

xctc-utils

v1.6.0

Published

localStorage存储 ``` sessionStorage存储 ``` crypto-js加密、解密 ``` 微信授权登录、微信分享 ``` 设备环境获取 ``` 是否是微信浏览器 ``` 时间戳转时间,字符串转时间戳

Downloads

142

Readme

项目中常用的方法集合

email: [email protected]

安装 npm i xctc-utils

更新到最新版本 npm install xctc-utils@latest

项目中引入 import useUtils from "xctc-utils"

LocalStorage使用,存取值时数据已经过处理

存储: useUtils.useLocalStorage(key,value)
取值: useUtils.getLocalStorage(key)

sessionStorage使用,存取值时数据已经过处理


存储: useUtils.useSessionStorage(key,value)
取值: useUtils.getSessionStorage(key)

将数据复制到剪贴板函数

useUtils.handleCopyValue(val)

删除缓存

key:string 需要删除的缓存键
isAll:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
如果 isAll 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
删除临时缓存: useUtils.removeSessionStorage(key,isAll)
删除永久缓存: useUtils.removeLocalStorage(key,isAll)

AES 加密、解密,同一个数据的加密和解密传入的key 和 iv保持一致。

work: 需要加密的对象,如传入的是对象,该方法默认进行JSON序列化处理。
key:16位或者32位字符串作为密钥
iv:16位或者32位字符串作为密钥偏移量
data: encrypt方法加密后返回的数据
加密:useUtils.encrypt( work:any , key:string , iv:string )
解密:useUtils.decrypt( data:string , key:string , iv:string )

类型判断

useUtils.type.isFunction(val) // 是否是函数
useUtils.type.isObject(val)   // 是否是对象 null 类型为对象,该方法已对null进行过滤
useUtils.type.isDate(val)     // 是否是时间对象
useUtils.type.isNumber(val)   // 是否是number类型
useUtils.type.isString(val)   // 是否是字符串类型
useUtils.type.isBoolean(val)  // 是否是boolean类型
useUtils.type.isWindow(val)    // 是否在浏览器环境下运行

获取当前设备环境

设备环境:


当前使用设备类型: useUtils.deviceEnvironment() // android ios
是否在微信浏览器环境中:  useUtils.weixinBrowser() // true false
是否是手持设备: useUtils.isMobile() // true 移动设备 false PC设备

crypto-js 加密解密方法

word: 需要加密的数据,数据可是字符串,对象等
key: 加密 密钥
iv:  密钥偏移量
data:encrypt方法加密后返回的数据
对同一个数据进行加密、解密时,encrypt和decrypt的iv和key保持一致
对word进行CryptoJS.enc.Utf8.parse转义
CryptoJS.AES.encrypt加密,加密模式:CBC ; padding: CryptoJS.pad.Pkcs7
对数据进行加密  
useUtils.encrypt(word:any,key:string,iv:string)
// 对数据进行解密
useUtils.decrypt(data:string,key:string,iv:string)

微信H5使用方法集合

微信授权登录
interface configOption {
    appId?:string, // 当前微信服务号 appId
    scope?:string,// 网页授权类型 默认 snsapi_userinfo
    http?:any, // 执行微信登录时,请求后端的接口方法,默认请求数据格式 {"app_id":"","js_code":"" },app_id为当前传入的配置参数,js_code为自动截取。
    codeKey?:string, // 回调地址栏中对 code 进行存储的键, codeKey:code
    stateKey?:string, // 回调地址栏中对 state 进行存储的键 stateKey:state
    cryptoiv?:string, // 将地址栏携带参数加密iv, 必须与 weixinShareInit 方法中的 iv 参数一致
    cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
}
进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
useUtils.weixinUrlCode(config)

微信config接口权限注入

interface ShareConfig{
    http:any, // 微信接口权限注入接口,后端接口,执行成功后返回数据格式:{appId:"",timestamp:"",noncestr:"",signature:""},成功后自动调取微信 wx.config 接口
    cb?:any, // wx.ready 成功后的回调方法
    appId:string, // 当前微信服务号 appId
    jsApiList?:string[],
}

useUtils.weixinShareConfig(config:ShareConfig)

微信分享接口加载

interface Share{
    title?:string, // 分享标题 默认值 微信分享
    desc?:string, // 分享描述 默认值 微信分享描述
    link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致,默认取当前访问地址
    imgUrl: '', // 分享图标
    data?:any,//微信分享时需要携带的数据,默认传键值格式 {id:1,name:"test",path:"微信分享跳转地址,如: /user/login,好友点击后自动跳转到/user/login页面"},系统自动将该数据保存到缓存中,同时将数据拼接到地址栏,跳转 path 页面后可通过地址栏获取拼接数据
    iv?:string, // 分享链接中对 state 数据加密的iv 必须与 weixinUrlCode 方法中的 cryptoiv 参数一致
    key?:string, // 分享链接中对 state 数据加密的 key 必须与 weixinUrlCode 方法中的 cryptokey 参数一致
}
useUtils.weixinShareInit(config:Share)

时间相关方法

获取当前时间的时间戳


useUtils.getTime() // 返回秒

时间戳转任意格式时间字符串

/**
 * 
 * @param num 传入的时间戳(秒):如 1675667655
 * @param format 解析后展示时间字符串格式,默认 "YYYY-MM-DD HH:mm:ss" 格式, 可传格式如: YYYY年MM月DD日 HH时 YYYY/MM/DD HH
 * @returns 
 */
useUtils.formatTimeStamp(num,format) // 返回 format 格式时间戳

时间字符串转时间戳

/**
 * 
 * @param str 字符串格式必须为: 2017/03/03 12:23:55 格式,避免兼容性,不能传 “-” 分割的时间字符串 2017-03-03
 * @returns 
 */
useUtils.formatStrTime(str) // 返回时间戳 秒