nth-check

  • Version 2.1.1
  • Published
  • 42.6 kB
  • 1 dependency
  • BSD-2-Clause license

Install

npm i nth-check
yarn add nth-check
pnpm add nth-check

Overview

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

Index

Functions

function compile

compile: (parsed: [a: number, b: number]) => (index: number) => boolean;
  • Returns a function that checks if an elements index matches the given rule highly optimized to return the fastest solution.

    Parameter parsed

    A tuple [a, b], as returned by parse.

    Returns

    A highly optimized function that returns whether an index matches the nth-check.

    Example 1

    const check = nthCheck.compile([2, 3]);
    check(0); // `false`
    check(1); // `false`
    check(2); // `true`
    check(3); // `false`
    check(4); // `true`
    check(5); // `false`
    check(6); // `true`

function generate

generate: (parsed: [a: number, b: number]) => () => number | null;
  • 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.

    Parameter parsed

    A tuple [a, b], as returned by parse.

    Returns

    A function that produces a sequence of indices.

    Example 1

    Always increasing (2n+3)

    const gen = nthCheck.generate([2, 3])
    gen() // `1`
    gen() // `3`
    gen() // `5`
    gen() // `8`
    gen() // `11`

    Example 2

    With end value (-2n+10)

    const gen = nthCheck.generate([-2, 5]);
    gen() // 0
    gen() // 2
    gen() // 4
    gen() // null

function nthCheck

nthCheck: (formula: string) => (index: number) => boolean;
  • 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](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.

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

    Parameter formula

    The formula to compile.

    Example 1

    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

function parse

parse: (formula: string) => [a: number, b: number];
  • Parses an expression.

    Returns

    An array containing the integer step size and the integer offset of the nth rule.

    Throws

    An Error if parsing fails.

    Example 1

    nthCheck.parse("2n+3"); // returns [2, 3]

function sequence

sequence: (formula: string) => () => number | null;
  • Parses and compiles a formula to a generator that produces a sequence of indices. Combination of parse and generate.

    Parameter formula

    The formula to compile.

    Returns

    A function that produces a sequence of indices.

    Example 1

    Always increasing

    const gen = nthCheck.sequence('2n+3')
    gen() // `1`
    gen() // `3`
    gen() // `5`
    gen() // `8`
    gen() // `11`

    Example 2

    With end value

    const gen = nthCheck.sequence('-2n+5');
    gen() // 0
    gen() // 2
    gen() // 4
    gen() // null

Package Files (3)

Dependencies (1)

Dev Dependencies (11)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/nth-check.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/nth-check)
  • HTML
    <a href="https://www.jsdocs.io/package/nth-check"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>