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

@danep/utils

v1.1.2

Published

简单的工具库

Readme

@danep/utils 简单的工具库

优点

  • 无依赖,无副作用
  • 经过测试,代码覆盖率100%

dateFormat 格式化日期

type dateFormat = (date: Date, fmt: string) => string

dateFormat(new Date(), 'YYYY-M-D')  // 2020/1/1

// 若小于占位符长度,补0
dateFormat(new Date(), 'YYYY-MM-DD')// 2020/01/01

| 占位符 | 说明 | | ------ | ---- | | Y | 年 | | M | 月 | | D | 日 | | h | 时 | | m | 分 | | s | 秒 | | x | 毫秒 | | q | 季度 |

urlParse 解析url

type urlParse = (url: string) => object

urlParse('www.baidu.com?a=1&b=2&c=3#hash') // { a: '1', b: '2', c: '3' }
urlParse('http://www.baidu.com') // {}
urlParse('www.baidu.com?a=%E5%93%88') // { a: '哈' } , 参数解码
urlParse('www.baidu.com?a&b=&c=3') // { a: 'true', b: 'true', c: '3' } , 空字符串解析成true

isValidArray 判定有效数组

type isValidArray = (val: any) => boolean

// 是数组并且长度 >0
isValidArray([]) //false
isValidArray([1]) //true

isPromise 判定promise

type isPromise = (val: any) => boolean

isPromise( new Promise( ()=>{} ) ) // true
isPromise( { then: () => {}, catch: () => {} } ) // true

getValue 获取值

如果参数是个函数,会返回函数的值,否知返回参数

type getValue = (val: any) => any

getValue(1) // 1
getValue(() => true) //true

promiseCache 缓存promise

type promiseCreator<T> = (arg) => Promise<T>

type cahce<U> = {
  get: (arg: U) => any,
  set: <T> (val: T, arg: U) => T
}

type promiseCache<T> = (
  promiseCreator: promiseCreator<T>,
  cahce?: cahce<any>
) => promiseCreator<T>

function getUserId() {
  return fetch().then(id => promise.reslove(id))
}

getUserId = promiseCache(getUserId)

getUserId() // 第一次,fetch 发出请求
getUserId() // 第二次,请缓存读取,不执行fetch
getUserId.cahce.get() // 主动从缓存读取

// 默认缓存媒介
function createCache () {
  let cache = null;
  return {
    get () { return cache; },
    set (val) {
      cache = val;
      return cache;
    }
  };
}

// 自定义媒介
function createCache () {
  let cache = {};
  return {
    get (id) { return cache[id]; },
    set (val, id) {
      if (!val && !id) cache = {};
      else cache[id] = val;
      return val;
    }
  };
}

function getUserInfo(id) {
  return fetch(id).then(user => promise.reslove(user))
}

getUserInfo = promiseCache(getUserInfo, createCache())

getUserInfo(1) // 第一次获取1,fetch 发出请求
getUserInfo(1) // 第二次获取1,请缓存读取,不执行fetch
getUserInfo(2) // 第一次获取2,fetch 发出请求

promiseCancel 取消promise

type promiseCreator<T> = (arg) => Promise<T>
type promiseCancel<T, U> =
  (promiseCreator: promiseCreator<T>)
    => promiseCreator<[Promise<T | U>, (error: U) => void]>

function getUserId() {
  return fetch().then(id => promise.reslove(id))
}

getUserId = promiseCancel(getUserId)
const [promise,cancel] = getUserId()
cancel(error) // 调用 cancel ,会使promise进入 rejected 状态,值为error

promiseAutoCancel 自动取消promise

type promiseCreator<T> = (arg) => Promise<T>
type promiseAutoCancel<T> = (
  promiseCreator: promiseCreator<T>,
  error: any | (() => any)
) => promiseCreator<T>

function getUserId() {
  return fetch().then(id => promise.reslove(id))
}

getUserId = promiseAutoCancel(getUserId, new Error('自动取消'))

getUserId() // 自动reject, throw Error('自动取消')
getUserId() // 正常