From 4f016e2ae092efc2cd51ab25cf9d8d31a64289a4 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 12:43:17 +0200 Subject: [PATCH] initial commit --- .github/workflows/build.yml | 27 +++++++++++++++++++++++++++ .gitignore | 2 ++ Cargo.toml | 19 +++++++++++++++++++ README.md | 28 +++++++++++++++++++++++++++- src/lib.rs | 24 ++++++++++++++++++++++++ tests/integration.rs | 15 +++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 tests/integration.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..85edd8b --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,27 @@ +name: Build + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + RUSTFLAGS: -Dwarnings + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Run rustfmt + run: cargo fmt --all -- --check + - name: Run clippy + run: cargo clippy --all-targets + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb117fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..dde741d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "plurify" +version = "0.1.0" +edition = "2021" + +license = "MIT" +readme = "README.md" +description = "Pluralize words in different locales" +keywords = ["plural", "singular", "inflect", "translate", "localization"] +categories = [ + "development-tools", + "localization", + "parsing", + "text-processing", + "value-formatting", +] +repository = "https://github.com/YGGverse/plurify" + +[dependencies] diff --git a/README.md b/README.md index 9796d8f..bab2732 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ # plurify -Library to pluralize words + +![Build](https://github.com/YGGverse/plurify/actions/workflows/build.yml/badge.svg) + +Pluralize words in different locales + +## Install + +``` bash +cargo add plurify +``` + +## Usage + +``` rust +use plurify::ns; + +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!(ns(1, uk), "кіт"); +assert_eq!(ns(2, uk), "кота"); +assert_eq!(ns(5, uk), "котів"); +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f09654c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,24 @@ +/// Get plural string `s` match number `n` +/// +/// ## Example +/// ``` rust +/// use plurify::ns; +/// +/// 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!(ns(1, uk), "кіт"); +/// assert_eq!(ns(2, uk), "кота"); +/// assert_eq!(ns(5, uk), "котів"); +/// ``` +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)] + }] +} diff --git a/tests/integration.rs b/tests/integration.rs new file mode 100644 index 0000000..957a0bb --- /dev/null +++ b/tests/integration.rs @@ -0,0 +1,15 @@ +use plurify::ns; + +#[test] +fn _ns() { + 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!(ns(1, uk), "кіт"); + assert_eq!(ns(2, uk), "кота"); + assert_eq!(ns(5, uk), "котів"); +}