-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yggverse
committed
Dec 18, 2024
1 parent
4ccf763
commit 4f016e2
Showing
6 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Cargo.lock | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), "котів"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)] | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), "котів"); | ||
} |