mirror of
https://github.com/YGGverse/plurify.git
synced 2026-03-31 17:15:36 +00:00
initial commit
This commit is contained in:
parent
4ccf763a0e
commit
4f016e2ae0
6 changed files with 114 additions and 1 deletions
27
.github/workflows/build.yml
vendored
Normal file
27
.github/workflows/build.yml
vendored
Normal file
|
|
@ -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
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Cargo.lock
|
||||
target
|
||||
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
|
|
@ -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]
|
||||
28
README.md
28
README.md
|
|
@ -1,2 +1,28 @@
|
|||
# plurify
|
||||
Library to pluralize words
|
||||
|
||||

|
||||
|
||||
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), "котів");
|
||||
```
|
||||
24
src/lib.rs
Normal file
24
src/lib.rs
Normal file
|
|
@ -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)]
|
||||
}]
|
||||
}
|
||||
15
tests/integration.rs
Normal file
15
tests/integration.rs
Normal file
|
|
@ -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), "котів");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue