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

hhp-utils

v2.0.20

Published

utils

Downloads

30

Readme

hhp-utils

代码工具库

为避免在不同项目中多次复制粘贴,这里把日常可能会使用到的函数整合起来,统一封装,并发布到 npm。

安装

  1. npmyarn 安装
# npm
npm install hhp-utils --save
# yarn
yarn add hhp-utils --save

如何使用

  • npm 安装
// 按需引入(推荐)
import { getUrlParams } from 'hhp-utils'

const url = 'https://hhp1614.com/?name=hhp1614&pass=1234'
const urlParams = getUrlParams(url)

console.log('urlParams', urlParams)
// 全部引入
import * as hhpUtils from 'hhp-utils'

const url = 'https://hhp1614.com/?name=hhp1614&pass=1234'
const urlParams = hhpUtils.getUrlParams(url)

console.log('urlParams', urlParams)
  • 浏览器使用

直接下载 dist 目录下的 hhp-utils.min.umd.js 或者 hhp-utils.umd.js 使用

<script src="https://github.com/hhp1614/hhp-utils/blob/master/dist/hhp-utils.min.js"></script>
<script>
  const url = 'https://hhp1614.com/?name=hhp1614&pass=1234'
  const result = hhpUtils.url.getUrlParams(url)
  console.log(result)
</script>

目录

API

device

getExplore

获取浏览器类型及版本,不传参数则默认为当前浏览器 UA

返回类型:IEEDGEFirefoxChromeOperaSafariUnkonwn

interface IExplore {
  type: string
  version: string
}
getExplore(ua: string = navigator.userAgent): IExplore

// 例子
import { getExplore } from 'hhp-utils'

getExplore()

getOS

获取当前操作系统类型

返回值:MacOSXwindowslinuxiosandroidwindowsPhoneUnkonwn

getOS(): string

// 例子
import { getOS } from 'hhp-utils'

getOS()

isMobile

判断是否为移动端,不传参数则默认为当前浏览器 UA

isMobile(ua: string = navigator.userAgent): boolean

// 例子
import { isMobile } from 'hhp-utils'

isMobile()

func

throttle

节流

throttle(func: Function, delay: number = 160): Function

// 例子
const foo = () => {}
throttle(foo, 50)

debounce

防抖

debounce(func: Function, wait: number = 160): Function

// 例子
const foo = () => {}
debounce(foo, 50)

format

milliFormat

数字千位分隔符

milliFormat(value: string | number, fixed?: number): string

// 例子
import { milliFormat } from 'hhp-utils'

milliFormat(123456) // => 123,456
milliFormat(123456.789, 2) // => 123,456.79

| 参数 | 类型 | 说明 | | ------- | ------------------ | ------------------- | | value | numberstring | 传入的值 | | fixed | number | 保留 fixed 位小数 |

random

randomColor

返回随机颜色,如 #a1b2c3

randomColor(): string

// 例子
import { randomColor } from 'hhp-utils'

const color = randomColor()

randomNum

返回指定范围随机数

randomNum(min: number, max: number): number

// 例子
import { randomNum } from 'hhp-utils'

const num = randomNum(10, 20)

| 参数 | 类型 | 默认值 | | ----- | -------- | ------ | | min | number | 0 | | max | number | 1 |

regexp

isEmail

判断是否为邮箱地址

isEmail(str: string): boolean

// 例子
import { isEmail } from 'hhp-utils'

isEmail('[email protected]') // true

isIdCard

判断是否为身份证号

isIdCard(str: string | number): boolean

// 例子
import { isIdCard } from 'hhp-utils'

isIdCard(123456789012345678) // false

isPhoneNum

判断是否为手机号

isPhoneNum(str: string | number): boolean

// 例子
import { isPhoneNum } from 'hhp-utils'

isPhoneNum(12345689012) // false

isUrl

判断是否为 URL 地址

isUrl(str: string): boolean

// 例子
import { isUrl } from 'hhp-utils'

isUrl('https://hhp1614.com') // true

time

timeFormat

时间格式化,返回格式:YYYY-MM-DD HH:mm:ss

timeFormat(time: number = +new Date()): string

// 例子
import { timeFormat } from 'hhp-utils'

const time = +new Date('2019-5-27 18:5:35')
timeFormat(time) // 2019-05-27 18:05:35

| 参数 | 类型 | 默认值 | 说明 | | ------ | -------- | ------------- | ------------------------------ | | time | number | +new Date() | 毫秒数,默认为当前时间的毫秒数 |

timeFormatPass

距离现在已经过去的时间,返回:年前 | 月前 | 天前 | 小时前 | 分钟前 | 刚刚

timeFormatPass(startTime: number | string | Date): string

// 例子
import { timeFormatPass } from 'hhp-utils'

const time = +new Date()
// const time = new Date()
// const time = '2019-5-30 18:41:35'
timeFormatPass(time) // 刚刚

| 参数 | 类型 | 默认值 | 说明 | | ----------- | -------------------------- | ------ | ------------------ | | startTime | numberstringDate | | 距离现在的开始时间 |

timeFormatRemain

现在距离结束时间的剩余时间,返回:${d}天 ${h}小时 ${m}分钟 ${s}秒

timeFormatRemain(endTime: number | string | Date): string

// 例子
import { timeFormatRemain } from 'hhp-utils'

timeFormatRemain(1559318400000)
timeFormatRemain('2019-6-1')
timeFormatRemain('2019-6-1 9:30:00')
timeFormatRemain(new Date('2019-6-1'))

| 参数 | 类型 | 默认值 | 说明 | | --------- | -------------------------- | ------ | ------------------ | | endTime | numberstringDate | | 距离现在的结束时间 |

type

type

返回数据类型

返回值:numberstringbooleanundefinednullobjectarraydateerrorregexpfunctionmathjsonsymbol

type(value: any): string

// 例子
import { type } from 'hhp-utils'

const numType = type(123) // 'number'
const arrType = type([]) // 'array'

url

getUrlParams

获取 URL 参数对象,不传参数则默认使用 window.location.href

interface IUrlParams {
  [key: string]: any
}
getUrlParams(url = window.location.href): IUrlParams

// 例子
import { getUrlParams } from 'hhp-utils'

const urlParams = getUrlParams('https://hhp1614.com/?name=hhp1614&pass=1234')
console.log(urlParams)
// => { name: 'hhp1614', pass: '1234' }

urlQueryString

对象序列化

interface IUrlParams {
  [key: string]: any
}
urlQueryString(obj: IUrlParams): string

// 例子
import { urlQueryString } from 'hhp-utils'

const params = {
  name: '管理员',
  pass: '123456'
}
console.log(urlQueryString(params))
// => name=%E7%AE%A1%E7%90%86%E5%91%98&pass=123456

说明

本项目使用 typescript 开发,编译采用 UMDESNext 模块规范。

使用 mocha + chai + ts-node 进行单元测试.