Skip to content

Commit

Permalink
build: migrate to eslint and upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerpadilla committed Aug 8, 2021
1 parent 99f0135 commit 3787a57
Show file tree
Hide file tree
Showing 15 changed files with 4,640 additions and 7,381 deletions.
2 changes: 0 additions & 2 deletions .coveralls.yml

This file was deleted.

79 changes: 79 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"ignorePatterns": ["coverage", "node_modules", "dist"],
"env": {
"browser": true,
"es2020": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.json"
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:prettier/recommended"
],
"rules": {
"semi": ["warn", "always"],
"complexity": ["error", 25],
"curly": ["error", "all"],
"no-restricted-syntax": ["error", "LabeledStatement", "WithStatement"],
"no-implicit-coercion": ["error", { "boolean": false }],
"no-new-wrappers": ["error"],
"no-new-object": ["error"],
"new-cap": ["error", { "newIsCap": false, "capIsNew": false }],
"no-array-constructor": ["error"],
"max-params": ["error", 5],
"max-lines": ["error", 500],
"max-statements": ["error", 50],
"one-var": ["error", "never"],
"max-nested-callbacks": ["error", 3],
"no-negated-condition": "error",
"no-case-declarations": 0,
"class-methods-use-this": 0,
"no-param-reassign": 0,
"no-use-before-define": 0,
"no-plusplus": 0,
"no-console": 0,
"prefer-destructuring": 0,
"consistent-return": 0,
"guard-for-in": 0,
"max-classes-per-file": 0,

"prettier/prettier": "warn",
"import/order": "warn",
"import/prefer-default-export": 0,
"import/no-unresolved": 0,
"@typescript-eslint/ban-ts-comment": ["error", { "ts-expect-error": true }],
"@typescript-eslint/no-inferrable-types": 0,
"@typescript-eslint/ban-types": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unused-vars": 0
},
"overrides": [
{
"files": ["webpack.config.js"],
"env": {
"browser": false,
"node": true
}
},
{
"files": ["src/**/*.spec.ts"],
"env": {
"jest": true
},
"rules": {
"@typescript-eslint/unbound-method": 0,
"@typescript-eslint/no-empty-function": 0,
"max-lines": ["error", 1200]
}
}
]
}

30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: tests
on: [push, pull_request]

jobs:
tests:
name: Run Tests
runs-on: ubuntu-latest

steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v2
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."

- uses: actions/setup-node@v2
with:
node-version: '16.5.0'

- name: Install
run: yarn install

- name: Run Tests
run: yarn test
- name: Submit coverage
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
run: cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/dist
.DS_Store
/.rpt2_cache
yarn-error.log
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

38 changes: 17 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# prouter

