選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # nth-check [![Build Status](https://travis-ci.org/fb55/nth-check.svg)](https://travis-ci.org/fb55/nth-check)
  2. Parses and compiles CSS nth-checks to highly optimized functions.
  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. import nthCheck, { parse, compile } from "nth-check";
  9. ```
  10. ##### `nthCheck(formula)`
  11. Parses and compiles a formula to a highly optimized function. Combination of `parse` and `compile`.
  12. 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 the passed _index_ matches the formula.
  13. **Note**: The nth-rule starts counting at `1`, the returned function at `0`.
  14. **Example:**
  15. ```js
  16. const check = nthCheck("2n+3");
  17. check(0); // `false`
  18. check(1); // `false`
  19. check(2); // `true`
  20. check(3); // `false`
  21. check(4); // `true`
  22. check(5); // `false`
  23. check(6); // `true`
  24. ```
  25. ##### `parse(formula)`
  26. Parses the expression, throws an `Error` if it fails. Otherwise, returns an array containing the integer step size and the integer offset of the nth rule.
  27. **Example:**
  28. ```js
  29. parse("2n+3"); // [2, 3]
  30. ```
  31. ##### `compile([a, b])`
  32. Takes an array with two elements (as returned by `.parse`) and returns a highly optimized function.
  33. **Example:**
  34. ```js
  35. const check = compile([2, 3]);
  36. check(0); // `false`
  37. check(1); // `false`
  38. check(2); // `true`
  39. check(3); // `false`
  40. check(4); // `true`
  41. check(5); // `false`
  42. check(6); // `true`
  43. ```
  44. ---
  45. License: BSD-2-Clause
  46. ## Security contact information
  47. To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
  48. Tidelift will coordinate the fix and disclosure.
  49. ## `nth-check` for enterprise
  50. Available as part of the Tidelift Subscription
  51. The maintainers of `nth-check` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-nth-check?utm_source=npm-nth-check&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)