#slug #slugify #macro

slugify-rs

一个从字符串生成缩略词的 Rust 库

2 个版本

0.0.3 2022 年 4 月 28 日
0.0.2 2022 年 4 月 28 日
0.0.1 2022 年 4 月 27 日

#697 in 文本处理

Download history 112/week @ 2024-03-14 145/week @ 2024-03-21 202/week @ 2024-03-28 129/week @ 2024-04-04 93/week @ 2024-04-11 162/week @ 2024-04-18 190/week @ 2024-04-25 238/week @ 2024-05-02 152/week @ 2024-05-09 148/week @ 2024-05-16 150/week @ 2024-05-23 169/week @ 2024-05-30 156/week @ 2024-06-06 412/week @ 2024-06-13 442/week @ 2024-06-20 746/week @ 2024-06-27

1,770 每月下载量
7 个 Crates 中使用 (通过 kaspa-wallet-core)

自定义许可

21KB
385

Slugify-rs

一个用于灵活生成缩略词并处理 Unicode 的实用宏。

slugify! 宏实现了一个灵活的缩略词生成器,允许使用停用词、自定义分隔符和最大长度选项。宏提供了具有合理默认参数的简单界面,但也可以在需要时覆盖参数。

功能

  • 支持 Unicode 字符串(音译转换)。
  • 支持自定义缩略词分隔符。
  • 停用词过滤。
  • 支持缩略词最大长度。
  • 为生成的缩略词添加随机性以避免唯一约束的影响

使用方法

此 crate 位于 crates.io,可以通过将 slugify 添加到项目的 Cargo.toml 依赖中来进行使用

[dependencies]
slugify-rs = "0.0.3"

示例 基本缩略词生成

assert_eq!(slugify!("hello world"), "hello-world");

// Using a custom separator
assert_eq!(slugify!("hello world", separator = "."), "hello.world");
assert_eq!(slugify!("hello world", separator = " "), "hello world");
assert_eq!(slugify!("hello world", separator = ""), "helloworld");

// Stop words filtering
assert_eq!(slugify!("the quick brown fox jumps over the lazy dog", stop_words = "the,fox"), "quick-brown-jumps-over-lazy-dog");

// Maximum length
assert_eq!(slugify!("hello world", max_length = 5), "hello");
assert_eq!(slugify!("the hello world", stop_words = "the", max_length = 5), "hello");

// Random values added to string through nanoid
// Default randomness string length is 5.
assert_eq!(slugify!("hello world", randomness=true).len(), "hello-world".len()+5);
assert_eq!(slugify!("hello world", randomness=true,randomness_length=8).len(), "hello-world".len()+8);

// Phonetic Conversion and accented text
assert_eq!(slugify!("影師嗎"), "ying-shi-ma");
assert_eq!(slugify!("Æúű--cool?"), "aeuu-cool");
assert_eq!(slugify!("Nín hǎo. Wǒ shì zhōng guó rén"), "nin-hao-wo-shi-zhong-guo-ren");

// Passing multiple optional parameters.
// NOTE: the order of optional parameters matters: stop_words, separator and then max_length. All of them are optional, however when specifying more than one optional parameter, this order must be adhered.

assert_eq!(slugify!("the hello world", stop_words = "the", separator = "-"), "hello-world");
assert_eq!(slugify!("the hello world", separator = ".", max_length = 10), "the.hello");
assert_eq!(slugify!("the hello world", stop_words = "the", max_length = 5), "hello");
assert_eq!(slugify!("the hello world", stop_words = "the", separator = "-", max_length = 20), "hello-world");

信息

此缩略词是从原始的 slugify crate 由 @mattgathu 分支的。

依赖项

~495KB