[![build status](https://travis-ci.org/rogerpadilla/prouter.svg?branch=master)](https://travis-ci.org/rogerpadilla/prouter?branch=master)
[![coverage status](https://coveralls.io/repos/rogerpadilla/prouter/badge.svg?branch=master)](https://coveralls.io/r/rogerpadilla/prouter?branch=master)
[![dependencies status](https://david-dm.org/rogerpadilla/prouter/status.svg)](https://david-dm.org/rogerpadilla/prouter/status.svg)
[![dev dependencies status](https://david-dm.org/rogerpadilla/prouter/dev-status.svg)](https://david-dm.org/rogerpadilla/prouter/dev-status.svg)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/sonemonu/prouter/blob/main/LICENSE)
[![tests](https://github.com/sonemonu/prouter/actions/workflows/tests.yml/badge.svg)](https://github.com/sonemonu/prouter)
[![coverage status](https://coveralls.io/repos/sonemonu/prouter/badge.svg?branch=main)](https://coveralls.io/r/sonemonu/prouter?branch=main)
[![npm downloads](https://img.shields.io/npm/dm/prouter.svg)](https://www.npmjs.com/package/prouter)
[![npm version](https://badge.fury.io/js/prouter.svg)](https://www.npmjs.com/prouter)

Expand All @@ -12,14 +11,16 @@ Fast, unopinionated, minimalist client side router library inspired in the simpl
Basically, give prouter a list of path expressions (routes) and a callback function (handler) for each one, and prouter will invoke the callbacks according to the activated path in the URL.

## Why prouter?
- **Performance:** [fast](https://github.com/rogerpadilla/prouter/blob/master/src/browser-router.spec.ts#L7) and tiny size (currently under 5kb before gzipping) are both must to have to smoothly run in any mobile or desktop browser.

- **Performance:** [fast](https://github.com/sonemonu/prouter/blob/master/src/browser-router.spec.ts#L7) and tiny size (currently under 5kb before gzipping) are both must to have to smoothly run in any mobile or desktop browser.
- **KISS principle everywhere:** do only one thing and do it well, routing! Guards? conditional execution? generic pre and post middlewares? all that and more is easily achivable with prouter (see examples below).
- **Learn once:** express router is very powerfull, flexible and simple, why not bringing a similar API to the frontend? Under the hood, prouter uses the same (wonderful) library that express for parsing routes [path-to-regexp](https://github.com/pillarjs/path-to-regexp) (so it allows the same flexibility to declare routes). Read more about the concept of middlewares [here](https://expressjs.com/en/guide/writing-middleware.html).
- **Unobtrusive:** it is designed from the beginning to play well with vanilla JavaScript or with any other library or framework.
- **Forward-thinking:** written in TypeScript for the future and transpiled to es5 with UMD format for the present... thus it transparently supports any module style: es6, commonJS, AMD. By default, prouter uses the modern [history](https://developer.mozilla.org/en-US/docs/Web/API/History_API) API for routing.
- Unit tests for every feature are created.

### Want to see it in action?

It is being used in this web app [wherudo.com](https://wherudo.com). There you can see how prouter is used to to do universal rendering for all the pages of that site.

## Installation
Expand Down Expand Up @@ -66,7 +67,6 @@ router
router.listen();
```


### guard middleware which conditionally avoid executing next handlers and prevent changing the path in the URL

```js
Expand All @@ -81,9 +81,8 @@ const router = prouter.browserRouter({
// Declare the paths and its respective handlers
router
.use('*', (req, resp, next) => {

// this handler will run for any routing event, before any other handlers

const isAllowed = authService.validateHasAccessToUrl(req.path);

if (!isAllowed) {
Expand Down Expand Up @@ -116,7 +115,6 @@ router.listen();
router.push('/admin');
```


### run a generic middleware (for doing some generic stuff) after running specific handlers

```js
Expand Down Expand Up @@ -144,7 +142,6 @@ router
router.listen();
```


### modularize your routing code in different files using Router Group

```js
Expand All @@ -161,7 +158,7 @@ productRouterGroup
resp.end();
})
.use('/create', (req, resp) => {
// do some stuff...
// do some stuff...
// and end the request-response cycle
resp.end();
})
Expand Down Expand Up @@ -198,7 +195,7 @@ router.listen();
router.push('/product/123');
```

### full example: modularized routing, generic pre handler acting as a guard, generic post handler.
### full example: modularized routing, generic pre handler acting as a guard, generic post handler

```js
import { browserRouter, routerGroup } from 'prouter';
Expand All @@ -214,7 +211,7 @@ productRouterGroup
next();
})
.use('/create', (req, resp, next) => {
// do some stuff...
// do some stuff...
// and pass control to the next handler
next();
})
Expand Down Expand Up @@ -249,23 +246,23 @@ router
next();
})
.use('/', (req, resp, next) => {

const doInfiniteScroll = () => {
// do infinite scroll ...
};
};

const onNavigation = (navigationEvt) => {
console.log('new path', navigationEvt.oldPath);
console.log('old path', navigationEvt.newPath);
// if navigating, then remove the listener for the window.scroll.
// if navigating, then remove the listener for the window.scroll.
router.off('navigation', onNavigation);
window.removeEventListener('scroll', doInfiniteScroll);
};

window.addEventListener('scroll', doInfiniteScroll);

// subscribe to the navigation event
router.on('navigation', onNavigation);
router.on('navigation', onNavigation);

// and pass control to the next handler
next();
Expand Down Expand Up @@ -341,7 +338,7 @@ document.body.addEventListener('click', (evt) => {
if (!isApplicationPath(url)) {
return;
}

// avoid the default browser's behaviour when clicking on a link
// (i.e. do not reload the page).
evt.preventDefault();
Expand All @@ -351,5 +348,4 @@ document.body.addEventListener('click', (evt) => {
});
```


### see more advanced usages in the [unit tests.](https://github.com/rogerpadilla/prouter/blob/master/src/browser-router.spec.ts)
### see more advanced usages in the [unit tests.](https://github.com/sonemonu/prouter/blob/master/src/browser-router.spec.ts)
Loading

0 comments on commit 3787a57

Please sign in to comment.