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

edon-helper

v0.0.11-beta.14

Published

一个功能丰富的实用工具函数库,包含 URL 处理、数据转换、设备检测、时间工具等多种常用功能,帮助开发者快速实现常见功能需求

Readme

edon-helper

一个功能丰富的实用工具函数库,包含 URL 处理、数据转换、设备检测、时间工具等多种常用功能,帮助开发者快速实现常见功能需求。

📦 安装

# 使用 pnpm
pnpm install edon-helper

# 使用 yarn
yarn add edon-helper

🌟 工具介绍

常用工具

提供多种常用工具函数,如睡眠函数、UUID 生成、随机数生成等。

import {
  sleep,
  generateUUID,
  randomBetween,
  isValidJson,
  isEmpty,
  debounce,
  throttle,
  simulateProgress,
} from 'edon-helper'

// 延迟执行
await sleep(1000)

// 生成UUID
const id = generateUUID()
const shortId = generateUUID(16)

// 生成随机数
const randomNum = randomBetween(1, 100)

// 检查JSON有效性
const valid = isValidJson('{"name": "John"}')

// 检查值是否为空
const empty = isEmpty({})

// 防抖函数
const debouncedFunction = debounce(() => {
  console.log('防抖函数执行')
}, 300)

// 节流函数
const throttledFunction = throttle(() => {
  console.log('节流函数执行')
}, 300)

// 进度模拟
simulateProgress(
  3000,
  (progress) => {
    console.log(`当前进度: ${progress}%`)
  },
  {
    smoothness: 50,
    easing: true,
  }
)

数字格式化工具集:fmtNum

功能完善的数字格式化工具,支持数字展示、金额处理、手机号格式化、时长转换、精确计算等多种场景。

import { fmtNum } from 'edon-helper'

// ============ 基础格式化 ============
// 千分位格式化
fmtNum.thousand(1234567) // "1,234,567"
fmtNum.thousand(12345.678, 2) // "12,345.68"

// 货币格式化
fmtNum.currency(123456789, '$') // "$123,456,789.00"
fmtNum.currency(12345.67, '¥') // "¥12,345.67"

// 小数处理
fmtNum.decimal(10.12323, 2) // 10.12
fmtNum.decimal(10.125, 2) // 10.13

// 百分比
fmtNum.percent(0.12345, 2) // 12.35
fmtNum.percent(0.5) // 50

// 数字补零
fmtNum.padZero(12, 5) // "00012"

// 文件大小格式化
fmtNum.fileSize(123456789) // "117.74 MB"
fmtNum.fileSize(1024) // "1.00 KB"

// 随机数生成
fmtNum.random(4, 10) // 7(整数)
fmtNum.random(4, 10, { decimal: 2 }) // 6.89(小数)

// ============ 数字缩写(社交媒体、数据看板常用)============
fmtNum.abbreviate(1000) // "1.0K"
fmtNum.abbreviate(1234567) // "1.2M"
fmtNum.abbreviate(1234567890) // "1.2B"
fmtNum.abbreviate(1000000000000) // "1.0T"
fmtNum.abbreviate(1234567, 2) // "1.23M" (自定义小数位)

// ============ 手机号格式化 ============
fmtNum.formatPhone('13812345678', 'space') // "138 1234 5678"
fmtNum.formatPhone('13812345678', 'hyphen') // "138-1234-5678"
fmtNum.formatPhone('13812345678', 'hide') // "138****5678"

// ============ 时长格式化 ============
fmtNum.formatDuration(3665, 'hms') // "1小时1分5秒"
fmtNum.formatDuration(3665, 'colon') // "01:01:05"
fmtNum.formatDuration(125, 'short') // "2分5秒" (自动省略为0的单位)

// ============ 金额处理 ============
// 元转分
fmtNum.yuanToFen(123.456789, 2) // 12345.68
fmtNum.yuanToFen(10) // 1000

// 分转元
fmtNum.fenToYuan(12345.678, 2) // 123.46
fmtNum.fenToYuan(1000) // 10

// 金额单位转换
fmtNum.amount(123.456789, 'yuan', 'fen', 2) // 12345.68
fmtNum.amount(12345.678, 'fen', 'yuan', 2) // 123.46

// 人民币大写
fmtNum.rmbUpper(12345.67) // "壹万贰仟叁佰肆拾伍元陆角柒分"
fmtNum.rmbUpper(10000) // "壹万元"
fmtNum.rmbUpper(0.01) // "壹分"

// ============ 精确计算(解决浮点数精度问题)============
fmtNum.add(0.1, 0.2) // 0.3 (而不是 0.30000000000000004)
fmtNum.subtract(0.3, 0.1) // 0.2
fmtNum.multiply(0.1, 0.2) // 0.02
fmtNum.divide(0.3, 0.1) // 3
fmtNum.divide(1, 3, 2) // 0.33 (指定小数位数)

类型定义

thousand(num: number | string, decimalPlaces?: number): string | undefined
currency(num: number | string, symbol: string): string | undefined
decimal(num: number | string, places: number): number | undefined
percent(num: number | string, places?: number): number | undefined
yuanToFen(yuan: number | string, decimalPlaces?: number): number | undefined
fenToYuan(fen: number | string, decimalPlaces?: number): number | undefined
amount(amount: number | string, fromUnit: 'yuan' | 'fen', toUnit: 'yuan' | 'fen', decimalPlaces: number): number | undefined
rmbUpper(num: number | string): string | undefined
random(min: number | string, max: number | string, options?: { decimal?: number }): number | undefined
fileSize(bytes: number | string): string | undefined
padZero(num: number | string, length: number): string | undefined
abbreviate(num: number | string, decimalPlaces?: number): string | undefined
formatPhone(phone: number | string, format?: 'space' | 'hyphen' | 'hide'): string | undefined
formatDuration(seconds: number | string, format?: 'hms' | 'colon' | 'short'): string | undefined
add(a: number | string, b: number | string): number | undefined
subtract(a: number | string, b: number | string): number | undefined
multiply(a: number | string, b: number | string): number | undefined
divide(a: number | string, b: number | string, decimalPlaces?: number): number | undefined

