From b73a94fc3c97d3eb09b103160df76be155ced3c2 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 13:57:39 +0200 Subject: [PATCH 1/4] update version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7d1e89b..e2f1a31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plurify" -version = "0.1.1" +version = "0.1.2" edition = "2021" license = "MIT" From 89675db0e831960bea720a0e855a088b370f47d4 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 21 Dec 2024 20:31:37 +0200 Subject: [PATCH 2/4] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 351f3cd..c7c8c0a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # plurify ![Build](https://github.com/YGGverse/plurify/actions/workflows/build.yml/badge.svg) +[![Documentation](https://docs.rs/plurify/badge.svg)](https://docs.rs/plurify) +[![crates.io](https://img.shields.io/crates/v/plurify.svg)](https://crates.io/crates/plurify) Pluralize words in different locales From 5aeb139caa7cce32c52bb8bf2ec97bcddfed76e9 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 28 Jan 2025 17:41:53 +0200 Subject: [PATCH 3/4] implement trait for `usize`, rename `ns` to `plurify` --- Cargo.toml | 2 +- README.md | 14 ++++++------- src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++-------- tests/integration.rs | 25 ++++++++++++++-------- 4 files changed, 65 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e2f1a31..e59a821 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plurify" -version = "0.1.2" +version = "0.2.0" edition = "2021" license = "MIT" diff --git a/README.md b/README.md index c7c8c0a..db504e5 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,16 @@ cargo add plurify ## Usage ``` rust -use plurify::ns; +use plurify::*; let en = &["cat", "cats", "cats"]; let uk = &["кіт", "кота", "котів"]; -assert_eq!(ns(1, en), "cat"); -assert_eq!(ns(2, en), "cats"); -assert_eq!(ns(5, en), "cats"); +assert_eq!(1.plurify(en), "cat"); +assert_eq!(2.plurify(en), "cats"); +assert_eq!(5.plurify(en), "cats"); -assert_eq!(ns(1, uk), "кіт"); -assert_eq!(ns(2, uk), "кота"); -assert_eq!(ns(5, uk), "котів"); +assert_eq!(1.plurify(uk), "кіт"); +assert_eq!(2.plurify(uk), "кота"); +assert_eq!(5.plurify(uk), "котів"); ``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e080b8e..7bccda8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,24 +1,55 @@ -/// Get plural string `s` match number `n` +// Functions + +/// Get plural string `s` for number `n` /// /// ## Example /// ``` rust -/// use plurify::ns; +/// use plurify::*; /// /// let en = &["cat", "cats", "cats"]; /// let uk = &["кіт", "кота", "котів"]; /// -/// assert_eq!(ns(1, en), "cat"); -/// assert_eq!(ns(2, en), "cats"); -/// assert_eq!(ns(5, en), "cats"); +/// assert_eq!(plurify(1, en), "cat"); +/// assert_eq!(plurify(2, en), "cats"); +/// assert_eq!(plurify(5, en), "cats"); /// -/// assert_eq!(ns(1, uk), "кіт"); -/// assert_eq!(ns(2, uk), "кота"); -/// assert_eq!(ns(5, uk), "котів"); +/// assert_eq!(plurify(1, uk), "кіт"); +/// assert_eq!(plurify(2, uk), "кота"); +/// assert_eq!(plurify(5, uk), "котів"); /// ``` -pub fn ns<'a>(n: usize, s: &[&'a str]) -> &'a str { +pub fn plurify<'a>(n: usize, s: &[&'a str]) -> &'a str { s[if n % 100 > 4 && n % 100 < 20 { 2 } else { [2, 0, 1, 1, 1, 2][usize::min(n % 10, 5)] }] } + +// Traits + +pub trait Plurify { + fn plurify<'a>(self, s: &[&'a str]) -> &'a str; +} + +impl Plurify for usize { + /// Get plural string for `usize` value + /// + /// ## Example + /// ``` rust + /// use plurify::*; + /// + /// let en = &["cat", "cats", "cats"]; + /// let uk = &["кіт", "кота", "котів"]; + /// + /// assert_eq!(1.plurify(en), "cat"); + /// assert_eq!(2.plurify(en), "cats"); + /// assert_eq!(5.plurify(en), "cats"); + /// + /// assert_eq!(1.plurify(uk), "кіт"); + /// assert_eq!(2.plurify(uk), "кота"); + /// assert_eq!(5.plurify(uk), "котів"); + /// ``` + fn plurify<'a>(self, s: &[&'a str]) -> &'a str { + plurify(self, s) + } +} diff --git a/tests/integration.rs b/tests/integration.rs index 957a0bb..81eb0de 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,15 +1,24 @@ -use plurify::ns; +#[cfg(test)] +use plurify::*; #[test] -fn _ns() { +fn test() { let en = &["cat", "cats", "cats"]; let uk = &["кіт", "кота", "котів"]; - assert_eq!(ns(1, en), "cat"); - assert_eq!(ns(2, en), "cats"); - assert_eq!(ns(5, en), "cats"); + assert_eq!(plurify(1, en), "cat"); + assert_eq!(plurify(2, en), "cats"); + assert_eq!(plurify(5, en), "cats"); - assert_eq!(ns(1, uk), "кіт"); - assert_eq!(ns(2, uk), "кота"); - assert_eq!(ns(5, uk), "котів"); + assert_eq!(plurify(1, uk), "кіт"); + assert_eq!(plurify(2, uk), "кота"); + assert_eq!(plurify(5, uk), "котів"); + + assert_eq!(1.plurify(en), "cat"); + assert_eq!(2.plurify(en), "cats"); + assert_eq!(5.plurify(en), "cats"); + + assert_eq!(1.plurify(uk), "кіт"); + assert_eq!(2.plurify(uk), "кота"); + assert_eq!(5.plurify(uk), "котів"); } From 81bd06eee8c204bf5dd2bcd2cb071f24c9fa89bd Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 28 Jan 2025 17:49:07 +0200 Subject: [PATCH 4/4] update version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e59a821..384b080 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plurify" -version = "0.2.0" +version = "0.2.1" edition = "2021" license = "MIT"