Skip to content

Commit 40b99c2

Browse files
authored
refactor: Port module to TS, Jest, ESLint (#7)
BREAKING: The main export is now a `default` export.
2 parents 8d05af6 + a9dd9c4 commit 40b99c2

22 files changed

+13368
-291
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
coverage/
3+
lib/

.eslintrc.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"extends": ["eslint:recommended", "prettier"],
3+
"env": {
4+
"node": true,
5+
"es6": true
6+
},
7+
"rules": {
8+
"eqeqeq": [2, "smart"],
9+
"no-caller": 2,
10+
"dot-notation": 2,
11+
"no-var": 2,
12+
"prefer-const": 2,
13+
"prefer-arrow-callback": [2, { "allowNamedFunctions": true }],
14+
"arrow-body-style": [2, "as-needed"],
15+
"object-shorthand": 2,
16+
"prefer-template": 2,
17+
"one-var": [2, "never"],
18+
"prefer-destructuring": [2, { "object": true }],
19+
"capitalized-comments": 2,
20+
"multiline-comment-style": [2, "starred-block"],
21+
"spaced-comment": 2,
22+
"yoda": [2, "never"],
23+
"curly": [2, "multi-line"],
24+
"no-else-return": 2
25+
},
26+
"overrides": [
27+
{
28+
"files": "*.ts",
29+
"extends": [
30+
"plugin:@typescript-eslint/eslint-recommended",
31+
"plugin:@typescript-eslint/recommended",
32+
"prettier/@typescript-eslint"
33+
],
34+
"parserOptions": {
35+
"sourceType": "module",
36+
"project": "./tsconfig.eslint.json"
37+
},
38+
"rules": {
39+
"@typescript-eslint/prefer-for-of": 0,
40+
"@typescript-eslint/member-ordering": 0,
41+
"@typescript-eslint/explicit-function-return-type": 0,
42+
"@typescript-eslint/no-unused-vars": 0,
43+
"@typescript-eslint/no-use-before-define": [
44+
2,
45+
{ "functions": false }
46+
],
47+
"@typescript-eslint/consistent-type-definitions": [
48+
2,
49+
"interface"
50+
],
51+
"@typescript-eslint/prefer-function-type": 2,
52+
"@typescript-eslint/no-unnecessary-type-arguments": 2,
53+
"@typescript-eslint/prefer-string-starts-ends-with": 2,
54+
"@typescript-eslint/prefer-readonly": 2,
55+
"@typescript-eslint/prefer-includes": 2,
56+
"@typescript-eslint/no-unnecessary-condition": 2,
57+
"@typescript-eslint/switch-exhaustiveness-check": 2,
58+
"@typescript-eslint/prefer-nullish-coalescing": 2
59+
}
60+
}
61+
]
62+
}

.github/FUNDING.yml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
1-
# These are supported funding model platforms
2-
3-
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4-
patreon: # Replace with a single Patreon username
5-
open_collective: # Replace with a single Open Collective username
6-
ko_fi: # Replace with a single Ko-fi username
1+
github: [fb55]
72
tidelift: "npm/nth-check"
8-
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9-
liberapay: # Replace with a single Liberapay username
10-
issuehunt: # Replace with a single IssueHunt username
11-
otechie: # Replace with a single Otechie username
12-
custom: # Replace with a single custom sponsorship URL

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
.vscode/
12
node_modules/
3+
coverage/
4+
lib/

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
coverage/
3+
lib/

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
arch:
3-
- amd64
4-
- ppc64le
3+
- amd64
4+
- ppc64le
55
node_js:
6-
- lts/*
6+
- lts/*

README.md

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# nth-check [![Build Status](https://travis-ci.org/fb55/nth-check.svg)](https://travis-ci.org/fb55/nth-check)
22

3-
A performant nth-check parser & compiler.
3+
Parses and compiles CSS nth-checks to highly optimized functions.
44

55
### About
66

@@ -11,43 +11,61 @@ This module can be used to parse & compile nth-checks, as they are found in CSS
1111
### API
1212

1313
```js
14-
const nthCheck = require("nth-check");
14+
import nthCheck, { parse, compile } from "nth-check";
1515
```
1616

1717
##### `nthCheck(formula)`
1818

19-
First parses, then compiles the formula.
19+
Parses and compiles a formula to a highly optimized function. Combination of `parse` and `compile`.
2020

21-
##### `nthCheck.parse(formula)`
21+
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.
2222

23-
Parses the expression, throws a `SyntaxError` if it fails. Otherwise, returns an array containing the integer step size and the integer offset of the nth rule.
23+
**Note**: The nth-rule starts counting at `1`, the returned function at `0`.
2424

25-
__Example:__
25+
**Example:**
2626

2727
```js
28-
nthCheck.parse("2n+3") //[2, 3]
28+
const check = nthCheck("2n+3");
29+
30+
check(0); // `false`
31+
check(1); // `false`
32+
check(2); // `true`
33+
check(3); // `false`
34+
check(4); // `true`
35+
check(5); // `false`
36+
check(6); // `true`
2937
```
3038

31-
##### `nthCheck.compile([a, b])`
39+
##### `parse(formula)`
40+
41+
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.
42+
43+
**Example:**
44+
45+
```js
46+
parse("2n+3"); // [2, 3]
47+
```
48+
49+
##### `compile([a, b])`
3250

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

35-
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`).
53+
**Example:**
3654

37-
__Example:__
3855
```js
39-
const check = nthCheck.compile([2, 3]);
40-
41-
check(0) //false
42-
check(1) //false
43-
check(2) //true
44-
check(3) //false
45-
check(4) //true
46-
check(5) //false
47-
check(6) //true
56+
const check = compile([2, 3]);
57+
58+
check(0); // `false`
59+
check(1); // `false`
60+
check(2); // `true`
61+
check(3); // `false`
62+
check(4); // `true`
63+
check(5); // `false`
64+
check(6); // `true`
4865
```
4966

5067
---
68+
5169
License: BSD-2-Clause
5270

5371
## Security contact information

compile.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)