Skip to content

Commit

Permalink
feat: replace var with const and let (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacanger committed Apr 28, 2020
1 parent 90ef8a7 commit 4e44c08
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 336 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ node_js:
- "9"
- "10"
- "11"
- "12"
notifications:
email:
on_success: never
48 changes: 24 additions & 24 deletions lib/layer.js
@@ -1,6 +1,6 @@
var debug = require('debug')('koa-router');
var { pathToRegexp, compile, parse } = require('path-to-regexp');
var uri = require('urijs');
const debug = require('debug')('koa-router');
const { pathToRegexp, compile, parse } = require('path-to-regexp');
const uri = require('urijs');

module.exports = Layer;

Expand All @@ -25,17 +25,17 @@ function Layer(path, methods, middleware, opts) {
this.paramNames = [];
this.stack = Array.isArray(middleware) ? middleware : [middleware];

for(var i = 0; i < methods.length; i++) {
var l = this.methods.push(methods[i].toUpperCase());
for(let i = 0; i < methods.length; i++) {
const l = this.methods.push(methods[i].toUpperCase());
if (this.methods[l-1] === 'GET') {
this.methods.unshift('HEAD');
}
}

// ensure middleware is a function
for (var i = 0; i < this.stack.length; i++) {
var fn = this.stack[i];
var type = (typeof fn);
for (let i = 0; i < this.stack.length; i++) {
const fn = this.stack[i];
const type = (typeof fn);
if (type !== 'function') {
throw new Error(
methods.toString() + " `" + (this.opts.name || path) +"`: `middleware` "
Expand Down Expand Up @@ -73,11 +73,11 @@ Layer.prototype.match = function (path) {
*/

Layer.prototype.params = function (path, captures, existingParams) {
var params = existingParams || {};
const params = existingParams || {};

for (var len = captures.length, i=0; i<len; i++) {
for (let len = captures.length, i=0; i<len; i++) {
if (this.paramNames[i]) {
var c = captures[i];
const c = captures[i];
params[this.paramNames[i].name] = c ? safeDecodeURIComponent(c) : c;
}
}
Expand Down Expand Up @@ -115,8 +115,8 @@ Layer.prototype.captures = function (path) {
*/

Layer.prototype.url = function (params, options) {
var args = params;
var url = this.path.replace(/\(\.\*\)/g, '');
let args = params;
const url = this.path.replace(/\(\.\*\)/g, '');

if (typeof params != 'object') {
args = Array.prototype.slice.call(arguments);
Expand All @@ -126,14 +126,14 @@ Layer.prototype.url = function (params, options) {
}
}

var toPath = compile(url, options);
var replaced;
const toPath = compile(url, options);
let replaced;

var tokens = parse(url);
var replace = {};
const tokens = parse(url);
let replace = {};

if (args instanceof Array) {
for (var len = tokens.length, i=0, j=0; i<len; i++) {
for (let len = tokens.length, i=0, j=0; i<len; i++) {
if (tokens[i].name) replace[tokens[i].name] = args[j++];
}
} else if (tokens.some(token => token.name)) {
Expand All @@ -145,7 +145,7 @@ Layer.prototype.url = function (params, options) {
replaced = toPath(replace);

if (options && options.query) {
var replaced = new uri(replaced)
replaced = new uri(replaced)
replaced.search(options.query);
return replaced.toString();
}
Expand Down Expand Up @@ -177,18 +177,18 @@ Layer.prototype.url = function (params, options) {
*/

Layer.prototype.param = function (param, fn) {
var stack = this.stack;
var params = this.paramNames;
var middleware = function (ctx, next) {
const stack = this.stack;
const params = this.paramNames;
const middleware = function (ctx, next) {
return fn.call(this, ctx.params[param], ctx, next);
};
middleware.param = param;

var names = params.map(function (p) {
const names = params.map(function (p) {
return p.name;
});

var x = names.indexOf(param);
const x = names.indexOf(param);
if (x > -1) {
// iterate through the stack, to figure out where to place the handler fn
stack.some(function (fn, i) {
Expand Down

0 comments on commit 4e44c08

Please sign in to comment.