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

@wll8/shortid

v1.0.0

Published

基于时间戳的短唯一 ID 生成器

Readme

shortid

基于时间戳的短唯一 ID 生成器,使用 35/61 进制编码,在可读性和长度之间取得平衡。

安装

npm install @wll8/shortid

使用

const { ShortID, getTimestamp, encode, decode, setConfig } = require('@wll8/shortid')

// 简洁方式
const x = new ShortID()
x.gen()      // "3moZBwc1"
x.gen(3)     // ["3moZBwc1", "3moZBwc2", "3moZBwc3"]

// 自定义配置
const y = new ShortID({ case: 'upper' })
y.gen()      // "31HZB3W31"

// 底层函数
const [time, seq] = getTimestamp()
const id = [time, seq].map(encode).join('')
console.log(id) // 3moZBwc1

// 自定义配置
setConfig({ case: 'mixed' }) // 大小写混合 + 数字(61 进制)
setConfig({ case: 'lower' }) // 仅小写 + 数字(35 进制)
setConfig({ case: 'upper' }) // 仅大写 + 数字(35 进制),默认

// 编码/解码
encode(12345)        // "3Wb"
decode('3Wb')        // 12345
decode('3Wb', 'upper') // 显式指定解码模式

API

new ShortID(config?)

创建一个 ShortID 实例。

  • config.case: 'upper'(默认) | 'mixed' | 'lower'

shortid.gen(n?)

生成唯一 ID。

  • 无参数:返回单个 ID 字符串
  • n > 0:返回包含 n 个 ID 的数组
  • n = 0:返回空数组

setConfig({ case })

设置全局配置。

  • case: 'upper'(默认) | 'mixed' | 'lower'

不同模式使用独立的时间戳计数器,互不干扰。

getTimestamp(start?)

返回 [时间戳偏移, 序列号] 数组。基于当前时间与参考时间的差值生成偏移量,同一毫秒内序列号自动递增。

  • start: 参考时间戳,默认 2020-03-07

encode(num)

将十进制数字编码为当前字符集下的字符串。

decode(str, mode?)

将编码字符串解码回十进制数字。可选 mode 参数用于解码其他模式下生成的旧 ID。

getCharset(mode?)

返回当前或指定模式的字符集字符串。

字符集

| 模式 | 字符集 | 长度 | ID 长度 | |------|--------|:----:|:------:| | upper(默认) | 1-9A-Z | 35 | 9 位 | | mixed | 1-9A-Za-z | 61 | 8 位 | | lower | 1-9a-z | 35 | 9 位 |

设计

  • 时间戳 + 自增序列号,单进程唯一
  • 默认参考时间 2020-03-07,系统运行约 70 年 ID 不超过 9 位
  • 可读性优先,字符集跳过 0O 等易混淆字符
  • 配置可随时切换,不同模式的 ID 互不冲突