URL处理

提供 URL 查询参数的获取、设置和删除功能。

import { getURLParams, setURLParams, delURLParams } from 'edon-helper'
// 获取URL参数
const params = getURLParams<{ id: string; type: string }>()
console.log(params.id, params.type)
// 设置URL参数
setURLParams('page', '1')
setURLParams({ page: '1', id: '2' })
// 删除URL参数
delURLParams('filter')

数据转换

包含下划线命名与驼峰命名的相互转换,以及字节大小格式化功能。

import { dataConvert, convertBytesSize } from 'edon-helper'

// 下划线转驼峰
const camelCaseObj = dataConvert({ user_name: 'John', age: 30 }, 'toCamel')
// { userName: 'John', age: 30 }

// 驼峰转下划线
const underscoreObj = dataConvert({ userName: 'John', age: 30 }, 'toUnderscore')
// { user_name: 'John', age: 30 }

// 字节大小格式化
console.log(convertBytesSize(1024)) // "1.00 KB"
console.log(convertBytesSize(1048576, 1)) // "1.0 MB"

设备检测

检测当前设备类型、浏览器环境等信息。

import { isMobile, isWx, isAndroid, isIos } from 'edon-helper'
if (isMobile()) {
  console.log('当前是移动设备')
}
if (isWx()) {
  console.log('当前在微信浏览器中')
}
if (isAndroid()) {
  console.log('当前是安卓设备')
}
if (isIos()) {
  console.log('当前是iOS设备')
}

日志记录

提供功能完善的日志记录工具,支持不同日志级别和样式。

import { Logger, logger } from 'edon-helper'

// 使用默认日志器
logger.info('这是一条信息日志')
logger.success('这是一条成功日志')
logger.warn('这是一条警告日志')
logger.error('这是一条错误日志')
logger.debug('这是一条调试日志')

// 自定义日志器
const customLogger = new Logger({
  showTimestamp: true,
  showLevel: true,
  enableStyle: true,
  prefix: 'MyApp',
})
customLogger.info('自定义日志器')

时间处理

强大的时间处理工具,基于 dayjs 扩展,支持多种时间操作。

import { timeUtil } from 'edon-helper'

// 格式化日期
console.log(timeUtil.formatDate()) // 2023-10-01
console.log(timeUtil.formatDateTime()) // 2023-10-01 12:00:00

// 计算时间差
const diff = timeUtil.diff('2023-01-01', '2023-01-10')
console.log(diff) // "9天 0小时 0分钟 0秒"

// 相对时间
console.log(timeUtil.fromNow('2023-01-01')) // "9个月前"

// 检查日期是否在范围内
const inRange = timeUtil.isBetween('2023-05-01', '2023-01-01', '2023-12-31')
console.log(inRange) // true

环境变量处理

处理环境变量和生成类型定义文件

import { getDotEnvObject, generateEnvTypes, joinRoot } from 'edon-helper'

// 获取环境变量对象
const envObj = await getDotEnvObject('dev')

// 生成环境变量类型定义
await generateEnvTypes('my-project', envObj)

// 拼接路径
const path = joinRoot('src', 'utils')

IP 地址获取

获取本地 IP 地址信息。

import { getLocalIPs } from 'edon-helper'

const ips = getLocalIPs()
console.log('IPv4地址:', ips.ipv4)
console.log('IPv6地址:', ips.ipv6)

Node 环境工具

提供 Node.js 环境相关工具函数。

import { isESM, safeRequire } from 'edon-helper'

// 检查是否为ESM环境
if (isESM()) {
  console.log('当前是ESM环境')
}

// 安全引入模块
const lodash = await safeRequire('lodash')

// 如果确定当前引入的包是esm
const lodash = await safeRequire('lodash-es', true)

优雅的加载动画

提供终端加载动画效果。

import { elegantSpinner } from 'edon-helper'

const spinner = elegantSpinner()
setInterval(() => {
  process.stdout.write(`\r${spinner()}`)
}, 50)

⛓️‍💥 相关生态工具

本工具库可与以下生态工具配合使用,提升开发效率:

  • edon-builder:一个基于esbuild的快速TypeScript项目构建工具,支持多格式输出(esm、cjs)、实时开发、代码混淆等功能,简化项目构建流程。点击前往了解>>

  • edon-config:一个统一代码规范工具集,集成 Prettier、ESLint、Commitlint 预设配置,支持 TypeScript/React 项目,遵循大厂代码规范,减少团队协作中的风格冲突。点击前往了解>>

  • edon-service:一个开箱即用的前端构建工具,基于Webpack封装,简化了前端项目的开发与构建流程。无论是开发环境的热更新、资源处理,还是生产环境的代码优化、打包部署,都能提供便捷的配置和稳定的性能。点击前往了解>>

  • edon-wbuilder:一个专为npm库、UI组件库设计的构建工具,基于Vite、Rollup、Esbuild打造,简化了库开发的构建流程,支持 React、Vue等主流框架,自动处理TypeScript类型生成、代码转换和产物优化。点击前往了解>>