# nth-check
[](#contributors-)
[](https://crates.io/crates/nth-check)
[](https://docs.rs/nth-check)
[](https://github.com/trananhtung/nth-check/actions/workflows/ci.yml)
[](#license)
**Parse the CSS `An+B` microsyntax and test positions** — the notation behind
`:nth-child(2n+1)`, `:nth-of-type(odd)`, and `:nth-last-child(-n+3)`. The Rust
counterpart of the [`nth-check`](https://www.npmjs.com/package/nth-check) npm package
(behind `css-select` / `cheerio`). Zero dependencies and `#![no_std]` (no `alloc`).
```rust
use nth_check::parse;
let odd = parse("2n+1").unwrap(); // also: parse("odd")
assert!(odd.matches(1) && odd.matches(3));
assert!(!odd.matches(2));
let first_three = parse("-n+3").unwrap();
assert!(first_three.matches(1) && first_three.matches(3));
assert!(!first_three.matches(4));
let n = parse("2n+1").unwrap();
assert_eq!((n.a(), n.b()), (2, 1)); // the a·n + b coefficients
```
## Why nth-check?
Implementing a CSS selector engine, scraper, or linter means evaluating
`:nth-child(...)`. The `An+B` grammar (`odd`, `even`, `2n`, `-n+3`, signed `b`,
whitespace around the sign) is small but fiddly to parse correctly. Rust had this
only buried inside heavyweight CSS stacks; this is the focused, dependency-free
piece.
```toml
[dependencies]
nth-check = "0.1"
```
## API
| `parse(s)` | Parse an `An+B` string into an [`Nth`] (`Result<Nth, ParseError>`) |
| `Nth::new(a, b)` | Build from coefficients directly |
| `Nth::a()` / `Nth::b()` | The `a` (step) and `b` (offset) coefficients |
| `Nth::matches(index)` | Whether the **1-based** `index` matches `a·n + b` |
| `Nth` also implements `FromStr` and `Display` | `"2n+1".parse()` / round-trips to canonical form |
## Behavior
- Positions are **1-based**, matching CSS `:nth-child` (the first element is 1).
- `matches(index)` is `true` when `index == a·n + b` for some non-negative integer
`n`; it never panics (computed in 64-bit internally).
- Accepts `odd`, `even` (case-insensitive), a signed integer, and the `An+B` forms
with optional whitespace around the middle sign. Follows the CSS grammar strictly:
the offset needs an explicit sign (`2n+7`, not `2n7`), as browsers require — so
malformed input like `2 n`, `n2`, or `++1` is rejected. (This is slightly stricter
than node-`nth-check`'s lenient regex; matching is identical where both accept.)
- `Display` renders a canonical form (`2n+1`, `-n+3`, `n`, `3`) that round-trips
through `parse`.
## `no_std`
`#![no_std]` and `core`-only — no `alloc`, no dependencies.
## Contributors ✨
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/nth-check/commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
</tr>
</tbody>
</table>
## License
Licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.