nth-check 0.1.1

Parse the CSS An+B microsyntax (2n+1, odd, -n+3) — as in :nth-child — and test whether a 1-based position matches. Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented3 out of 8 items with examples
  • Size
  • Source code size: 31.33 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 244.11 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/nth-check
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

nth-check

All Contributors

Crates.io Documentation CI 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 npm package (behind css-select / cheerio). Zero dependencies and #![no_std] (no alloc).

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.

[dependencies]
nth-check = "0.1"

API

Item Purpose
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 specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

Licensed under either of Apache-2.0 or MIT at your option.