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

safe-function

v1.0.8

Published

安全的 js 函数

Downloads

47

Readme

安全的 js 函数

safe-function

封装常用 js 函数,在函数内部处理异常报错,从此远离满屏 try catch

轻量,包大小 2kb

好用,1 分钟上手

放心用,绝对安全

用处

使用前

try {
  let name = JSON.parse('{"name": "safe"}')
} catch (error) {
  console.log(error)
}

try {
  let urlObj = new URL(url);
} catch (error) {
  console.log(error)
}

使用后

import { jsonPrase, newURL } from 'safe-function'

let obj = jsonPrase('{"name": "safe"}')

let urlObj = newURL(url);

快速开始

包下载:npm i safe-function

使用方法

CommonJS 格式:

const { newURL } = require('safe-function');

ES 模块格式:

import { newURL } from 'safe-function';

API 列表

URL

newURL(url: string, base?: string)

  • 获取 url 字符串解析对象,替代 new URL()
  • 返回:URL | {}

JSON

jsonPrase(text: string, reviver?)

  • json 字符串转 json 对象,替代 JSON.parse()
  • 返回:Object | null

jsonStringify(value: Object, replacer?, space?: number | string)

  • json 对象转 json 字符串,替代 JSON.stringify()
  • 返回:JSON 字符串

localStorage

setLocal(key: string, value, errCB?: Function)

  • 往 localStorage 写入数据,替代 localStorage.setItem()
  • errCB 回调函数,当 localStorage 存储数据超过最大容量时执行

getLocal(key: string)

  • 获取 localStorage 数据,替代 localStorage.getItem()
  • 返回:Object | null

removeOneLocal(key: string)

  • 清除某一个 key 的数据,替代 localStorage.removeItem()

removeAllLocal()

  • 清空 localStorage 全部数据,替代 localStorage.clear()

removeArrayLocal(keys: string[])

  • 清除指定数组 keys 的数据

DOM

getCptStyle(el: HTMLElement, att: string = '', after: string | null = null)

  • 获取元素计算属性,替代 getComputedStyle()
  • 返回:'' | Object | string

selectorOne(select)

  • 获取指定选择器的单个元素,替代 document.querySelector()
  • 返回:HTMLElement | null

selectorAll(select)

  • 获取指定选择器的全部元素,替代 document.querySelectorAll()
  • 返回:NodeListOf | []

insertEl(tag: string, content: string, id?: string)

  • 往 body 插入带内容元素

isStandardTag(tagName: string)

  • 判断当前浏览器是否支持标准标签
  • 返回:boolean

domReady(callback: () => void)

  • 当前页面 dom ready(DOMContentLoaded 或者已经 onload) 时触发 callback 函数

注意事项