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

general-methods

v0.0.26

Published

封装一些常用的方法

Readme

更新日志

版本| 日期 | 方法 | 内容 |修改者 -|------------|-------|---------------------------------|- v0.0.26| 2025-06-04 | processId | 新增随机数 |charon-npm v0.0.24| 2024-04-28 | sm2 | sm2加密 |charon-npm v0.0.23| 2024-04-11 | exportData | 修复功能 |charon-npm v0.0.22| 2024-04-11 | exportData | 优化导出功能当数据为空时判断 |charon-npm v0.0.21| 2024-03-18 | isInnerIp | 优化 |charon-npm v0.0.20| 2024-03-18 | isInnerIp | 新增判断是否是内网ip |charon-npm v0.0.19| 2024-01-29 | -- | 优化文档 |charon-npm v0.0.18| 2024-01-29 | exportData | 新增导出方法 |charon-npm v0.0.17| 2024-01-08 | -- | 优化文档 |charon-npm v0.0.16| 2024-01-08 | getWeek | 新增获取当前星期的方法 |charon-npm v0.0.15| 2023-12-27 | -- | 优化文档 |charon-npm v0.0.14| 2023-12-27 | -- | 新增防抖函数(debounce)和节流函数(throttle) |charon-npm v0.0.13| 2023-12-12 | encrypt | 修复返回异常 |charon-npm

使用

安装

npm install general-methods --save-d
特别注意在项目中一般配置的是全局方法
generalMethods可以直接用this.$methods来代替配置该方法文件在src/utils/methods.js

使用

import generalMethods from 'general-methods'

formatDate

/**日期时间格式转换
 * 使用例子:generalMethods.formatDate(date, format)
 * @param date 时间戳
 * @param format 格式化字符串yyyy-MM-dd HH:mm:ss
 * */
const time = new Date().getTime()
time = generalMethods.formatDate(time, 'yyyy-MM-dd HH:mm:ss')

// res
time = '2023-11-21 11:21:23'

getWeek

/**获取当前星期几
 * 使用例子:generalMethods.getWeek(weekData)
 * @param weekData 星期数组,默认值:['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
 * */
week = generalMethods.getWeek()

// res
week = '星期x'

clearFields

/**清空对象里的值
 * 使用例子:generalMethods.clearFields(Fields)
 * @param Fields 需要清空的对象
 * */
const obj = { name: '张三', age: 25, interest: ['篮球', '足球'] }
generalMethods.clearFields(obj)

// res
obj = { name: '', age: '', interest: [] }

flatLevelDataToThreeData

/**平级数据转换成树形数据
 * 使用例子:generalMethods.flatLevelDataToThreeData(data, children, id, pId, sort)
 * @param data 需要转换的数组
 * @param children 子级对应的字段默认children
 * @param id 唯一标识对应的字段默认id
 * @param pId 父级id对应的字段默认pId
 * @param sort 排序对应的字段
 * */
const data = [
  { id: 1, pId: 0, name: '花园', sort: 1 },
  { id: 2, pId: 0, name: '果园', sort: 3 },
  { id: 3, pId: 0, name: '动物园', sort: 2 },
  { id: 4, pId: 1, name: '玫瑰', sort: 1 },
]
data = generalMethods.flatLevelDataToThreeData(data, 'children', 'id', 'pId', 'sort')
// res
data = [
  {
    "id": 1,
    "pId": 0,
    "name": "花园",
    "sort": 1,
    "children": [
      {
        "id": 4,
        "pId": 1,
        "name": "玫瑰",
        "sort": 1
      }
    ]
  },
  {
    "id": 3,
    "pId": 0,
    "name": "动物园",
    "sort": 2
  },
  {
    "id": 2,
    "pId": 0,
    "name": "果园",
    "sort": 3
  }
]

encrypt

/**RSA加密
 * 使用例子:generalMethods.encrypt(password, publicKey)
 * @param password 需要加密的字符串
 * @param publicKey 公钥
 * */
const password = 'admin'
const publicKey = 'MIIBIjANBgkqhk'
password = generalMethods.encrypt(password, publicKey)

// res
password = 'd6SbpOG59G7QoUK6ceheKBNqrUK74Jh3LTjBo2yC+fv6TnZZ+Q9BticXa5n2Ep3'

decrypt

/**RSA解密
 * 使用例子:generalMethods.decrypt(value, privateKey)
 * @param value 需要加密的字符串
 * @param privateKey 秘钥
 * */
