nth-check
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -8,2 +8,4 @@ /** | ||
* @example | ||
* | ||
* ```js | ||
* const check = nthCheck.compile([2, 3]); | ||
@@ -18,4 +20,38 @@ * | ||
* check(6); // `true` | ||
* ``` | ||
*/ | ||
export declare function compile(parsed: [a: number, b: number]): (index: number) => boolean; | ||
/** | ||
* 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. | ||
* | ||
* @param parsed A tuple [a, b], as returned by `parse`. | ||
* @returns A function that produces a sequence of indices. | ||
* @example <caption>Always increasing (2n+3)</caption> | ||
* | ||
* ```js | ||
* const gen = nthCheck.generate([2, 3]) | ||
* | ||
* gen() // `1` | ||
* gen() // `3` | ||
* gen() // `5` | ||
* gen() // `8` | ||
* gen() // `11` | ||
* ``` | ||
* | ||
* @example <caption>With end value (-2n+10)</caption> | ||
* | ||
* ```js | ||
* | ||
* const gen = nthCheck.generate([-2, 5]); | ||
* | ||
* gen() // 0 | ||
* gen() // 2 | ||
* gen() // 4 | ||
* gen() // null | ||
* ``` | ||
*/ | ||
export declare function generate(parsed: [a: number, b: number]): () => number | null; | ||
//# sourceMappingURL=compile.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compile = void 0; | ||
exports.generate = exports.compile = void 0; | ||
var boolbase_1 = require("boolbase"); | ||
@@ -12,2 +12,4 @@ /** | ||
* @example | ||
* | ||
* ```js | ||
* const check = nthCheck.compile([2, 3]); | ||
@@ -22,2 +24,3 @@ * | ||
* check(6); // `true` | ||
* ``` | ||
*/ | ||
@@ -58,1 +61,61 @@ function compile(parsed) { | ||
exports.compile = compile; | ||
/** | ||
* 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. | ||
* | ||
* @param parsed A tuple [a, b], as returned by `parse`. | ||
* @returns A function that produces a sequence of indices. | ||
* @example <caption>Always increasing (2n+3)</caption> | ||
* | ||
* ```js | ||
* const gen = nthCheck.generate([2, 3]) | ||
* | ||
* gen() // `1` | ||
* gen() // `3` | ||
* gen() // `5` | ||
* gen() // `8` | ||
* gen() // `11` | ||
* ``` | ||
* | ||
* @example <caption>With end value (-2n+10)</caption> | ||
* | ||
* ```js | ||
* | ||
* const gen = nthCheck.generate([-2, 5]); | ||
* | ||
* gen() // 0 | ||
* gen() // 2 | ||
* gen() // 4 | ||
* gen() // null | ||
* ``` | ||
*/ | ||
function generate(parsed) { | ||
var a = parsed[0]; | ||
// Subtract 1 from `b`, to convert from one- to zero-indexed. | ||
var b = parsed[1] - 1; | ||
var n = 0; | ||
// Make sure to always return an increasing sequence | ||
if (a < 0) { | ||
var aPos_1 = -a; | ||
// Get `b mod a` | ||
var minValue_1 = ((b % aPos_1) + aPos_1) % aPos_1; | ||
return function () { | ||
var val = minValue_1 + aPos_1 * n++; | ||
return val > b ? null : val; | ||
}; | ||
} | ||
if (a === 0) | ||
return b < 0 | ||
? // There are no result — always return `null` | ||
function () { return null; } | ||
: // Return `b` exactly once | ||
function () { return (n++ === 0 ? b : null); }; | ||
if (b < 0) { | ||
b += a * Math.ceil(-b / a); | ||
} | ||
return function () { return a * n++ + b; }; | ||
} | ||
exports.generate = generate; | ||
//# sourceMappingURL=compile.js.map |
import { parse } from "./parse"; | ||
import { compile } from "./compile"; | ||
export { parse, compile }; | ||
import { compile, generate } from "./compile"; | ||
export { parse, compile, generate }; | ||
/** | ||
* Parses and compiles a formula to a highly optimized function. | ||
* Combination of `parse` and `compile`. | ||
* Combination of {@link parse} and {@link compile}. | ||
* | ||
@@ -28,2 +28,33 @@ * If the formula doesn't match any elements, | ||
export default function nthCheck(formula: string): (index: number) => boolean; | ||
/** | ||
* Parses and compiles a formula to a generator that produces a sequence of indices. | ||
* Combination of {@link parse} and {@link generate}. | ||
* | ||
* @param formula The formula to compile. | ||
* @returns A function that produces a sequence of indices. | ||
* @example <caption>Always increasing</caption> | ||
* | ||
* ```js | ||
* const gen = nthCheck.sequence('2n+3') | ||
* | ||
* gen() // `1` | ||
* gen() // `3` | ||
* gen() // `5` | ||
* gen() // `8` | ||
* gen() // `11` | ||
* ``` | ||
* | ||
* @example <caption>With end value</caption> | ||
* | ||
* ```js | ||
* | ||
* const gen = nthCheck.sequence('-2n+5'); | ||
* | ||
* gen() // 0 | ||
* gen() // 2 | ||
* gen() // 4 | ||
* gen() // null | ||
* ``` | ||
*/ | ||
export declare function sequence(formula: string): () => number | null; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compile = exports.parse = void 0; | ||
exports.sequence = exports.generate = exports.compile = exports.parse = void 0; | ||
var parse_1 = require("./parse"); | ||
@@ -8,5 +8,6 @@ Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_1.parse; } }); | ||
Object.defineProperty(exports, "compile", { enumerable: true, get: function () { return compile_1.compile; } }); | ||
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return compile_1.generate; } }); | ||
/** | ||
* Parses and compiles a formula to a highly optimized function. | ||
* Combination of `parse` and `compile`. | ||
* Combination of {@link parse} and {@link compile}. | ||
* | ||
@@ -36,1 +37,36 @@ * If the formula doesn't match any elements, | ||
exports.default = nthCheck; | ||
/** | ||
* Parses and compiles a formula to a generator that produces a sequence of indices. | ||
* Combination of {@link parse} and {@link generate}. | ||
* | ||
* @param formula The formula to compile. | ||
* @returns A function that produces a sequence of indices. | ||
* @example <caption>Always increasing</caption> | ||
* | ||
* ```js | ||
* const gen = nthCheck.sequence('2n+3') | ||
* | ||
* gen() // `1` | ||
* gen() // `3` | ||
* gen() // `5` | ||
* gen() // `8` | ||
* gen() // `11` | ||
* ``` | ||
* | ||
* @example <caption>With end value</caption> | ||
* | ||
* ```js | ||
* | ||
* const gen = nthCheck.sequence('-2n+5'); | ||
* | ||
* gen() // 0 | ||
* gen() // 2 | ||
* gen() // 4 | ||
* gen() // null | ||
* ``` | ||
*/ | ||
function sequence(formula) { | ||
return (0, compile_1.generate)((0, parse_1.parse)(formula)); | ||
} | ||
exports.sequence = sequence; | ||
//# sourceMappingURL=index.js.map |
@@ -44,3 +44,3 @@ "use strict"; | ||
if (number === null || idx < formula.length) { | ||
throw new Error("n-th rule couldn't be parsed ('" + formula + "')"); | ||
throw new Error("n-th rule couldn't be parsed ('".concat(formula, "')")); | ||
} | ||
@@ -78,1 +78,2 @@ return [a, sign * number]; | ||
exports.parse = parse; | ||
//# sourceMappingURL=parse.js.map |
{ | ||
"name": "nth-check", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Parses and compiles CSS nth-checks to highly optimized functions.", | ||
@@ -16,2 +16,7 @@ "author": "Felix Boehm <me@feedic.com>", | ||
"types": "lib/index.d.ts", | ||
"module": "lib/esm/index.js", | ||
"exports": { | ||
"require": "./lib/index.js", | ||
"import": "./lib/esm/index.js" | ||
}, | ||
"files": [ | ||
@@ -30,3 +35,5 @@ "lib/**/*" | ||
"prettier": "prettier '**/*.{ts,md,json,yml}'", | ||
"build": "tsc", | ||
"build": "npm run build:cjs && npm run build:esm", | ||
"build:cjs": "tsc --sourceRoot https://raw.githubusercontent.com/fb55/nth-check/$(git rev-parse HEAD)/src/", | ||
"build:esm": "npm run build:cjs -- --module esnext --target es2019 --outDir lib/esm && echo '{\"type\":\"module\"}' > lib/esm/package.json", | ||
"prepare": "npm run build" | ||
@@ -51,12 +58,13 @@ }, | ||
"devDependencies": { | ||
"@types/jest": "^27.0.1", | ||
"@types/node": "^16.9.1", | ||
"@typescript-eslint/eslint-plugin": "^4.31.1", | ||
"@typescript-eslint/parser": "^4.31.1", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"jest": "^27.2.0", | ||
"prettier": "^2.4.1", | ||
"ts-jest": "^27.0.5", | ||
"typescript": "^4.4.3" | ||
"@types/boolbase": "^1.0.1", | ||
"@types/jest": "^27.5.0", | ||
"@types/node": "^17.0.35", | ||
"@typescript-eslint/eslint-plugin": "^5.25.0", | ||
"@typescript-eslint/parser": "^5.25.0", | ||
"eslint": "^8.15.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"jest": "^27.5.1", | ||
"prettier": "^2.6.2", | ||
"ts-jest": "^27.1.4", | ||
"typescript": "^4.6.4" | ||
}, | ||
@@ -63,0 +71,0 @@ "jest": { |
@@ -7,3 +7,3 @@ # nth-check [![Build Status](https://travis-ci.org/fb55/nth-check.svg)](https://travis-ci.org/fb55/nth-check) | ||
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()`. | ||
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. | ||
@@ -68,2 +68,58 @@ `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). | ||
##### `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 | ||
```js | ||
const gen = nthCheck.generate([2, 3]); | ||
gen(); // `1` | ||
gen(); // `3` | ||
gen(); // `5` | ||
gen(); // `8` | ||
gen(); // `11` | ||
``` | ||
**Example:** With an end value | ||
```js | ||
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 | ||
```js | ||
const gen = nthCheck.sequence("2n+3"); | ||
gen(); // `1` | ||
gen(); // `3` | ||
gen(); // `5` | ||
gen(); // `8` | ||
gen(); // `11` | ||
``` | ||
**Example:** With an end value | ||
```js | ||
const gen = nthCheck.sequence("-2n+5"); | ||
gen(); // 0 | ||
gen(); // 2 | ||
gen(); // 4 | ||
gen(); // null | ||
``` | ||
--- | ||
@@ -70,0 +126,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42230
28
748
137
11
1