Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Dec 18, 2024
1 parent 4ccf763 commit 4f016e2
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/build.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cargo.lock
target
19 changes: 19 additions & 0 deletions Cargo.toml
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]
28 changes: 27 additions & 1 deletion README.md
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), "котів");
```
24 changes: 24 additions & 0 deletions src/lib.rs
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)]
}]
}
15 changes: 15 additions & 0 deletions tests/integration.rs
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), "котів");
}

0 comments on commit 4f016e2

Please sign in to comment.