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

hzs-utils

v1.0.8

Published

前端常用工具函数封装

Downloads

20

Readme


简介

前端项目开发中,会经常使用一些工具函数,随着项目数量的增多、工具函数的增多,在每个项目里面都维护一个 util.js 会带来不少的麻烦,所以还是维护一个工具函数包,希望大家也一并 commit!

安装使用

1. 安装包使用方式

安装

# npm
npm install hzs-utils -S

# yarn
yarn add hzs-utils -S

ESM 导入使用

import { random } from "hzs-utils";
console.log(random(1, 10));

RequireJS 导入使用

const { random } = require("hzs-utils");
console.log(random(1, 10));

2. 网页 script 直接引入使用方式

直接拷贝 dist 下的 hzs-utils-umd.js 到项目里面,可直接引用

<!DOCTYPE html>
<html lang="en">
  <body>
    <script src="/dist/hzs-utils-umd.js"></script>
    <script>
      console.log(dutils.random(1, 10));
    </script>
  </body>
</html>

函数概览

     /**
     * 生成数字范围内的随机数
     * @param min 最小数字
     * @param max 最大数字
     * @returns number类型
     */
    export function random(min: number, max: number): number

    /**
     * @desc 手机号脱敏
     * @param {String} num
     * @demo 155****8810
     */
    export function desensitizePhoneNumber(num: string): string


    /**
     * @desc 电子邮件脱敏
     * @param {String} email
     */
    export function desensitizeEmail(email: string): string

    /**
     * @desc 身份证号脱敏
     * @param {String} idCard
     */
    export function desensitizeIdCard(idCard: string): string
    /**
     * @desc 姓名脱敏
     * @param {String} name
     */
    export function desensitizeName(name: string): string

    /**
    * 节流函数
    * @param func - 要节流的函数
    * @param wait - 节流时间间隔,默认为 500 毫秒
    * @param immediate - 是否立即执行,默认为 true
    * @returns 节流后的函数
    */
    export function throttle<T extends (...args: any[]) => any>(func: T, wait: number = 500, immediate: boolean = true): T
    /**
     * 防抖函数
     * 防抖原理:一定时间内,只有最后一次操作,再过 wait 毫秒后才执行函数
     *
     * @param func 要执行的回调函数
     * @param wait 延时的时间,默认为 500 毫秒
     * @param immediate 是否立即执行,默认为 false
     * @returns 防抖后的函数
     */
    export function debounce<T extends (...args: any[]) => any>(func: T, wait: number = 500, immediate: boolean = false): T
    /**
     * 深度克隆
     *
     * @param obj 需要深度克隆的对象
     * @param cache 缓存
     * @returns 克隆后的对象或者原值(不是对象)
     */
    export function deepClone(obj: any, cache: WeakMap<any, any> = new WeakMap()): any;

    /**
     * 将对象转换为 URL 参数字符串
     *
     * @param data 需要转换的对象
     * @param isPrefix 是否自动加上 "?"
     * @param arrayFormat 数组格式化规则  例如data: {
                name: '冷月夜',
                fruits: ['apple', 'banana', 'orange']
            }
     * indices ?name=冷月夜&fruits[0]=apple&fruits[1]=banana&fruits[2]=orange
     * brackets ?name=冷月夜&fruits[]=apple&fruits[]=banana&fruits[]=orange
     * repeat ?name=冷月夜&fruits=apple&fruits=banana&fruits=orange
     * comma  ?name=冷月夜&fruits=apple,banana,orange

     * @returns 转换后的 URL 参数字符串
     */
    export function getQueryParams(
        data: Record<string, any> = {},
        isPrefix: boolean = true,
        arrayFormat: 'indices' | 'brackets' | 'repeat' | 'comma' = 'brackets'
    ): string
    /**
     * 验证电子邮箱格式
     * @param value 待验证的字符串
     * @returns 如果验证通过,返回true,否则返回false
     */
    export function isEmail(value: string): boolean
    /**
     * 验证手机格式
     * @param value 待验证的字符串
     * @returns 如果验证通过,返回true,否则返回false
     */
    export function isMobile(value: string): boolean
    /**
     * 验证身份证号码
     * @param value 待验证的字符串
     * @returns 如果验证通过,返回true,否则返回false
     */
    export function isIdCard(value: string): boolean
     /**
     * 读取文件类型参数转换为 Base64 编码
     * @param file 接受一个 File 类型参数。
     * @returns 通过 Promise 异步处理文件读取和转换过程。 
     * 成功时调用 resolve 并返回结果。
     * 失败时调用 reject 并传递错误信息。
     */
    export function getBase64(file: File)