-
Notifications
You must be signed in to change notification settings - Fork 45
Housekeeping #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Housekeeping #23
Changes from all commits
2e1e014
0c9ee74
e428540
71efb36
22ff5a7
b7b38f1
d89a8fb
169f50c
9fa7814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
module.exports = { | ||
extends: 'airbnb-base', | ||
parserOptions: { | ||
sourceType: 'script' | ||
}, | ||
rules: { | ||
strict: ['error', 'safe'], | ||
'no-underscore-dangle': 'off', | ||
'no-param-reassign': 'off', | ||
'no-unused-vars': ['error', { 'argsIgnorePattern': 'next|res|req|err' }] | ||
} | ||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
.nyc_output/ | ||
.tern-port | ||
coverage/ | ||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
language: node_js | ||
node_js: | ||
- "node" | ||
- "8" | ||
- "10" | ||
- "12" | ||
- lts/* | ||
- node | ||
|
||
script: | ||
- yarn lint | ||
- yarn test | ||
|
||
deploy: | ||
provider: npm | ||
email: [email protected] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,34 @@ | ||
'use strict'; | ||
|
||
const { IncomingMessage, ServerResponse } = require('http'); | ||
const Layer = require('express/lib/router/layer'); | ||
const Router = require('express/lib/router'); | ||
const { Router } = require('express'); | ||
|
||
const last = (arr = []) => arr[arr.length - 1]; | ||
const noop = Function.prototype; | ||
const isError = (arg) => arg instanceof Error; | ||
const isRequest = (arg) => arg instanceof IncomingMessage; | ||
const isResponse = (arg) => arg instanceof ServerResponse; | ||
const noop = () => {}; | ||
|
||
function copyFnProps(oldFn, newFn) { | ||
Object.keys(oldFn).forEach((key) => { | ||
newFn[key] = oldFn[key]; | ||
}); | ||
return newFn; | ||
function copyProps(source, dest) { | ||
return Object.keys(source).reduce((acc, key) => { | ||
const value = source[key]; | ||
return Object.assign(acc, { [key]: value }); | ||
}, dest); | ||
} | ||
|
||
function wrap(fn) { | ||
const newFn = function newFn(...args) { | ||
const ret = fn.apply(this, args); | ||
const next = (args.length === 5 ? args[2] : last(args)) || noop; | ||
if (ret && ret.catch) ret.catch(err => next(err)); | ||
const predicates = [isError, isRequest, isResponse]; | ||
const next = args.find((arg) => predicates.every((match) => !match(arg))) || noop; | ||
if (ret && ret.catch) ret.catch((err) => next(err)); | ||
return ret; | ||
}; | ||
Object.defineProperty(newFn, 'length', { | ||
value: fn.length, | ||
writable: false, | ||
}); | ||
return copyFnProps(fn, newFn); | ||
} | ||
|
||
function patchRouterParam() { | ||
const originalParam = Router.prototype.constructor.param; | ||
Router.prototype.constructor.param = function param(name, fn) { | ||
fn = wrap(fn); | ||
return originalParam.call(this, name, fn); | ||
}; | ||
return copyProps(fn, newFn); | ||
} | ||
|
||
Object.defineProperty(Layer.prototype, 'handle', { | ||
|
@@ -44,4 +42,8 @@ Object.defineProperty(Layer.prototype, 'handle', { | |
}, | ||
}); | ||
|
||
patchRouterParam(); | ||
const originalParam = Router.prototype.constructor.param; | ||
Router.prototype.constructor.param = function param(name, fn) { | ||
fn = wrap(fn); | ||
return originalParam.call(this, name, fn); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not change the value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Value of |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you expand on the reason for this refactor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought
reduce
serves better purpose conveying message to the reader because source object is reduced (Yeah it is mapped but every mapper is in fact a reducer) to destination one. Plusreduce
has return value compared toforEach
which I thought is neat and leveraged here.