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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hyjs/utils

v0.5.0

Published

Some common utility functions in JavaScript/Node.js

Readme

utils

一些常用的工具类

使用

npm i @hyjs/utils -S

#or yarn

yarn add @hyjs/utils -S

#or pnpm

pnpm i @hyjs/utils -S

getDevice 获取当前设备

getDevice();
// "iOS" | "Android" | "Web"

ieIE 是否IE

isIE();
// boolean

isMobile 是否移动端

isMobile();
// boolean

compressImage 压缩图片

await compressImage(file);
// Promise<Blob>

downloadFile 下载流文件

注意:在请求时需要设置 headersresponseType: blob

downloadFile(data, 'image/jpeg', 'filename');
// filename.jpeg

convertBase64ToFile Base64转File或Blob

await convertBase64ToFile(base64, 'file', 'filename');
// Promise<File>

await convertBase64ToFile(base64, 'blob', 'filename');
// Promise<Blob>

fileToBase64 File转Base64

await fileToBase64(file);
// Promise<string>

getAudioDuration 获取视频/音频时长

await getAudioDuration(file);
// 12s

getAccessType 获取类型函数

const accessType = getAccessType({});
// Object

const accessType = getAccessType(new RegExp());
// RegExp

const accessType = getAccessType(Symbol());
// Symbol

...

sleep 休眠函数

async function() {
  await sleep(3000);
  // 3s ----
  console.log('log');
}

debounce 防抖函数

// debounce(() => {}, 毫秒);
debounce(() => {}, 2000);

throttle 节流函数

// throttle(() => {}, 毫秒);
throttle(() => {}, 2000);

convertCurrency 数字转大写金额

convertCurrency(987654321);
// 玖亿捌仟柒佰陆拾伍万肆仟叁佰贰拾壹元整

numberToChinese 数字转大写数字

numberToChinese(987654321);
// 九億八仟七百六十五萬四仟三百二十一

filterObjectEmpty 过滤对象指定内容

filterObjectEmpty({
  a: undefined,
  b: null,
  c: '',
  d: 0
});
// { d: 0 }

filterObjectEmpty({
  a: undefined,
  b: 111,
  c: 222
}, [111, 222]);
// { a: undefined }

random4Code 生成4位code

random4Code();
// dsj1

randomChar 生成指定长度的字符,可选择number(数字), lowercase(小写字母), capital(大写字母)

randomChar();
// zZqt

randomChar(32);
// w2rAOdMRqhlhNEYzVUv2zw0Zp616rNFp

randomChar(32, ['number']);
// 05099593713036830668381743720300

randomNumber 生成数字

randomNumber(100);
// 32

randomNumber(1, 3);
// 2

currency 千分位分隔

currency(987654321);
// '987,654,321.00'

currency(987654321, 1);
// '987,654,321.0'

currency(987654321, 0);
// '987,654,321'

toHump 下划线转驼峰

toHump('a_bc_d_e');
// aBcDE

toLine 驼峰转下划线

toLine('aBcDE');
// a_bc_d_e

uuid 生成uuid

uuid();
// 15afbbae-a98b-b07c-df94-e2f916ac1cd1

dateFormatter 时间格式化

dateFormatter('YYYY-MM-DD hh:mm:ss');
// 2022-01-13 12:00:00

dateFormatter('YYYY-MM-DD hh:mm:ss', 'Thu Jan 13 2022 12:00:00 GMT+0800 (中国标准时间)')
// 2022-01-13 12:00:00

RegExp+Name 正则校验

使用anyRule在线正则查询

  • Strict 严谨的
  • Loose 宽松的
// RegExpIMEI 手机机身码(IMEI)
RegExpIMEI.test('123456789012345');

// RegExpURL 网址(URL)
RegExpURL.test('www.npmjs.com');
RegExpURL.test('https://www.npmjs.com');
...

getWxEnv 获取当前微信环境

⚠️微信内判断是否在小程序需要引入wx的sdk

const env = await getWxEnv();

switch(env) {
  case 'wx':
    // 微信内
    break;
  case 'mini-wx':
    // 小程序内
    break;
  case 'no-wx':
    // 非微信
    break;
  default:
    //未知
}