2.1.1 • Published 2 years ago

nth-check v2.1.1

Weekly downloads
12,714,759
License
BSD-2-Clause
Repository
github
Last release
2 years ago

nth-check Build Status

Parses and compiles CSS nth-checks to highly optimized functions.

About

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(). It can be used to check if a given index matches a given nth-rule, or to generate a sequence of indices matching a given nth-rule.

nth-check focusses on speed, providing optimized functions for different kinds of nth-child formulas, while still following the spec.

API

import nthCheck, { parse, compile } from "nth-check";
nthCheck(formula)

Parses and compiles a formula to a highly optimized function. Combination of parse and compile.

If the formula doesn't match any elements, it returns boolbase's falseFunc. Otherwise, a function accepting an index is returned, which returns whether or not the passed index matches the formula.

Note: The nth-rule starts counting at 1, the returned function at 0.

Example:

const check = nthCheck("2n+3");

check(0); // `false`
check(1); // `false`
check(2); // `true`
check(3); // `false`
check(4); // `true`
check(5); // `false`
check(6); // `true`
parse(formula)

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.

Example:

parse("2n+3"); // [2, 3]
compile([a, b])

Takes an array with two elements (as returned by .parse) and returns a highly optimized function.

Example:

const check = compile([2, 3]);

check(0); // `false`
check(1); // `false`
check(2); // `true`
check(3); // `false`
check(4); // `true`
check(5); // `false`
check(6); // `true`
generate([a, b])

Returns a function that produces a monotonously increasing sequence of indices.

If the sequence has an end, the returned function will return null after the last index in the sequence.

Example: An always increasing sequence

const gen = nthCheck.generate([2, 3]);

gen(); // `1`
gen(); // `3`
gen(); // `5`
gen(); // `8`
gen(); // `11`

Example: With an end value

const gen = nthCheck.generate([-2, 5]);

gen(); // 0
gen(); // 2
gen(); // 4
gen(); // null
sequence(formula)

Parses and compiles a formula to a generator that produces a sequence of indices. Combination of parse and generate.

Example: An always increasing sequence

const gen = nthCheck.sequence("2n+3");

gen(); // `1`
gen(); // `3`
gen(); // `5`
gen(); // `8`
gen(); // `11`

Example: With an end value

const gen = nthCheck.sequence("-2n+5");

gen(); // 0
gen(); // 2
gen(); // 4
gen(); // null

License: BSD-2-Clause

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

nth-check for enterprise

Available as part of the Tidelift Subscription

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.

css-selecteslint-plugin-vuecomponennenttvuedragdropuploadimages@cashremit/cr-streamline-iconscloud-archive-s3bb-chat@fundefund/funde_ckgql_din_modlitaotao2018afryxiconsmutasi-bca@l1nyanm1ng/react-picture-viewercthpb-plugin-social@olivervorasai/sliderreact-app-rewired-upstreampedtypes-giscogoportutilsunblock-block-save-variables@infinitebrahmanuniverse/nolb-nt@saaspe/componentsexpand-react-bridgesklif-ui-kitsklif-apimichaels-ssr@everything-registry/sub-chunk-2352@314oner_npm/universal-components-libraryreact-native-credit-card-pkgp149-tabletest-popupssklif-ui@enirisdev/angular-google-charts@hoaitt/ckeditor5-build-classic-mention@hproinformatica/functions@humanity.cash/types@kalkanisys/vue-select@knooks/use-title@kommunicate/kommunicate-chatbot-plugin@lego-js/tests@lordbeanbag/vite-plugin-svg-icons-monorepo@lozzychops/dropdown-menu@leiyulf/gantt-task-react@landra_sistemas/react-query-assist@lanz1/v-money3@juanaraneta/dept-central-lib-clientadminlsacnextjs-stories@marinaga/bananize@mayuriachewad/pickupbiz-npm-package@mblode/react-native-feather@mikesoes/scrap-tp@mauriciora/heroicons@meysam213/react-leaflet@podlubnaja/ui-kit@percent/percent-api-hooks@rainbow57/eslint-plugin-vue@refactorpro/pixi-virtual-joystick@ngxvoice/ngx-voicelistner@nirin100/quadratic-protocol@nirin100/quadratic-sdk@nodeberry/solidity-payment-processor@frodzislaw/front3@gema/css-select@farvater/open-pedigree@faizanhaider/iconsax@fabwcie/ckeditor5-preview@object-studio/react-native-web@ozancakir/ckeditor5-build-inline-balloon@pagopa/selfcare-common-frontend@navi001/design-system@purnasatria/directus-extension-document-interface@opticss/css-select@oriduk/ckeditor5-rebuild-musicanote@oriduk/ckeditor5-rebuild-oriduk@stansaal/ckeditor5-custom-buildlf-ui-kit@tillschweneker/ckeditor5-build-strapi-wysiwyg-markdown@tomw2w/my-nuxt-layerlitepie-datepicker-gabe@img-arena/img-ui-mui-theme@img-arena/ui-core@iarani/opendata@ikon-x/ckeditor5-custom-build-for-irrosoft@jlallement/cookieconsent@itsridhopratama/taman-baca-masyarakat@itayn-fireberry-org/itayn-test@junvary/quasar-ui-qcascader@juanzenn/ckeditor5-custom-buildjulien-easy-modal@sak1sham/react-toc-highlight@rstacruz/pnpm@safely-project/safely-ts@stasa87/react-insta-stories@southcn/ckeditor5-build-inlinekafirchain-tetris@skeetboothppq/component-library@silver0542/directus-extension-cma-components-editor@shivarajapple/first-library@syedsadiqali11/emoji-mart@taymoon/ckeditor5-custom-build
2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

5 years ago

1.0.1

9 years ago

1.0.0

10 years ago