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

@rayuse/utils

v0.0.3-beta.6

Published

存放一些零散、未分类的工具函数

Downloads

3

Readme

utils

常用的一些没有特定分类的工具函数。

引用方式

import { fixedRound, ... } from '@rayuse/utils'

parseTime

格式化时间

parseTime(new Date(2021, 10, 24), '{y}/{m}/{d}') // '2021/11/24'
parseTime(1637743490387, '{y}/{m}/{d}') // '2021/11/24'
parseTime('2021-11-24', '{y}/{m}/{d}') // '2021/11/24'

parseTime(1637743490387, '{y}/{m}/{d} {h}:{i}:{s}') // '2021/11/24 16:11:44'
parseTime('2021-11-24', '{y}/{m}/{d} {h}:{i}:{s}') // 2021/11/24 00:00:00

fixedRound

四舍五入保留指定位数小数,并自动补零

::: tip 函数内部对精度丢失问题进行了优化 :::

众所周知,JavaScript 存在精度丢失的问题,0.1 + 0.2 !== 0.3。这就给我们在进行数字处理,包括但不限于四则运算等情况下带来了烦恼。

这个函数则是将原始值扩大相应倍数后进行运算,将运算结果再还原倍数后 Math.round,这样就可以有效地规避精度丢失问题。

fixedRound(0.2577) // 0.26
fixedRound(0.1 + 0.2) // 0.30
fixedRound(0.5234575, 4) // 0.5235

sleep

::: tip 灵感来自于其它语言的 sleep 函数 ::: 指定代码暂停运行 x 毫秒

  • 场景1: 前端模拟接口请求时的 loading 状态
  • 场景2: 更新补充中...
// 场景1
const loading = ref(false)
async function getData() {
	loading.value = true
	await sleep(2000) // 两秒后改变 loading 状态
	loading.value = false
}

getVal

安全获取对象属性,提供兜底值

  • 场景1: 读取对象上不存在的属性,并提供缺省值

  • 场景2: 对象值为 undefined 或者 null 时,提供缺省值

const a = { b: 1, c: 2, e: null }

getVal(() => a.d, 0) // 0 因为不存在属性 c,正常读取会报错可能导致代码终止运行,此时可通过此函数避免报错并提供页面友好的缺省值
getVal(() => a.e, '--') // '--'

formatCashType

千分计数法

formatCashReg('1234567') // '1,234,567'
formatCashReg('1234567.12') // '1,234,567.12'

upDigit

将金额转换为大写

upDigit(1234.12) // '壹仟贰佰叁拾肆元壹角壹分整'
upDigit(1234) // '壹仟贰佰叁拾肆元整'

isBlankObj

判断是否为空对象

isBlankObj({}) // true
isBlankObj(null) // false
isBlankObj(undefined) // false
isBlankObj('') // false
isBlankObj([]) // true