普贤教程 › 包参考
nanoid 包
NanoID(URL 友好随机 ID)
| 项 | 值 |
|---|---|
| 包名 | nanoid |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | nanoid.px |
| 分类 | 标识、校验与版本 |
| 文件数 / 行数 | 1 / 109 |
安装与引入
bash
pxpkg init # 首次 pxpkg add nanoid@0.2.0 pxpkg install
px
import nanoid所属分类教程:标识、校验与版本
公开接口
| 函数 | 参数 |
|---|---|
nd_alphabet | |
nd_generate | n=21 |
nd_valid | s |
其余 5 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
nd_generate_custom | alphabet, n=21 |
nd_generate_nonsecure | n=21 |
nd_default_size | |
nd_valid_custom | s, alphabet |
nd_generate_prefix | prefix, n=21 |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/nanoid —— NanoID(URL 友好随机 ID) ------------------------------------------------------------ 纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。 nd_generate(n=21) → Ok(str) | Err 默认 21 位 URL 安全 ID nd_alphabet() → str 默认字母表(64 字符) nd_valid(s) → bool 是否全由字母表构成 字母表 64 字符(- _ 0-9 a-z A-Z);os_random_hex 逐字节 → b % 64, 因 256 % 64 == 0,索引**无取模偏差**(无需拒绝采样)。 ============================================================