Skip to content

Commit

Permalink
refactor: Port module to TS, Jest, ESLint (#7)
Browse files Browse the repository at this point in the history
BREAKING: The main export is now a `default` export.
  • Loading branch information
fb55 committed Dec 1, 2020
2 parents 8d05af6 + a9dd9c4 commit 40b99c2
Show file tree
Hide file tree
Showing 22 changed files with 13,368 additions and 291 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
lib/
62 changes: 62 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"extends": ["eslint:recommended", "prettier"],
"env": {
"node": true,
"es6": true
},
"rules": {
"eqeqeq": [2, "smart"],
"no-caller": 2,
"dot-notation": 2,
"no-var": 2,
"prefer-const": 2,
"prefer-arrow-callback": [2, { "allowNamedFunctions": true }],
"arrow-body-style": [2, "as-needed"],
"object-shorthand": 2,
"prefer-template": 2,
"one-var": [2, "never"],
"prefer-destructuring": [2, { "object": true }],
"capitalized-comments": 2,
"multiline-comment-style": [2, "starred-block"],
"spaced-comment": 2,
"yoda": [2, "never"],
"curly": [2, "multi-line"],
"no-else-return": 2
},
"overrides": [
{
"files": "*.ts",
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint"
],
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.eslint.json"
},
"rules": {
"@typescript-eslint/prefer-for-of": 0,
"@typescript-eslint/member-ordering": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-unused-vars": 0,
"@typescript-eslint/no-use-before-define": [
2,
{ "functions": false }
],
"@typescript-eslint/consistent-type-definitions": [
2,
"interface"
],
"@typescript-eslint/prefer-function-type": 2,
"@typescript-eslint/no-unnecessary-type-arguments": 2,
"@typescript-eslint/prefer-string-starts-ends-with": 2,
"@typescript-eslint/prefer-readonly": 2,
"@typescript-eslint/prefer-includes": 2,
"@typescript-eslint/no-unnecessary-condition": 2,
"@typescript-eslint/switch-exhaustiveness-check": 2,
"@typescript-eslint/prefer-nullish-coalescing": 2
}
}
]
}
12 changes: 1 addition & 11 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
github: [fb55]
tidelift: "npm/nth-check"
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with a single custom sponsorship URL
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.vscode/
node_modules/
coverage/
lib/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
lib/
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
arch:
- amd64
- ppc64le
- amd64
- ppc64le
node_js:
- lts/*
- lts/*
56 changes: 37 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nth-check [![Build Status](https://travis-ci.org/fb55/nth-check.svg)](https://travis-ci.org/fb55/nth-check)

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

### About

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

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

##### `nthCheck(formula)`

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

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

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.
**Note**: The nth-rule starts counting at `1`, the returned function at `0`.

__Example:__
**Example:**

```js
nthCheck.parse("2n+3") //[2, 3]
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`
```

##### `nthCheck.compile([a, b])`
##### `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:**

```js
parse("2n+3"); // [2, 3]
```

##### `compile([a, b])`

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

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`).
**Example:**

__Example:__
```js
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
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`
```

---

License: BSD-2-Clause

## Security contact information
Expand Down
51 changes: 0 additions & 51 deletions compile.js

This file was deleted.

9 changes: 0 additions & 9 deletions index.js

This file was deleted.

0 comments on commit 40b99c2

Please sign in to comment.