#uuid #short #npm-package #generate #alphabet #parse #format

no-std short-uuid

一个生成和解析短 uuid 的库

3 个版本

0.1.2 2024年2月23日
0.1.1 2024年2月23日
0.1.0 2024年2月23日

#293 in 数据结构

Download history 43/week @ 2024-03-14 9/week @ 2024-03-21 39/week @ 2024-03-28 24/week @ 2024-04-04 19/week @ 2024-04-11 25/week @ 2024-04-18 17/week @ 2024-04-25 25/week @ 2024-05-02 372/week @ 2024-05-09 443/week @ 2024-05-16 480/week @ 2024-05-23 509/week @ 2024-05-30 544/week @ 2024-06-06 502/week @ 2024-06-13 559/week @ 2024-06-20 386/week @ 2024-06-27

2,083 每月下载量

MIT 许可证

24KB
359

short-uuid

将标准 UUID 生成并转换为更短的或不同格式的格式,并返回。

JavaScript npm 包 short-uuid 的端口,感谢作者。

默认 flickrBase58 字母表中的短 uuid 字符串示例

mhvXdrZT4jP5T8vBxuvm75

入门

使用 cargo 安装包

cargo add short-uuid

或将它添加到你的 Cargo.toml

[dependencies]
short-uuid = "0.1.0"

示例

生成 flickrBase58 格式编码的短 uuidv4

use short_uuid::ShortUuid;

let shortened_uuid = ShortUuid::generate();

使用宏生成 flickrBase58 格式编码的短 uuidv4

use short_uuid::short;

let shortened_uuid = short!();

使用自定义字母表生成短 uuidv4

use short_uuid::{ShortUuidCustom, CustomTranslator};

let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();

let custom_short = ShortUuidCustom::generate(&translator);
let custom_short_string = custom_short.to_string();

从标准 uuid 获取缩短的 uuid

use short_uuid::ShortUuid;
// create normal uuid v4
let uuid = uuid::Uuid::new_v4();

let short = ShortUuid::from_uuid(&uuid);

使用自定义字母表从标准 uuid 获取缩短的 uuid

use short_uuid::{ShortUuidCustom, CustomTranslator};

let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();

let uuid = uuid::Uuid::new_v4();
let short_custom = ShortUuidCustom::from_uuid(&uuid, &translator);
let short_custom_string = short_custom.to_string();

从 uuid 字符串获取缩短的 uuid

use short_uuid::ShortUuid;

let uuid_str = "3cfb46e7-c391-42ef-90b8-0c1d9508e752";
let short_uuid = ShortUuid::from_uuid_str(&uuid_str);

使用自定义字母表从 uuid 字符串获取缩短的 uuid

use short_uuid::{ShortUuidCustom, CustomTranslator};

let custom_alphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
let translator = CustomTranslator::new(custom_alphabet).unwrap();

let uuid_str = "3cfb46e7-c391-42ef-90b8-0c1d9508e752";
let short_custom = ShortUuidCustom::from_uuid_str(&uuid_str, &translator).unwrap();
let short_custom_string = short_custom.to_string();

参考

依赖项

~225KB