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

@upthen/utils

v1.0.9

Published

A collection of utility functions for upthen projects.

Readme

@upthen/utils

一个轻量级的工具函数库,为 upthen 项目提供常用的工具函数。

安装

npm install @upthen/utils

使用

import { safeJsonParse, sleep, uuid } from "@upthen/utils";

API 文档

JSON 工具

safeJsonParse

安全解析 JSON 字符串,避免解析错误导致程序崩溃。

const safeJsonParse = <T>(jsonString: string | null, defaultValue?: T): T | null

参数:

  • jsonString: 待解析的 JSON 字符串
  • defaultValue: 解析失败时的默认值

返回值: 解析结果或默认值

示例:

// 解析成功的情况
const validJson = '{"name": "John", "age": 30}';
const result1 = safeJsonParse(validJson);
console.log(result1); // 输出: { name: "John", age: 30 }

// 解析失败的情况
const invalidJson = '{name: "John", age: 30}';
const result2 = safeJsonParse(invalidJson, { name: "Default", age: 0 });
console.log(result2); // 输出: { name: "Default", age: 0 }

REM 布局工具

getRemPxTransBase

获取 REM 转换 PX 的基准值,用于响应式布局。

const getRemPxTransBase = (baseWidth?: number) => number;

参数:

  • baseWidth: 基准宽度,默认为 1920,以基准宽度下 1rem 为 100px

返回值: REM 转换基准值

示例:

const base = getRemPxTransBase(1920);
console.log(base); // 输出计算后的基准值

异步工具

sleep

异步等待指定的毫秒数。

const sleep = (ms: number): Promise<void>

参数:

  • ms: 需要等待的毫秒数

返回值: 返回一个 Promise,在指定的毫秒数后 resolve

示例:

async function demo() {
  console.log("开始等待");
  await sleep(2000);
  console.log("等待 2000 毫秒后执行");
}
demo();

随机数工具

uuid

生成符合 UUID v4 标准的唯一标识。

const uuid = () => string;

返回值: 符合 UUID v4 格式的唯一字符串

示例:

const id = uuid();
console.log(id); // 输出类似 "f47ac10b-58cc-4372-a567-0e02b2c3d479" 的字符串

generateRandomCode

生成指定长度的随机码,包含字母和数字。

const generateRandomCode = (length: number) => string;

参数:

  • length: 随机码的长度,若未提供或小于 1,则默认长度为 6

返回值: 生成的随机码字符串

示例:

const code1 = generateRandomCode(); // 默认长度 6
const code2 = generateRandomCode(8); // 指定长度 8
console.log(code1); // 输出类似 "aB3cD7" 的字符串
console.log(code2); // 输出类似 "xY9zW2v1" 的字符串

文件工具

base64ToImage

将 Base64 字符串转换为 HTMLImageElement 对象。

const base64ToImage = (base64String: string): Promise<HTMLImageElement>

参数:

  • base64String: 用于转换的 Base64 字符串

返回值: 返回一个 Promise,该 Promise 解析为 HTMLImageElement 对象,转换失败时拒绝

示例:

const base64Str = "data:image/png;base64,iVBORw0KG...";
base64ToImage(base64Str)
  .then((img) => {
    document.body.appendChild(img);
  })
  .catch((error) => {
    console.error("图片转换失败:", error);
  });

base64ToBlob

将 Base64 数据转换为 Blob 对象。

const base64ToBlob = (base64Data: any) => Blob;

参数:

  • base64Data: 用于转换的 Base64 数据

返回值: 返回一个表示图片的 Blob 对象

示例:

const base64Str = "data:image/png;base64,iVBORw0KG...";
const blob = base64ToBlob(base64Str);
console.log(blob); // 输出转换后的 Blob 对象

浏览器权限工具

requestMicrophonePermission

请求麦克风权限。

const requestMicrophonePermission = async () => void

示例:

await requestMicrophonePermission();

requestCameraPermission

请求摄像头权限。

const requestCameraPermission = async () => void

示例:

await requestCameraPermission();

requestScreenSharePermission

请求屏幕分享权限。

const requestScreenSharePermission = async () => void

示例:

await requestScreenSharePermission();

requestMediaPermission

请求麦克风和摄像头权限。

const requestMediaPermission = async () => void

示例:

await requestMediaPermission();

requestAllPermission

请求麦克风、摄像头和屏幕分享权限。

const requestAllPermission = async () => void

示例:

await requestAllPermission();

hasMicrophonePermission

检查是否已获取麦克风权限。

const hasMicrophonePermission = () => Promise<boolean>;

返回值: 返回一个 Promise,resolve 时返回布尔值表示是否已获取麦克风权限

示例:

const hasPermission = await hasMicrophonePermission();
console.log(hasPermission); // true or false

hasCameraPermission

检查是否已获取摄像头权限。

const hasCameraPermission = () => Promise<boolean>;

返回值: 返回一个 Promise,resolve 时返回布尔值表示是否已获取摄像头权限

示例:

const hasPermission = await hasCameraPermission();
console.log(hasPermission); // true or false

hasScreenSharePermission

检查是否已获取屏幕分享权限。

const hasScreenSharePermission = () => Promise<boolean>;

返回值: 返回一个 Promise,resolve 时返回布尔值表示是否已获取屏幕分享权限

示例:

const hasPermission = await hasScreenSharePermission();
console.log(hasPermission); // true or false

hasAllPermission

检查是否已获取麦克风、摄像头和屏幕分享权限。

const hasAllPermission = () => Promise<boolean>;

返回值: 返回一个 Promise,resolve 时返回布尔值表示是否已获取所有权限

示例:

const hasAll = await hasAllPermission();
console.log(hasAll); // true or false