From 458bc41b3b8444b8c460ae208847944fd8ad349b Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 12:57:24 +0200 Subject: [PATCH 1/9] update readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index bab2732..4db4f53 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,10 @@ cargo add plurify ## Usage +* [Documentation](https://docs.rs/plurify) + +### Example + ``` rust use plurify::ns; From 97ba2aa4f811d2db6a05b31df2d3c017ef2205e6 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 13:01:21 +0200 Subject: [PATCH 2/9] update readme --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4db4f53..351f3cd 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ Pluralize words in different locales +* [Documentation](https://docs.rs/plurify) + ## Install ``` bash @@ -12,10 +14,6 @@ cargo add plurify ## Usage -* [Documentation](https://docs.rs/plurify) - -### Example - ``` rust use plurify::ns; From 37da5d5a66055b4cfa3cdde334fa41d89d9a9b9e Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 13:26:58 +0200 Subject: [PATCH 3/9] use `usize::min` function --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f09654c..86f1e30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,6 @@ pub fn ns<'a>(n: usize, s: &[&'a str]) -> &'a str { s[if (n % 100) > 4 && (n % 100) < 20 { 2 } else { - [2, 0, 1, 1, 1, 2][std::cmp::min(n % 10, 5)] + [2, 0, 1, 1, 1, 2][usize::min(n % 10, 5)] }] } From 214d2f036193b861f20d03e14762fde337a42540 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 13:52:01 +0200 Subject: [PATCH 4/9] remove extra brackets --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 86f1e30..e080b8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ /// assert_eq!(ns(5, uk), "котів"); /// ``` pub fn ns<'a>(n: usize, s: &[&'a str]) -> &'a str { - s[if (n % 100) > 4 && (n % 100) < 20 { + s[if n % 100 > 4 && n % 100 < 20 { 2 } else { [2, 0, 1, 1, 1, 2][usize::min(n % 10, 5)] From 9a81c995333033d807301c9301536e06abfb05a7 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 13:53:52 +0200 Subject: [PATCH 5/9] update version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index dde741d..7d1e89b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plurify" -version = "0.1.0" +version = "0.1.1" edition = "2021" license = "MIT" From b73a94fc3c97d3eb09b103160df76be155ced3c2 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 13:57:39 +0200 Subject: [PATCH 6/9] 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 7/9] 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 8/9] 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 9/9] 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"