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

@15ms/shiki-id

v1.0.0

Published

A compact self-describing identifier encoder.

Downloads

56

Readme

@15ms/shiki-id

紧凑的自描述标识编码器:2 位结构格式 + 时间前缀 + 随机片段。

特性

  1. 强唯一性,追求成本平衡的低碰撞概率,概率唯一即可
  2. 时间有序,前缀编码创建时间,DB 索引聚簇友好,支持按时间范围查询(同一结构格式内成立)
  3. 自描述性,头部 2 位结构格式 f 由编码参数与纪元号可计算得出,解析方无需外部配置即可拆解;编码结构和纪元轮转可无损演进
  4. 架构无关,无状态,不引入中心化服务或节点注册
  5. 符号友好,大小写不敏感,零特殊符号,base36 字符集
  6. 容量紧凑,≤ 20 位,适配 varchar(20) DDL
  7. 业务无关,纯标识符,不携带实体类型、租户等信息

快速开始

import { buildIdFormat, IdGenerator } from '@15ms/shiki-id';

// (tLevel, cLevel, bLevel, epochIndex, parity) → 2 位格式 f
const format = buildIdFormat({ tLevel: 0, cLevel: 0, bLevel: 2, epochIndex: 0 });

const generator = new IdGenerator(format, { rSize: 4 });

generator.create();    // "400ayx00x7k2"   有序样式(系统内部)
generator.create();    // "400ayx01m3p9"
generator.decode(id);  // { t: '0ayx', c: '00', r: 'x7k2' }

生成器直接消费 f 字符串;rSize 缺省取容量上限(总长 20 封顶)。

ID 结构

{ 结构格式 f, 2 位 }{ 时间编码 t 位 }{ 自增轮转 c 位 }{ 随机片段 r 位 }

| 段 | 说明 | |----|------| | f | 由档位与纪元号打包的 2 位 base36,自描述 | | t | 桶编码的创建时间(tLevel 0~2 → 4~6 位) | | c | 进程内自增轮转(cLevel 0~1 → 2~3 位) | | r | 密码学安全随机后缀(4~14 位,不进 f) |

档位参数

| 参数 | 取值 | 说明 | |------|------|------| | tLevel | 0~2 | 时间编码档位(4~6 位) | | cLevel | 0~1 | 自增轮转档位(2~3 位) | | bLevel | 0~2 | 桶距档位:15 秒 / 5 分钟 / 1 小时 | | epochIndex | 0~35 | 纪元序号,0 号内置默认 2025-01-01 | | parity | 0~1 | 0 有序样式,1 无序样式 |

预设配置

| 配置 (t,c,b,e) | f | 桶距 | 寿命(t 位) | 总长 | |----------------|-----|------|--------------|------| | (0,0,2,0) | 40 | 1 小时 | ≈ 191 年(4 位) | 12 | | (0,0,1,0) | 20 | 5 分钟 | ≈ 16 年(4 位) | 12 | | (1,0,0,0) | c0 | 15 秒 | ≈ 29 年(5 位) | 15 |

纪元与轮转

import { indexOfEpoch } from '@15ms/shiki-id';

const epochs = [Date.UTC(2030, 0, 1)];               // 自定义纪元须晚于默认纪元,自序号 1 起
const generator = new IdGenerator(format, { epochs }); // 0 号内置默认,无需登记

indexOfEpoch(epochs, Date.now());                    // 最新的 ≤ now 纪元序号,全都晚于则 -1

寿命临近回绕时登记新纪元并切换生成器:新 f 自动不同,跨纪元 ID 永不碰撞,旧纪元 ID 仍可自解析。

无序样式(用户可见展示)

面向用户展示 ID 时用 parity: 1:段序反转为 {f}{r}{c}{t},随机片段成为视觉主体,同时段创建的 ID 可区分;解析方读 f 得 p = 1 即可机械还原,零额外存储。

const displayFormat = buildIdFormat({ tLevel: 0, cLevel: 0, bLevel: 2, epochIndex: 0, parity: 1 });
new IdGenerator(displayFormat, { rSize: 4 }).create();   // "41x2k700xya0"

碰撞率

以 (0,0,2,0)、1 小时桶距、1296 个轮转分组为例:

| 每桶 ID 数 | 碰撞率 | |-----------|--------| | 1,000 | ~10⁻⁷ | | 10,000 | ~0.002% | | 100,000 | ~0.18% |

文档