const value = 'admin'
const privateKey = 'MIIBIjANBgkqhk'
value = generalMethods.decrypt(value, privateKey)

// res
value = 'd6SbpOG59G7QoUK6ceheKBNqrUK74Jh3LTjBo2yC+fv6TnZZ+Q9BticXa5n2Ep3'

sm2

/**国密算法加密
 * 使用例子:generalMethods.sm2(password, publicKey)
 * @param password 需要加密的字符串
 * @param publicKey 公钥
 * */
const password = 'admin'
const publicKey = 'MIIBIjANBgkqhk'
password = generalMethods.sm2(password, publicKey)

// res
password = 'd6SbpOG59G7QoUK6ceheKBNqrUK74Jh3LTjBo2yC+fv6TnZZ+Q9BticXa5n2Ep3'

formatId

/**根据id获取数组中的名称
 * 使用例子:generalMethods.formatId(id, obj)
 * @param id id值
 * @param obj 数据,包含data[],value字段,label字段
 * */
const id = 1
const name = ''
const obj = {
  data: [
    { id: 1, name: '张三' },
    { id: 2, name: '李四' },
    { id: 3, name: '王五' },
  ],
  value: 'id',
  label: 'name'
}
name = generalMethods.formatId(id, obj)

// res
name = '张三'

formatIds

/**根据id组获取数组中的名称集合
 * 使用例子:generalMethods.formatId(ids, obj)
 * @param ids ids数组集合
 * @param obj 数据,包含data[],value字段,label字段
 * */
const ids = [1, 2]
const names = ''
const obj = {
  data: [
    { id: 1, name: '张三' },
    { id: 2, name: '李四' },
    { id: 3, name: '王五' },
  ],
  value: 'id',
  label: 'name'
}
names = generalMethods.formatId(ids, obj)

// res
names = '张三、李四'

exportJsonToExcel

/**将json导出为excel
 * 使用例子:generalMethods.exportJsonToExcel(th, filterVal, jsonData, defaultTitle)
 * @param th 表头数据集合,例:['姓名', '性别', '年龄']
 * @param filterVal 导出的值对应的字段,例:['name', 'sex', 'age']
 * @param jsonData 要导出的数据数组
 * @param defaultTitle 导出的文件标题
 * */

exportTableToExcel

/**导出某个id的表格
 * 使用例子:generalMethods.exportTableToExcel(id)
 * @param id 表格的id
 * */

getUrlParams

/**获取浏览器地址栏参数
 * 使用例子:generalMethods.getUrlParams(key, decode)
 * @param key(string) 需要获取的key值
 * @param decode(boolean) 是否采用decodeURIComponent进行解码
 * */

debounce

/**防抖函数,当一个动作一段时间连续触发,只执行最后一次,例如搜索查询,避免用户在未全部输入完整时直接请求接口
 * 使用例子:generalMethods.debounce(fun, time)
 * @param fun(function) 需要执行的函数
 * @param time(number) 时间(ms),默认500ms
 * */
// 示例
function testFunction() {
  generalMethods.debounce(async () => {
    console.log('执行代码')
  }, 500)
}

throttle

/**节流函数,限制一个函数在一定时间内只能执行一次,例如提交按钮,避免重复提交
 * 使用例子:generalMethods.throttle(fun, time)
 * @param fun(function) 需要执行的函数
 * @param time(number) 时间(ms),默认500ms
 * */
// 示例
function testFunction() {
  generalMethods.throttle(async () => {
    console.log('执行代码')
  }, 500)
}

exportData

/**导出函数
 * 使用例子:generalMethods.exportData(fun, title)
 * @param fun(function) 导出接口函数
 * @param title(string) 导出文件标题
 * */
// 示例
const body = {
  pageNum: this.pageNum,
  pageSize: this.pageSize, 
  ...this.searchForm
}
generalMethods.exportData(this.$requestData.exportHouse(body), title).then(() => {
  this.$message({
    type: 'success',
    duration: 1000,
    message: '导出成功'
  })
})
//注意:接口配置需要新增 responseType: 'blob'
//return request({ url: '/api/report/export',  method: 'get', data: '', params: body, responseType: 'blob' })

isInnerIp

/**获取浏览器地址栏参数
 * 使用例子:generalMethods.isInnerIp()
 * */

processId

/**获取随机id
 * 使用例子:generalMethods.processId()
 * */