You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # nth-check [![Build Status](https://travis-ci.org/fb55/nth-check.svg)](https://travis-ci.org/fb55/nth-check)
  2. A performant nth-check parser & compiler.
  3. ### About
  4. This module can be used to parse & compile nth-checks, as they are found in CSS 3's `nth-child()` and `nth-last-of-type()`.
  5. `nth-check` focusses on speed, providing optimized functions for different kinds of nth-child formulas, while still following the [spec](http://www.w3.org/TR/css3-selectors/#nth-child-pseudo).
  6. ### API
  7. ```js
  8. var nthCheck = require("nth-check");
  9. ```
  10. ##### `nthCheck(formula)`
  11. First parses, then compiles the formula.
  12. ##### `nthCheck.parse(formula)`
  13. Parses the expression, throws a `SyntaxError` if it fails, otherwise returns an array containing two elements.
  14. __Example:__
  15. ```js
  16. nthCheck.parse("2n+3") //[2, 3]
  17. ```
  18. ##### `nthCheck.compile([a, b])`
  19. Takes an array with two elements (as returned by `.parse`) and returns a highly optimized function.
  20. If the formula doesn't match any elements, it returns [`boolbase`](https://github.com/fb55/boolbase)'s `falseFunc`, otherwise, a function accepting an _index_ is returned, which returns whether or not a passed _index_ matches the formula. (Note: The spec starts counting at `1`, the returned function at `0`).
  21. __Example:__
  22. ```js
  23. var check = nthCheck.compile([2, 3]);
  24. check(0) //false
  25. check(1) //false
  26. check(2) //true
  27. check(3) //false
  28. check(4) //true
  29. check(5) //false
  30. check(6) //true
  31. ```
  32. ---
  33. License: BSD